有人对env
个变量有问题吗?由于某些原因,每次我使用帮助器env('VARIABLE')
都会返回null
.这件事发生得非常出乎意料,我真的不知道原因.重新启动Apache/IDE/计算机无法正常工作.
有人对env
个变量有问题吗?由于某些原因,每次我使用帮助器env('VARIABLE')
都会返回null
.这件事发生得非常出乎意料,我真的不知道原因.重新启动Apache/IDE/计算机无法正常工作.
The solution is simple, but neither the IDE nor the debugger says anything about it. It just returns null
. When you use php artisan config:cache
, according to the documentation:
If you execute
php artisan config:cache
command during your deployment process, you should be sure that you are only calling theenv()
function from within your configuration files.
Obviously I have env
variables outside the config files, so after caching I was not able to use it outside anymore. The php artisan config:clear
puts it back to work.
What I've found more about the usage of env
, that it should be used only within config files. You can access env
variables from the rest of the project using other helper method config()
. Be sure to assign it to another key in config file, e.g. 'key' => env('CACHE_DRIVER')
更重要的是,您必须记住每次更改.env
文件时都要运行php artisan config:cache
.在缓存新值之前,Laravel不会加载新值.如果没有缓存,则无需执行此操作.