Laravel 5-env()始终返回null [英] Laravel 5 - env() always returns null
问题描述
我试图找出为什么我的env()
助手总是返回null
的原因.这尤其在app.php
文件中引起麻烦,在app.php
文件中,默认情况下广泛使用env()
帮助器.也许有任何神秘的服务器设置?
I try to find out why my env()
helper always returns null
. This causes trouble especially in app.php
file, where are env()
helpers widely used by default. Perhaps any mysterious server setting?
我的环境文件:
APP_ENV=production
APP_KEY=base64:mymagickey=
APP_DEBUG=false
APP_LOG_LEVEL=info
APP_URL=http://www.example.com
etc...
编辑-我尝试了以下操作:
php artisan cache:clear
php artisan view:clear
php artisan config:cache
当然,我正在使用像这样的env
助手:env('APP_ENV')
and ofcourse, i am using env
helper like this: env('APP_ENV')
但是仍然没有成功.奇怪的是,$_ENV
php变量包含.env
文件中的每个变量.
But still no success. The wierd part is, that $_ENV
php variable contains every single variable from .env
file.
推荐答案
env(...)
函数在缓存配置后将不起作用. (从laravel 5.2到当前的5.7)
env(...)
function will not work after you cached the config. (starting from laravel 5.2 till current 5.7)
如果在部署过程中使用
config:cache
命令,则必须确保仅从配置文件中调用env
函数,而不从应用程序中的其他任何地方调用.
If you are using the
config:cache
command during deployment, you must make sure that you are only calling theenv
function from within your configuration files, and not from anywhere else in your application.
所以正确的答案应该是
如果要从应用程序内部调用env,则强烈建议您向配置文件中添加适当的配置值,然后从该位置调用env,从而使您可以将env调用转换为配置调用.
If you are calling env from within your application, it is strongly recommended you add proper configuration values to your configuration files and call env from that location instead, allowing you to convert your env calls to config calls.
我引用了相同的文档
但是要快速修复,可以这样做:
But for a quick fix this will do:
php artisan config:clear
现在应该很清楚为什么在尝试config:cache
时,即使在缓存之前清除了配置,它也无济于事.
And now it should be clear why, when you tried config:cache
, it did not help, even though it clears the config prior to caching.
这篇关于Laravel 5-env()始终返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!