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') { $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(); }
php environment-variables production-environment laravel-dotenv phpdotenv
Sander visser
source share