Symfony
sponsored by SensioLabs
Menu
  • About
  • Documentation
  • Screencasts
  • Cloud
  • Certification
  • Community
  • Businesses
  • News
  • Download
  1. Home
  2. Documentation
  3. Components
  4. The Dotenv Component
  • Documentation
  • Book
  • Reference
  • Bundles
  • Cloud
Search by Algolia

Table of Contents

  • Installation
  • Usage

The Dotenv Component

Edit this page

Warning: You are browsing the documentation for Symfony 4.3, which is no longer maintained.

Read the updated version of this page for Symfony 6.2 (the current stable version).

The Dotenv Component

The Dotenv Component parses .env files to make environment variables stored in them accessible via $_ENV or $_SERVER.

Installation

1
$ composer require symfony/dotenv

Note

If you install this component outside of a Symfony application, you must require the vendor/autoload.php file in your code to enable the class autoloading mechanism provided by Composer. Read this article for more details.

Usage

Sensitive information and environment-dependent settings should be defined as environment variables (as recommended for twelve-factor applications). Using a .env file to store those environment variables eases development and CI management by keeping them in one "standard" place and agnostic of the technology stack you are using (nginx vs PHP built-in server for instance).

Note

PHP has a lot of different implementations of this "pattern". This implementation's goal is to replicate what source .env would do. It tries to be as similar as possible with the standard shell's behavior (so no value validation for instance).

Load a .env file in your PHP application via Dotenv::load():

1
2
3
4
5
6
7
use Symfony\Component\Dotenv\Dotenv;

$dotenv = new Dotenv();
$dotenv->load(__DIR__.'/.env');

// You can also load several files
$dotenv->load(__DIR__.'/.env', __DIR__.'/.env.dev');

Given the following .env file content:

1
2
3
# .env
DB_USER=root
DB_PASS=pass

Access the value with $_ENV in your code:

1
2
$dbUser = $_ENV['DB_USER'];
// you can also use ``$_SERVER``

The load() method never overwrites existing environment variables. Use the overload() method if you need to overwrite them:

1
2
// ...
$dotenv->overload(__DIR__.'/.env');

As you're working with the Dotenv component you'll notice that you might want to have different files depending on the environment you're working in. Typically this happens for local development or Continuous Integration where you might want to have different files for your test and dev environments.

You can use Dotenv::loadEnv() to ease this process:

1
2
3
4
use Symfony\Component\Dotenv\Dotenv;

$dotenv = new Dotenv();
$dotenv->loadEnv(__DIR__.'/.env');

The Dotenv component will then look for the correct .env file to load in the following order whereas the files loaded later override the variables defined in previously loaded files:

  1. If .env exists, it is loaded first. In case there's no .env file but a .env.dist, this one will be loaded instead.
  2. If one of the previously mentioned files contains the APP_ENV variable, the variable is populated and used to load environment-specific files hereafter. If APP_ENV is not defined in either of the previously mentioned files, dev is assumed for APP_ENV and populated by default.
  3. If there's a .env.local representing general local environment variables it's loaded now.
  4. If there's a .env.$env.local file, this one is loaded. Otherwise, it falls back to .env.$env.

This might look complicated at first glance but it gives you the opportunity to commit multiple environment-specific files that can then be adjusted to your local environment. Given you commit .env, .env.test and .env.dev to represent different configuration settings for your environments, each of them can be adjusted by using .env.local, .env.test.local and .env.dev.local respectively.

Note

.env.local is always ignored in test environment because tests should produce the same results for everyone.

You can adjust the variable defining the environment, default environment and test environments by passing them as additional arguments to Dotenv::loadEnv() (see loadEnv() for details).

4.2

The Dotenv::loadEnv() method was introduced in Symfony 4.2.

You should never store a .env.local file in your code repository as it might contain sensitive information; create a .env file (or multiple environment-specific ones as shown above) with sensible defaults instead.

Note

Symfony Dotenv can be used in any environment of your application: development, testing, staging and even production. However, in production it's recommended to configure real environment variables to avoid the performance overhead of parsing the .env file for every request.

As a .env file is a regular shell script, you can source it in your own shell scripts:

1
source .env

Add comments by prefixing them with #:

1
2
3
# Database credentials
DB_USER=root
DB_PASS=pass # This is the secret password

Use environment variables in values by prefixing variables with $:

1
2
DB_USER=root
DB_PASS=${DB_USER}pass # Include the user as a password prefix

Note

The order is important when some env var depends on the value of other env vars. In the above example, DB_PASS must be defined after DB_USER. Moreover, if you define multiple .env files and put DB_PASS first, its value will depend on the DB_USER value defined in other files instead of the value defined in this file.

Embed commands via $() (not supported on Windows):

1
START_TIME=$(date)

Note

Note that using $() might not work depending on your shell.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
We stand with Ukraine.
Version:
Show your Sylius expertise

Show your Sylius expertise

Measure & Improve Symfony Code Performance

Measure & Improve Symfony Code Performance

↓ Our footer now uses the colors of the Ukrainian flag because Symfony stands with the people of Ukraine.

Avatar of Jason Johnstone, a Symfony contributor

Thanks Jason Johnstone for being a Symfony contributor

1 commit • 2 lines changed

View all contributors that help us make Symfony

Become a Symfony contributor

Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

Learn how to contribute

Symfony™ is a trademark of Symfony SAS. All rights reserved.

  • What is Symfony?
    • Symfony at a Glance
    • Symfony Components
    • Case Studies
    • Symfony Releases
    • Security Policy
    • Logo & Screenshots
    • Trademark & Licenses
    • symfony1 Legacy
  • Learn Symfony
    • Symfony Docs
    • Symfony Book
    • Reference
    • Bundles
    • Best Practices
    • Training
    • eLearning Platform
    • Certification
  • Screencasts
    • Learn Symfony
    • Learn PHP
    • Learn JavaScript
    • Learn Drupal
    • Learn RESTful APIs
  • Community
    • SymfonyConnect
    • Support
    • How to be Involved
    • Code of Conduct
    • Events & Meetups
    • Projects using Symfony
    • Downloads Stats
    • Contributors
    • Backers
  • Blog
    • Events & Meetups
    • A week of symfony
    • Case studies
    • Cloud
    • Community
    • Conferences
    • Diversity
    • Documentation
    • Living on the edge
    • Releases
    • Security Advisories
    • SymfonyInsight
    • Twig
    • SensioLabs
  • Services
    • SensioLabs services
    • Train developers
    • Manage your project quality
    • Improve your project performance
    • Host Symfony projects
    Deployed on
Follow Symfony
Search by Algolia