January 13, 2017 Javier Eguiluz

A common practice when developing applications is to store some configuration options as environment variables in a .env file (pronounced "dot-env"). You can already use this technique in Symfony applications, but in Symfony 3.3 we've decided to make it a built-in feature thanks to the new Dotenv component.

In practice, the Dotenv component parses .env files to make environment variables stored in them accessible in your application via getenv() , $_ENV or $_SERVER . If your .env file contains these variables:

1 2 DB_USER=root DB_PASS=pass

The following code will parse them and turn them into environment variables:

1 2 3 use Symfony\Component\Dotenv\Dotenv ; ( new Dotenv ()) -> load ( __DIR__ . '/.env' );

Now you can get the database password in your application as follows:

1 $dbPassword = getenv ( 'DB_PASS' );

In addition to loading the variables, you can just parse them because the component defines three stages: load, parse, and populate.

Before creating a new component, we reviewed the existing libraries that provide similar features, but none of them matched our specific set of requirements: