Hy, I'm using Laravel 5.2.27 and I want to load different .env files based on the current set environment.
This feature is also described in the Documentation
But when I use artisan via php artisan ... --env=client only the .env files is loaded and not the .env.client file.
I assume artisan --env does't set the APP_ENV key right. When I debug Illuminate\Foundation\Bootstrap\checkForSpecificEnvironmentFile I get null for env('APP_ENV') and so no other file is loaded!?
Did you clear the config cache?
We only ever load the env files in order to populate the config cache, or if there's not a config cache.
hy, yes I tried to clear the config cache, nothing changed.
so if I want to use one instance of laravel to run two identical apps but with different configs, this isn't really possible?
The plan is to make subdomains like client1.app.com and client2.app.com and each subdomain has his own .env.clienXfile. But only one .env file would be loaded and cached?
What would be the best way so approach such a solution in laravel?
hy, yes I tried to clear the config cache, nothing changed.
When I say clear, I really do mean :clear, not :cache, as in, refreshing the cache. I really mean delete it.
Please ask on the forums.
I am having the same type of issue.
php artisan --env=testing migrate:refresh for example is just refreshing the database specified in .env. In fact so does php artisan --env=foo migrate:refresh so it seems like the --env option is not working at all.
yes that's right, seems like --env=foo isn't passed right into the laravel app starup and therefore always the .env file loads.
I assume artisan --env does't set the APP_ENV key right. When I debug IlluminateFoundationBootstrapcheckForSpecificEnvironmentFile I get null for env('APP_ENV') and so no other file is loaded!?
@GrahamCampbell why is this closed?
I have done config:clear like you said but it does not work.
@di5abled is right that APP_ENV is always null which means the env that is loaded will always be the default .env file.
I can confirm. Got files: .env .env.testing and: php artisan migrate --env=testing is always executed with settings from .env
@flashek And you definitely cleared the config cache FIRST? Not the same as recaching.
15:42 $ php artisan config:clear
Configuration cache cleared!
15:42 $ php artisan --env=testing migrate
Nothing to migrate.
Also, the cache directory is empty:
15:47 $ ll storage/framework/cache/
total 8
drwxr-xr-x 3 flash staff 102 20 kwi 16:00 .
drwxr-xr-x 7 flash staff 238 20 kwi 16:01 ..
-rwxr-xr-x 1 flash staff 13 25 lut 12:55 .gitignore
Started xdebug to find out what is going on. In file https://github.com/laravel/framework/blob/5.2/src/Illuminate/Foundation/Bootstrap/DetectEnvironment.php#L39 the condition is true so it ends with just return. It should go futher and load proper .env file. Somehow optional --env=testing wasn't counted in.
Well the proper .env file would be loaded correctly with the next few lines there if only the APP_ENV was set before hand, but that's not happening.
I think the Illuminate\Foundation\Bootstrap\LoadConfiguration bootstrapper does load the APP_ENV but that only happens later on.
IMHO APP_ENV should be set based on --env= parameter to continue loading the rest of variables.
I did: export APP_ENV=testing and it works but anyway it shouldn't work that way?
I made PR #13351
Thanks @flashek.
Thanks
I just spent so many HOURS on this problem. My head was going to explode. I was so confused. I'm really disappointed that Laravel 5.2 didn't accept the pull request since this was clearly broken functionality.
@nowak-ninja Thank you for the export APP_ENV=testing hint and for the pull request.
Dang, this gets me every time. Thanks to Google for bringing me back here.
I really wish the --env option worked. It's weird that sometimes export APP_ENV=testing is required instead.
Most helpful comment
Dang, this gets me every time. Thanks to Google for bringing me back here.
I really wish the
--envoption worked. It's weird that sometimesexport APP_ENV=testingis required instead.