Laravel5.4 不同环境下 env 文件设置
Laravel5.4 现在支持不同环境下 env
文件设置 (好像是 L5.4 最新支持的吧,记不清楚了,也有可能 L5.2-5.3 就已经支持了),可以针对不同环境 (development, staging, production) 设置 env 文件为:
development: .env.development
staging: .env.staging
production: .env.production
根据不同环境服务器设置系统变量 (可根据 phpinfo()
查看 APP_ENV
环境变量是否 OK):
development: APP_ENV=development
staging: APP_ENV=staging
production: 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')
);
}
2019-02-19 更新#
APP_ENV
可在 etc/nginx/fastcgi.conf
中设置就行,这样 nginx 会把这些常量传给 PHP 作为环境变量:
.
.
.
fastcgi_param APP_ENV staging;
本作品采用《CC 协议》,转载必须注明作者和本文链接
推荐文章: