Home > Back-end >  Laravel Dynamically load different .env file
Laravel Dynamically load different .env file

Time:10-11

I would like to ask how can I possibly dynamically load different env file setting? I have read the documentation, I have created two files (.env and .env.uat) .env for development and .env.uat for client testing environment, and it depends on the url to use different env file, eg: (company-dev) -> .env, (company-uat) -> .env.uat

I have added these two lines of code to bootstrap/app.php, actually it works, but when I want to execute php artisan migrate, then it said that HTTP_HOST couldn't found, so it will load the .env.uat as the fallback file. Can someone tell me where should I modify code please? Thanks!! (Actually I knew I can manually change the .env file in different environment everytime, I am seeking some automatic way to recognise the env file for me. $envFile = $_SERVER['HTTP_HOST'] == 'xxx-dev-testing.com' ? '.env' : '.env-uat'; $app->loadEnvironmentFrom($envFile);

CodePudding user response:

There's no HTTP_HOST in the command line. You'll need a different approach. Ultimately, though, .env shouldn't be in version control at all. Your UAT environment would just have a .env with different values.

  • Related