Configure Your Symfony Parameters with The .env Manager in Forge and Envoyer

tomat
tomat
Published in
2 min readJan 8, 2017

--

This is a simple solution for using the .env editor built into Envoyer and Forge to set your Symfony configuration parameters. As long as you’re using the Symfony Standard distribution you don’t need to install any extra packages.

First configure your .env file content with whatever parameters you need, for example:

export DATABASE_USER="forge"
export DATABASE_NAME="forge"
export DATABASE_PASSWORD="secret"

Note that we put export before each variable so that we can source the file from our shell later. You can name these variables anything.

Now we need to actually tell Symfony what these variables mean. Thankfully the package incenteev/composer-parameter-handler is already included in the Symfony Standard distribution, and has a handy option called env-map, which is configured in your composer.json file:

{
"extra": {
"incenteev-parameters": {
"file": "app/config/parameters.yml",
"env-map": {
"database_user": "DATABASE_USER",
"database_password": "DATABASE_PASSWORD",
"database_name": "DATABASE_NAME"
}
}
}
}

Here, of course, database_user is the name of a parameter, and DATABASE_USER is the name of the environment variable that we want mapped to that parameter. The ParameterHandler should already be configured in the scripts part of your composer.json file; if not, check the original in the Symfony Standard distribution.

Now we just need to actually load the environment variables from the .env file before composer install has been run in the deploy workflow.

In Envoyer we simply configure our own deployment hook that runs before “Install Composer Dependencies”, something like this:

cd {{release}}
source .env
composer install -n -o --no-dev

Make sure there is no parameters.yml file commited, and if you’re using Forge you should add a rm -f app/config/parameters.yml line as well before composer install since it will be present from earlier deploys.

--

--