Laravel5.4 不同环境下 env 文件设置

来源:互联网 发布:软件自动升级方案 编辑:程序博客网 时间:2024/06/15 13:01

Laravel5.4现在支持不同环境下env文件设置(好像是L5.4最新支持的吧,记不清楚了,也有可能L5.2-5.3就已经支持了),可以针对不同环境(development, staging, production)设置env文件为:

development: .env.developmentstaging: .env.stagingproduction: .env.production

根据不同环境服务器设置系统变量(可根据phpinfo()查看APP_ENV环境变量是否OK):

development: APP_ENV=developmentstaging: APP_ENV=stagingproduction: APP_ENV=production

这样,项目根目录下就会有根据不同环境对应的.env.xxx文件,放入版本控制,本地的环境对应.env不需要放入版本控制。

原理可看laravel的源码:

namespace Illuminate\Foundation\Bootstrap;use Dotenv\Dotenv;use Dotenv\Exception\InvalidPathException;use Symfony\Component\Console\Input\ArgvInput;use Illuminate\Contracts\Foundation\Application;class LoadEnvironmentVariables{    /**     * Bootstrap the given application.     *     * @param  \Illuminate\Contracts\Foundation\Application  $app     * @return void     */    public function bootstrap(Application $app)    {        if ($app->configurationIsCached()) {            return;        }        $this->checkForSpecificEnvironmentFile($app);        try {            (new Dotenv($app->environmentPath(), $app->environmentFile()))->load();        } catch (InvalidPathException $e) {            //        }    }    /**     * Detect if a custom environment file matching the APP_ENV exists.     *     * @param  \Illuminate\Contracts\Foundation\Application  $app     * @return void     */    protected function checkForSpecificEnvironmentFile($app)    {        if (php_sapi_name() == 'cli' && with($input = new ArgvInput)->hasParameterOption('--env')) {            $this->setEnvironmentFilePath(                $app, $app->environmentFile().'.'.$input->getParameterOption('--env')            );        }        if (! env('APP_ENV')) {            return;        }        $this->setEnvironmentFilePath(            $app, $app->environmentFile().'.'.env('APP_ENV')        );    }
原创粉丝点击