laravel 的 .env 檔案只有 config 資料夾內呼叫才有效嗎?
给我你的怀抱
给我你的怀抱 2017-06-13 09:24:33
0
2
882

筆者寫一個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 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/...
Peter_Zhu

Laravel 有輔助函數 env() 呀= =

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!