What's the Symfony Dotenv Component
Symfony Dotenv parses .env files to make environment variables stored in them accessible via $_SERVER or $_ENV.
https://symfony.com/components/Dotenv
Why?
A dotenv file allows you to remove hardcoded credentials or config from your code. For an extensive explanation on why this is a good thing, check out the Config chapter of the Twelve-Factor App website.
Requirements
- The 1.1.x versions require a minimum of PHP 7.3 and Drupal 9.
- The 1.2.x versions require a minimum of PHP 8.0 and Drupal 10.
Configuration
Automatic
If your project has Drush installed, you can use the drush dotenv:init command to automatically set up .env in your project,
- creating a .env file with
APP_ENVand your database credentials from settings.php - creating a .env.example file with all keys from the .env file, without files
- adding the .env file to .gitignore
- copying the load.environment.php file to your project and setting it to autoload using Composer
Manual
Copy the load.environment.php file to your project root and add it to the Composer autoload config. Afterwards, run composer update --lock to update the hash in composer.lock and composer dump-autoload to recreate the autoload files.
Add a .env file in the root of your project. It has to at least contain the APP_ENV environment variable:
APP_ENV=prod
How does it work?
You can now add environment variables to your .env file and it will automatically be available in the $_ENV global var.
You can use it in settings.php, in service providers or in other places throughout your code. Some examples:
<?php
// settings.php
$databases['default']['default'] = [
'database' => $_ENV['DB_DATABASE'],
'username' => $_ENV['DB_USERNAME'],
'password' => $_ENV['DB_PASSWORD'] ?? '',
'prefix' => '',
'host' => $_ENV['DB_HOST'] ?? 'localhost',
'port' => $_ENV['DB_PORT'] ?? 3306,
'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
'driver' => 'mysql',
];
$config['mandrill.settings'] = [
'mandrill_api_key' => $_ENV['MANDRILL_API_KEY'],
];
<?php
namespace Drupal\yourmodule;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\DependencyInjection\ServiceProviderInterface;
class YourmoduleServiceProvider implements ServiceProviderInterface
{
public function register(ContainerBuilder $container)
{
$container->setParameter('yourmodule.some_secret', $_ENV['SOME_SECRET']);
}
}
On live environments, you should invoke drush dotenv:dump every time your .env file changes. If you don't, the .env file will be loaded at every request, which will decrease the performance of your application.
You can use the drush dotenv:dump command to get debugging info about the scanned dotenv files and the loaded variables.
Read the Symfony documentation for more information.
Usage with Drush
Drush has the ability to read options from environment variables.
Project information
- Project categories: Developer tools, Performance
2,382 sites report using this module
- Created by robin.houtevelts on , updated
Stable releases for this project are covered by the security advisory policy.
Look for the shield icon below.

