New in Symfony 4.2: Define env vars per environment

Home Dev New in Symfony 4.2: Define env vars per environment

Contributed by
Kévin Dunglas
in #28533.

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 + environment name + .local (.env.dev.local, .env.test.local,
    .env.prod.local). These are the environment-specific settings overridden
    locally on your machine.
  • .env.local. Used for overriding values on your local machine. This file is
    loaded for all environments except test.
  • .env + environment name (.env.dev, .env.test, .env.prod). These
    are the files that define environment-specific settings. Useful for example to
    override settings like the database connection for tests.
  • .env. Same file as used by current Symfony apps.

We’re working on a Symfony Flex recipe to bring this new feature into the full
Symfony framework (see symfony/recipes #466). Once merged, you’ll need to make
the following changes in your app:

  • Rename .env.dist as .env (and commit it to the shared repository).
  • Rename .env as .env.local (and remove it from the shared repository).

Be trained by Symfony experts
2018-11-5 Paris
2018-11-5 Paris
2018-11-7 Paris

Leave a Reply

Your email address will not be published.