首页  >  问答  >  正文

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

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

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

给我你的怀抱给我你的怀抱2661 天前872

全部回复(2)我来回复

  • 黄舟

    黄舟2017-06-13 09:26:33

    官方的建议是,在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/...

    回复
    0
  • 天蓬老师

    天蓬老师2017-06-13 09:26:33

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

    回复
    0
  • 取消回复