October 30, 2018 Javier Eguiluz

Since the introduction of the DotEnv component in Symfony 3.3, we've been working hard to improve the experience of using env vars to configure Symfony apps. In Symfony 4.2, we've added a new loadForEnv() method to allow you define different env vars per environment.

Instead of creating a single .env file at your project's root dir, you can now create one or more env var files. The values for the env var are looked for in the following order:

.env.$APP_ENV.local (don't commit this file to the repository) defines environment-specific overrides for your local machine;

(don't commit this file to the repository) defines environment-specific overrides for your local machine; .env.$APP_ENV (commit this file to the repository) and defines environment-specific defaults for all developers/machines;

(commit this file to the repository) and defines environment-specific defaults for all developers/machines; .env.local (don't commit this file to the repository) overrides the main .env config only for your local machine;

(don't commit this file to the repository) overrides the main config only for your local machine; .env (commit this file to the repository) and defines the default values for the env vars used in the application.

We're working on a Symfony Flex recipe to bring this new feature into the full Symfony framework (see symfony/recipes #466). There's no need to change anything in your existing applications if you don't want to, but new projects will get the latest recipes versions, with the following changes: