checkForSpecificEnvironmentFile() 只會加載 .env 或 .env.env('APP_ENV')
優先順序:.env.env('APP_ENV') -> .env
ref: Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables checkForSpecificEnvironmentFile()
沒有下 php artisen optimize 取 env 的流程大概是這樣
1. app\Http\Kernel handle() <-- public/index.php line 51
2. app\Http\Kernel bootstrap()
3. $app->bootstrapWith(app\Http\Kernel::$bootstrappers):載入 .env
4. bootstrap ENV 後,執行 $app->afterLoadingEnvironment() callback:載入 .env.local, opt/.env
下 php artisen optimize 後,3 跟 4 會變成
3. 有 cache ,不載入 .env
4. 因為未載入 .env ,所以 env('APP_ENV') 為 null ,不會載入 .env.local
php artisen optimize 寫 cache 大概的流程
1. app\Console\Kernel handle() <-- artisan.php line 35
2. app\Console\Kernel bootstrap()
3. $app->bootstrapWith(app\Console\Kernel::$bootstrappers):載入 .env
4. bootstrap ENV 後,執行 $app->afterLoadingEnvironment() callback:載入 .env.local, opt/.env
5. 執行 optimize
- config cache clear
- config cached:app\Console\Kernel bootstrap(),重複 3 和 4 ,但 3 載入 .env.env('APP_ENV')
- route cache clear
- route checed
因此,執行 php artisan optimize 的話,afterLoadingEnvironment 的 callback 可以不放 .env.local 載入那段。
另,在有 cache 的情況下,執行 php artisan optimize ,步驟 3 會因為有 cache 不載入 .env , 5 的 config cached 因為 env('APP_ENV') = null ,載入 .env ,此時 afterLoadingEnvironment 的 callback 又沒有 .env.local 載入那段,造成 cached config 用到 .env.local 的值都會為空。
