Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have environment specific .env files for dotenv (in Laravel 5)

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

APP_ENV=local

According to everything I've read on the subject, all other environmental specific configuration should be placed in this file, so database passwords, urls etc, which are then read into the main config array like this:

env('DB_HOST', 'localhost')

While I feel this may work for a few specific things like database passwords that you might not want committed, what I really want is to be able to commit most or all of my different environmental values for each environment.

Thus what I want is for .env to define APP_ENV as "local", "staging" or "production" and then have a .local.env or .env.local file containing the values, which I can then commit and the correct file will be loaded based on APP_ENV.

Is this possible? Laravel 4 had the cascading config arrays which seemed a lot more flexible but if I can have an environmental .env file then I can live with that.

like image 336
Gnuffo1 Avatar asked Jan 20 '15 11:01

Gnuffo1


1 Answers

Solved it in the end by modifying app/Providers/ConfigServiceProvider.php. This file is added as a stub to your app folder when you create a project and is intended for overriding config values.

It now handles the cascading configs, so that any values in config/local/app.php for example will override config/app.php. As the comment below says it doesn't handle matching arrays in the environment config and will just replace then. But I can solve that 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);
        }
    }

}
like image 125
Gnuffo1 Avatar answered Oct 09 '22 16:10

Gnuffo1