Symfony environment variables

Written on May 13, 2017 by Ricardo de Vries.

Symfony is a trademark of Fabien Potencier

This post may be outdated due to the release of the Dotenv component. See this blog post for more information.

Environment variables are increasingly popular to manage your application configuration. They are one of the main concepts of the twelve-factor app methodology. Their main advantages are that they can be changed between deploys without changing any code and that they don’t need to be checked into the code repository.

Since Symfony 3.2 there is full support for environment variables and in Symfony 3.3 the old SYMFONY__ variables are deprecated and will not be treated special anymore by Symfony in 4.0. The old SYMFONY__ variables behavior was simple, when the application container was built, the values of those environment variables were dumped into the compiled container. Therefore, if those variables changed during the application execution, the updated values were ignored.

Today we are going to change the Symfony Standard Edition and add environment variables to them. Lets start by downloading a new Symfony Standard Edition via the Symfony Installer1:

$ symfony new demo

 Downloading Symfony...

    5.5 MiB/5.5 MiB ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓  100%

 Preparing project...

 ✔  Symfony 3.3 was successfully installed. Now you can:

    * Change your current directory to /Users/Ricardo/demo

    * Configure your application in app/config/parameters.yml file.

    * Run your application:
        1. Execute the php bin/console server:start command.
        2. Browse to the http://localhost:8000 URL.

    * Read the documentation at [http://symfony.com/doc]

Directly go in to the newly created directory and execute the following commands:

$ cd demo
$ rm -f app/config/parameter*

Don’t be scared by removing these parameter files, we are going to use the environment variables these files are not longer needed. Do not forget to remove /app/config/parameters.yml from .gitignore:

Next up we need to add several environment variables to our development machine. The best location for environment variables in development is ~/.bash_profile. For production I advise you to use /etc/environment so that deployment scripts can access these variables if they are used with another user on that production server.

Add the following lines and adjust them if needed:

export SYMFONY_ENV="dev"
export SYMFONY_SECRET="2134bd98ff78a6950de9cb02d3fb958c4ae450d2"
export SYMFONY_LOCALE="en"
export SYMFONY_DB_HOST="127.0.0.1"
export SYMFONY_DB_PORT="3306"
export SYMFONY_DB_NAME="symfony"
export SYMFONY_DB_USER="root"
export SYMFONY_DB_PASS="password"

Close your terminal completly and re-open it to activate your variables. You can test if they work by issuing:

$ echo $SYMFONY_ENV

Then we need to edit our app/config/config.yml file. First we need to remove the import of the parameter file. Lets edit the values of all the others. For environment variables, you need to use '%env(PARAMETER)%' to grab and use the environment variables in Symfony. For example, to activate the doctrine hostname variable we need to change host: '%database_host%' to host: '%env(SYMFONY_DB_PORT)%'. Change all the variables accordingly to their environment variable name.

Now we need to edit our composer.json file to remove some command involving the parameter files. Look at line 42 and remove that line. Also remove line 66, 67 and 68. When you removed those lines, it is time to remove a composer package from the installation. This package takes care of the parameter files but since we removed them, the package is obsolete for our project:

$ composer remove incenteev/composer-parameter-handler

If anything went well, you did not recieve any error. Congratulations, you are now using environment variables instead of a parameter file. See you next time.

  1. The Symfony Installer is a tool to easily install a new symfony codebase within seconds. See their documentation for more information.