WARNING: You are browsing the documentation for Symfony 4.0 which is not maintained anymore. Consider upgrading your projects to Symfony 4.1.

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.

The Dotenv Component

4.0 version
Maintained Unmaintained
3.3

The Dotenv Component

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

Installation

1
$ composer require symfony/dotenv

Alternatively, you can clone the https://github.com/symfony/dotenv repository.

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 getenv() in your code:

$dbUser = getenv('DB_USER');
// you can also use ``$_ENV`` or ``$_SERVER``

Note

Symfony Dotenv never overwrites existing environment variables.

You should never store a .env file in your code repository as it might contain sensitive information; create a .env.dist file 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

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.