This example shows us how to use different environment files for DEV, TEST and PROD environments. In the case of PROD (production) environment, the variables are defined in Apache vhost file on the server for security reasons. We will use The Dotenv Component for DEV and TEST environments.


Setup


Install the Dotenv component with composer require --dev symfony/dotenv command.


Configurations


parameters.yml.dist


# app/config/parameters.yml.dist

parameters:
secret: '%env(APP_SECRET)%'

console


# bin/console

...
(new \Symfony\Component\Dotenv\Dotenv())->load(__DIR__.'/../app/config/env/.env.'.$env);

$kernel = new AppKernel($env, $debug);
...

Dev


# app/config/env/.env.dev

APP_SECRET=DevSecret

# web/app_dev.php

...
(new \Symfony\Component\Dotenv\Dotenv())->load(__DIR__.'/../app/config/env/.env.dev');

$kernel = new AppKernel('dev', true);
...

Test


# app/config/env/.env.test

APP_SECRET=TestSecret

# web/app_test.php

...
(new \Symfony\Component\Dotenv\Dotenv())->load(__DIR__.'/../app/config/env/.env.test');

$kernel = new AppKernel('test', true);
...

Prod


# /etc/apache2/sites-available/your_app.conf

...
SetEnv APP_SECRET ProdSecret
...

Test