筆者寫一個Artisan
指令,在App\Console
下的command
檔案內呼叫getenv
方法,來取得.env
的配置,結果回傳null;
但是透過在config/services.php
取得.env
的配置,再在App\Console
下的command
文件呼叫config
方法,就能取得想要的環境變數值, 為什麼會這樣呢?
黄舟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 appelsecation.
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, ing you to your sours configur toyour to ite you toal ing you.摘自 https://laravel.com/docs/5.2/...