Skip to content
Advertisement

Laravel – custom .env file

Laravel assumes that .env file should describe environment, and it should not be committed to your repo.

What if I want to keep both .env files for dev and production (say .env-production and .env-dev) in my repo and add some custom logic to which file should be used, for example based on current domain name.

Something like

if ($_SERVER['HTTP_HOST'] == 'prod.domain.com') {
    load('.env-production');
} else {
    load('.env-dev');
}

How can i achieve it? Thanks!

Advertisement

Answer

Nadeem0035 gave me pretty good idea what to do

bootstrapapp.php right before return $app;

$envFile = $_SERVER['HTTP_HOST'] == 'prod.domain.com' ? '.env-production' : '.env-dev';
$app->loadEnvironmentFrom($envFile);
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement