Dotenv requires a production .env file

I use dotenv for PHP to manage environment settings (not lavarel, but I marked it because lavarel also uses dotenv)

I excluded .env from the code base, and I added the .env.example file for all other collaborators

On the github dotenv page:

phpdotenv is intended for a development environment and should not normally be used in production. In production, the actual environment variables must be set so that there is no overhead for downloading the .env file for each request. This can be achieved using an automated deployment process using tools like Vagrant, chef or Puppet, or it can be manually configured using cloud nodes such as Pagodabox and Heroku.

What I don't understand is that I get the following exception:

PHP Fatal error: Uncaught exception 'InvalidArgumentException' with message 'Dotenv: Environment file .env not found or not readable.

This contradicts what the documentation says: "The actual environment variables must be set so that there is no overhead for downloading the .env file for each request."

So the question is, is there any reason why dotenv throws this exception and / or am I missing something? First of all, the behavior is different from other dotenv libraries (ruby)

I can easily get around this, not a very pleasant solution:

 if(getenv('APPLICATION_ENV') !== 'production') { /* or staging */ $dotenv = new Dotenv\Dotenv(__DIR__); $dotenv->load(); } 

The most pleasant solution, in my opinion, but I think dotenv should handle this.

 $dotenv = new Dotenv\Dotenv(__DIR__); //Check if file exists the same way as dotenv does it //See classes DotEnv\DotEnv and DotEnv\Loader //$filePath = $dotenv->getFilePath(__DIR__); //This method is protected so extract code from method (see below) $filePath = rtrim(__DIR__, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR . '.env'; //both calls are cached so (almost) no performance loss if(is_file($filePath) && is_readable($filePath)) { $dotenv->load(); } 
+10
php environment-variables production-environment laravel-dotenv phpdotenv
source share
3 answers

Dotenv was built around the idea that it would only be used in development environments. Thus, it always expects the presence of an .env file.

A solution you didn't like is the recommended way to use Dotenv. And it seems that he will not change in the near future . Related discussion in the tracker on the project issue: https://github.com/vlucas/phpdotenv/issues/63#issuecomment-74561880

Note that Mark offers a good approach to production / intermediate boot environments that skips file downloads but does not validate

 $dotenv = new Dotenv\Dotenv(); if(getenv('APP_ENV') === 'development') { $dotenv->load(__DIR__); } $dotenv->required('OTHER_VAR'); 
+11
source share

If you are having trouble creating the variable APP_ENV, this code is simpler:

 $dotenv = new Dotenv\Dotenv(__DIR__); if(file_exists(".env")) { $dotenv->load(); } 
+3
source share

Also reviewed this, my current solution is to use the Lumen way (as of June 6, 2016), which was proposed in the discussion :

 try { (new Dotenv\Dotenv(__DIR__.'/../'))->load(); } catch (Dotenv\Exception\InvalidPathException $e) { // } 

If necessary, you can perform some additional exception handling (for example, drop to default values ​​or do some checking.

+1
source share

All Articles