3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Laravelのヘルパ関数env()でnullが返却されていた話

Last updated at Posted at 2018-09-11

「サーバーにあげたら挙動がおかしくなった:angel_tone2:
という事があったのでメモです。

原因

以下のコマンドでキャッシュを作った事により、env()はnullしか返してくれなくなった!

php artisan config:cache

ということでした。
ソースを見ると、そんな仕様みたいです・・・:sweat_smile:

日本語訳のマニュアルには以下のように記載されています:writing_hand_tone1:
https://readouble.com/laravel/5.5/ja/helpers.html#method-env

Note: 開発期間中にconfig:cacheコマンドを実行する場合は、設定ファイルの中で必ずenv関数だけを使用してください。設定ファイルがキャッシュされると、.envファイルはロードされなくなり、env関数の呼び出しは全てnullを返します。

(ちょっと表現がおかしい・・・?)

対策

env()関数はconfigファイル内だけで使うようにする!
config以外で使いたい場合は、一度configに定義した上で、config()関数で取得する!
です。

知っていればそれだけなんですが、知らないとハマる可能性高いので、あらかじめチームでは共有しておいた方がよいですね・・・:sweat_smile:

3
3
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?