问题描述
我试图找出为什么我的 env()
助手总是返回 null
.这会导致麻烦,尤其是在 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)
Laravel 文档说
如果您在部署期间使用 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!