SmxCde SmxCde - 3 months ago 31
PHP Question

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!

Answer

Nadeem0035 gave me pretty good idea what to do

bootstrap\app.php right before return $app;

$envFile = $_SERVER['HTTP_HOST'] == 'prod.domain.com' ? '.env-production' : '.env-dev';
$app->loadEnvironmentFrom($envFile);