laravel 的 .env 文件只有 config 文件夹内调用才有效吗?

查看:283
本文介绍了laravel 的 .env 文件只有 config 文件夹内调用才有效吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

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

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

解决方案

官方的建议是,在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 文件只有 config 文件夹内调用才有效吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆