laravel 的 .env 文件只有 config 文件夹内调用才有效吗?
给我你的怀抱
给我你的怀抱 2017-06-13 09:24:33
0
2
870

笔者写一个 Artisan命令,在AppConsole下的command文件内调用getenv方法,来获取.env的配置,结果返回 null;

但是通过在config/services.php获取.env的配置,再在AppConsole下的command文件调用config方法,就能获取到想要的环境变量值, 为什么会这样呢?

给我你的怀抱
给我你的怀抱

全部回复(2)
黄舟

官方的建议是,在config里面调用env,在别的地方调用config。

这其实是个大坑,我第一次写laravel的时候就发现这个env在控制器里面就是读不出来。(也不是百分百读不出来,是执行了 php artisan config:cache 之后读不出来)

Caching And Env

If you are using the config:cache command during deployment, you must make sure that you are only calling the env function from within your configuration files, and not from anywhere else in your application.

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.

摘自 https://laravel.com/docs/5.2/...

Peter_Zhu

Laravel 有辅助函数 env() 呀= =

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!