You are browsing the Symfony 4 documentation, which changes significantly from Symfony 3.x. If your app doesn't use Symfony 4 yet, browse the Symfony 3.4 documentation.

Nov 2018 Changes to .env & How to Update

4.2 version

Nov 2018 Changes to .env & How to Update

In November 2018, several changes were made to the core Symfony recipes related to the .env file. These changes make working with environment variables easier and more consistent - especially when writing functional tests.

If your app was started before November 2018, your app does not require any changes to keep working. However, if/when you are ready to take advantage of these improvements, you will need to make a few small updates.

What Changed Exactly?

But first, what changed? On a high-level, not much. Here's a summary of the most important changes:

  • A) The .env.dist file no longer exists. Its contents should be moved to your .env file (see the next point).
  • B) The .env file is now commited to your repository. It was previously ignored via the .gitignore file (the updated recipe does not ignore this file). Because this file is committed, it should contain non-sensitive, default values. Basically, the .env.dist file was moved to .env.
  • C) A .env.local file can now be created to override environment variables for your machine. This file is ignored in the new .gitignore.
  • D) When testing, your .env file is now read, making it consistent with all other environments. You can also create a .env.test file for test-environment overrides.
  • E) If you pass the --env= flag when running bin/console, this value will override your APP_ENV environment variable (if set). And so, if you pass --env=prod, the DotEnv component will try to load your .env* files.

There are a few other improvements, but these are the most important. To take advantage of these, you will need to modify a few files in your existing app.

Updating My Application

If you created your application after November 15th 2018, you don't need to make any changes! Otherwise, here is the list of changes you'll need to make - these changes can be made to any Symfony 3.4 or higher app:

  1. Create a new config/bootstrap.php file in your project. This file loads Composer's autoloader and loads all the .env files as needed (note: in an earlier recipe, this file was called src/.bootstrap.php).

  2. Update your public/index.php (index.php diff) file to load the new config/bootstrap.php file. If you've customized this file, make sure to keep those changes (but use the rest of the changes).

  3. Update your bin/console (bin/console diff) file to load the new config/bootstrap.php file.

  4. Update .gitignore:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    # .gitignore
    # ...
    
    ###> symfony/framework-bundle ###
    - /.env
    + /.env.local
    + /.env.*.local
    
    # ...
    
  5. Rename .env to .env.local and .env.dist to .env:

    1
    2
    3
    4
    5
    6
    7
    8
    9
       # Unix
       $ mv .env .env.local
       $ git mv .env.dist .env
    
       # Windows
       $ mv .env .env.local
       $ git mv .env.dist .env
    
    You can also update the `comment on the top of .env`_ to reflect the new changes.
    
  6. If you're using PHPUnit, you will also need to create a new .env.test file and update your phpunit.xml.dist file so it loads the config/bootstrap.php file.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.