[英]Laravel not reading env variables from different env files
从5.0升级到5.8后,如果env与.env文件不同,则laravel停止读取env变量。
例如,如果我在.env
带有USE_SSL=true
.env
文件。 env('USE_SSL')
将为true
但是,如果我有指向另一个环境的.env
文件:
APP_ENV=dev
然后,我将拥有包含USE_SSL=true
.env.dev
文件, env('USE_SSL')
将为null。
composer dump-autoload
和php artisan config:clear
,以及php artisan config:cache
不走运。 缓存与否,我无法获得价值。 任何想法,将不胜感激。
我将其添加为答案,但是请注意,根据laravel文档,这不是应该使用.env文件的方式。 由于某些限制,这只是我需要使用的一种方式,这些限制要求我为每个env使用不同的配置文件,并在运行时加载它。 要正确使用.env文件,请检查docs 。
这是一种在运行时加载不同配置文件的方式,具体取决于APP_ENV指向的位置。 我将其标记为已回答,因为它正在回答此特定问题。
.env
APP_ENV=specific_domain
.env.specific_domain
USE_SSL=true
Http /内核.php
public function __construct(Application $app, Router $router)
{
parent::__construct($app, $router);
$app_env = explode("=", file($app->environmentFilePath(), FILE_IGNORE_NEW_LINES)[0])[1];
$app->loadEnvironmentFrom(".env.$app_env");
}
好吧,如果您真的想这样做,
Route::get('renderEnvChaining',function(){
$myCustomEnv = parse_ini_file(base_path(env('CUSTOM_ENV')));
return $myCustomEnv['USE_SSL']; // This will return true
});
在我的.env文件中,我提到了这一点,
CUSTOM_ENV=.env.example
现在我在默认的env.example我把它
USE_SSL="true" // This will return true
USE_SSL=true // This will return 1
我只是发现.ini和.env遵循相同的值,所以我将.env文件解析为.ini文件,
尝试一下,我之前从未做过,请在运行到生产服务器之前与其他开发人员联系。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.