laravel 的 .env 文件只有 config 文件夹内调用才有效吗?
给我你的怀抱
给我你的怀抱 2017-06-13 09:24:33
[PHP讨论组]

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

但是通过在config/services.php获取.env的配置,再在App\Console下的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/...

天蓬老师

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

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号