How to create .env files for dotenv (in Laravel 5)

I just started using Laravel 5, which uses the dotenv library. This uses the .env file in the root of the project, which sets up the environment using this line:

APP_ENV=local 

In accordance with everything that I read on this subject, all other environment-specific configurations should be placed in this file, therefore database passwords, URLs, etc., which are then read into the main configuration array as follows:

 env('DB_HOST', 'localhost') 

Although I feel this may work for a few specific things, such as database passwords, which you might not want to commit, I really want to be able to capture most or all of my different environment values ​​for each environment.

So I want .env to define APP_ENV as "local", "staging" or "production" and then have a .local.env or .env.local file containing values ​​that I can then commit and the correct file will be loaded based on APP_ENV.

Is it possible? Laravel 4 had cascading configuration arrays that seemed a lot more flexible, but if I can have an .env file for the environment, then I can live with it.

+5
source share
3 answers

In the end, he solved this by changing the application /Providers/ConfigServiceProvider.php. This file is added as a stub to the application folder when creating the project and is designed to override configuration values.

Now it processes cascading configs, so any values ​​in config / local / app.php, for example, will override config / app.php. The comment below says that it does not process the corresponding arrays in the environment configuration and will simply be replaced. But I can solve it when I need it.

 <?php namespace App\Providers; use Illuminate\Support\ServiceProvider; use Symfony\Component\Finder\Finder; class ConfigServiceProvider extends ServiceProvider { /** * Overwrite any vendor / package configuration. * * This service provider is intended to provide a convenient location for you * to overwrite any "vendor" or package configuration that you may want to * modify before the application handles the incoming request / command. * * Modified 2014-01-20 to allow environment specific configs to be loaded * from app/config/[environment]/ which will cascade over the base configs. * * @return void */ public function register() { $config = app('config'); $envPath = app()->configPath() . '/' . getenv('APP_ENV'); foreach (Finder::create()->files()->name('*.php')->in($envPath) as $file) { $configName = basename($file->getRealPath(), '.php'); $oldConfigValues = $config->get($configName); $newConfigValues = require $file->getRealPath(); // Replace any matching values in the old config with the new ones. // Doesn't yet handle matching arrays in the config, it will just replace them. $config->set($configName, $newConfigValues + $oldConfigValues); } } } 
+7
source

You do not need to use .env for everything. There are several options.

Option 1 - Use Only .env for Variable

 'default' => env('DB_CONNECTION'), 

Option 2 - use only .env for the variable, but by default for the system if it does not exist

 'default' => env('DB_CONNECTION', 'mysql'), 

Option 3 - just copy your variable and don't set it with .env

 'default' => 'mysql', 

Option 2 is probably best for most configuration options. You still define (and commit) your configuration option in your git repository, but you can easily override it in any .env file in the future if you want.

Option 1 is best for passwords, application keys, etc. - so they are never tied to your git repository.

Option 3 for several configuration variables that, as you know, will never be changed.

Note. Cascading the Laravel 4 configuration folder is no longer available.

+5
source

Easily customize your Laravel 5 environment.

  • Open the root folder of the application and find ".env.example",
  • Copy and rename to ".env",
  • Please put the ".env" file in your environment,
  • If you use GIT, make sure that you do not push this file to the GIT repository.

For a "full explanation" I am writing this configuration here .

I quote the dotenv developer,

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

+3
source

Source: https://habr.com/ru/post/1211503/


All Articles