2017 © Pedro Peláez
 

package laravel-cascading-config

Multiple cascading environmental configuration files support for Laravel 5

image

machaven/laravel-cascading-config

Multiple cascading environmental configuration files support for Laravel 5

  • Friday, November 24, 2017
  • by machaven
  • Repository
  • 1 Watchers
  • 0 Stars
  • 9 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Laravel 5 Cascading Config Files

Why? - The .env problem

When continously developing and extending a laravel application, you may find that your .env file becomes large and hard to maintain. When multiple branches with .env changes are deployed, it becomes a nightmare to consolidate all the .env changes and update the live .env file., (*1)

This forced me to rethink configuration and what is truly important inside an .env file., (*2)

What to keep in your .env file, (*3)

  • Standard laravel .env settings
  • Sensitive information, like login credentials for other apis.

What to take out of your .env file, (*4)

  • All custom settings that might seldom change.

Why use cascading config files?

  1. Cascading configuration files allow you to keep a set of config files for every environment. All environment configs override the base configs in the config folder. This allows you to setup a dynamic config for each environment., (*5)

  2. Settings changes can be recorded on pull requests. This prevents settings changes from being lost when only changed on the server .env file., (*6)

Furthermore if you use the env() function in your config files, it will allow you to override any defaults in the config file with variables from the .env file if they exist., (*7)

Install

Install the package through composer, (*8)

composer require machaven/laravel-cascading-config, (*9)

After installing, add the ServiceProvider to the providers array in app/config/app.php (not required for laravel 5.5+), (*10)

Machaven\LaravelCascadingConfig\CascadingConfigProvider::class,, (*11)

How this works

The service provider will check if the directory exists for the environment configured in your .env file. If the environment folder exists, then all configuration files will be read from it and merged over the configurations from the config folder., (*12)

Example for cascading config files

Firstly, create your default config file in the standard laravel config/ folder., (*13)

config/example.php, (*14)

Now create a folder to override the example config for an environment. A folder can be created for any environment defined in your APP_ENV. In this example, we use local:, (*15)

config/local/, (*16)

Now create another example config in the local folder:, (*17)

config/local/example.php, (*18)

Example config file contents

config/example.php, (*19)

<?php

return [
    'clientApi' => [
        'curlTimeout' => env('CLIENT_API_CURL_TIMEOUT', 5),
        'baseUrl' => env('CLIENT_API_BASE_URL', 'http://test.com/api/'),
        'username' => env('CLIENT_API_USERNAME'), // Always read username and password from .env
        'password' => env('CLIENT_API_PASSWORD'),
    ],
    'test' => env('TEST', 'prod'),
];

config/local/example.php, (*20)

<?php

return [
    'clientApi' => [
        'baseUrl' => env('CLIENT_API_BASE_URL', 'http://test.local/api/'),
    ],
    'test' => env('TEST', 'local'),
];

In the local configuration above, we are not overriding the curlTimeout array key., (*21)

Example config results

When APP_ENV is local:, (*22)

>>> config('example');
=> [
     "clientApi" => [
       "curlTimeout" => 5, // This is merged from the config/example.php config file
       "baseUrl" => "http://test.local/api/",
       "username" => "username in .env file",
       "password" => "password in .env file",
     ],
     "test" => "local",
   ]

When APP_ENV is prod:, (*23)

>>> config('example');
=> [
     "clientApi" => [
       "curlTimeout" => 5,
       "baseUrl" => "http://test.com/api/",
       "username" => "username in .env file",
       "password" => "password in .env file",
     ],
     "test" => "prod",
   ]

As you can see, the above file is not overridden by the one in the config/local/ folder., (*24)

When APP_ENV is prod and TEST='ENV FILE' is added to .env file:, (*25)

>>> config('example');
=> [
     "clientApi" => [
       "curlTimeout" => 5,
       "baseUrl" => "http://test.com/api/",
       "username" => "username in .env file",
       "password" => "password in .env file",
     ],
     "test" => "ENV FILE",
   ]

The test key is overridden here by the TEST variable in the .env file. This is because we are using the env() helper in our config files to override settings for any environment; if they exit in the .env., (*26)

The Versions

24/11 2017

dev-master

9999999-dev

Multiple cascading environmental configuration files support for Laravel 5

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

by David Olivier

laravel config environment cascading

24/11 2017

1.0

1.0.0.0

Multiple cascading environmental configuration files support for Laravel 5

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

by David Olivier

laravel config environment cascading