有人对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:cachecommand during your deployment process, you should be sure that you are only calling the env() 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不会加载新值.如果没有缓存,则无需执行此操作.

Laravel相关问答推荐

laravel vue 惯性分页器删除上一个和下一个链接

如何从laravel中的另一个表中获取用户名

1 子编译中的警告(使用'stats.children:true'和'--stats-children'了解更多详细信息)

如何更改 laravel sanctum 返回message: Unauthenticated.

Laravel 中的合同和 PHP 中的接口有什么区别?

调用未定义的函数 mb_strimwidth

在 Laravel 4 迁移中创建 MYSQL 过程

Laravel/Lumen 中两列之间的 SQL

Laravel 5.4 有时验证规则不起作用

扩展模型 == 扩展 Eloquent?

为什么 BroadCastEvent 在 Laravel 中排队?如何阻止它?

升级到 laravel 5.3 后错误 datetime format: 1292 Incorrect datetime value: '0000-00-00 00:00:00'

Laravel - 动态创建表(无需迁移)

Laravel 5.3 通知 - 仅使用Electron邮件地址通知

Laravel 存储链接不适用于生产

如何清理 Laravel Bootstrap 缓存配置文件?

Laravel 表:只能有一个自动列,并且必须定义为键

在 laravel 的自定义路径中创建模型

Eloquent的关系 - attach附加(但不保存)到 Has Many

带有 Xdebug 的 Laravel 5 总是抛出The payload is invalid..