Improvements to the Handling of .env Files for all Symfony Versions
When Symfony 4.0 was released, the
.env file was introduced as a way to set
environment variables. The core of the system has not changed. But, thanks to
recent updates to some core Symfony recipes,
.env loading has some new features
that can be enjoyed on any Symfony Flex project!
If you have an existing Symfony app (started before today), your app does not require any changes to keep working. But, 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.distfile no longer exists. Its contents should be moved to your
.envfile (see the next point).
- B) The
.envfile is now committed to your repository. It was previously ignored via the
.gitignorefile (the updated recipe does not ignore this file). Because this file is committed, it should contain non-sensitive, default values. Basically, the
.env.distfile was moved to
- C) A
.env.localfile can now be created to override environment variables for your machine. This file is ignored in the new
- D) When testing, your
.envfile is now read, making it consistent with all other environments. You can also create a
.env.testfile for test-environment overrides.
Point (D) is one of the main motivations behind these changes: it makes setting
and overriding environment variables for your
test environment a delight.
These improvements can be used in any Symfony Flex project (Symfony 3.4 or higher). Changes to the recipes (see below) make it possible to take advantage of the Symfony 4.2 feature - Define env vars per environment - in lower versions.
To take advantage of these, you will need to modify a few files in your existing app.
Updating My Application¶
Updating your existing application is optional, but just requires modifying a few files. To update, see: DotEnv: Update my Application in the Symfony Docs.
Bonus: --env & --no-debug Console Deprecations Removed¶
--no-debug were recently deprecated. However, based on
community feedback and work on these new
.env features, the deprecation
has been reverted: the
--no-debug options will continue to work!
And, the proper
.env files will be loaded. For example, want to create the
schema in your
test environment using a customized
DATABASE_URL in your
.env.test file? That will work just like you expect:
$ php bin/console doctrine:schema:create --env=test
Have any questions? Let us know!
❤️ The Symfony Community
Improvements to the Handling of .env Files for all Symfony Versions symfony.com/blog/improvements-to-the-handling-of-env-files-for-all-symfony-versionsTweet this
Comments are closed.
To ensure that comments stay relevant, they are closed for old posts.