23/02/2018 - SYMFONY
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.
Install the Dotenv component with composer require --dev symfony/dotenv
command.
# app/config/parameters.yml.dist
parameters:
secret: '%env(APP_SECRET)%'
# bin/console
...
(new \Symfony\Component\Dotenv\Dotenv())->load(__DIR__.'/../app/config/env/.env.'.$env);
$kernel = new AppKernel($env, $debug);
...
# 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);
...
# 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);
...
# /etc/apache2/sites-available/your_app.conf
...
SetEnv APP_SECRET ProdSecret
...
http://your_app/app_dev.php/
will use DevSecret
.http://your_app/app_tes.php/
will use TestSecret
.http://your_app/{app.php}/
will use ProdSecret
.