在 Laravel 应用程序上哪里指定应用程序版本?

问题描述 投票:0回答:5

我曾经在

composer.json
中指定应用程序版本,直到我在 Stack Overflow 中读到这是一个不好的做法。在 PHP Laravel 应用程序上指定应用程序版本的标准文件是什么? (即:在 .NET 中,它是 config 文件,在 iOS 上,它是 info.plist,在 Android 上,它是 Manifest,等等... )

php laravel laravel-5
5个回答
55
投票

config/app.php
绝对是获取此类信息的地方。

为什么?因为配置文件(或应该)包含在您的版本控制流程中,并且它们(应该)包含有关您的应用程序的非敏感信息。简单版本指示器的完美位置。

在数组的

name
索引下,你可以像这样设置一个
version
索引

/*
|--------------------------------------------------------------------------
| Application Version
|--------------------------------------------------------------------------
|
| This value is the version of your application. This value is used when
| the framework needs to place the application's version in a notification
| or any other location as required by the application or its packages.
*/

'version' => '1.0.0',

11
投票

@delatbabel

版本有一个很好的开始

配置/app.php

'version' => env('APP_VERSION', '1.0.0'),

对于配置/错误:

'app_version' => env('APP_VERSION', '1.0.0'),

然后您可以简单地通过以下方式获取您的版本:

config('app.version')

Docker

现在在 docker 中你可以在构建上设置默认版本:

ENV APP_VERSION=1.0.0

例如,使用 Circle CI,您可以添加您的内部版本号:

ARG BUILD_NR
ENV APP_VERSION=1.0.$BUILD_NR

现在在您的 config.yml 中,您可以添加以下作业突击队:

 deliver-prod:
    machine: true
    steps:
      - checkout
      - docker build --build-arg BUILD_NR=$CIRCLE_BUILD_NUM .

4
投票

我通常将其设置在我的 .env 文件中,然后在配置中选择它。

例如.env 说:

APP_VERSION=2.1

config/app.php 说:

'version' => env('APP_VERSION', '0.0.1-alpha'),

1
投票

我现在的做法:GitLab 管道在每次成功构建后都会写入“version.php”文件。该文件包含以下内容:

<?php const VERSION = '1.2.3';

在“config/app.php”中我包含该文件

require_once __DIR__ . '/../version.php';

...

'version' => VERSION,

...

可以按如下方式查询版本(例如作为路由):

Route::get('api/version', function () {
   return ["version" => config('app.version')];
})
->name('version');

0
投票

这个包很旧但很有用https://github.com/antonioribeiro/version

© www.soinside.com 2019 - 2024. All rights reserved.