Laravel Environment Switcher

Introduction

The basic operation of the environment switcher is to permit the use of different environment files based upon a variable that is added to the web servers configuration to control which one is loaded.

when you make changes to the environment files you need to execute

php artisan config:clear

and php artisan config:cache

The various env files are committed to source control with the exception of the .local environment file this file is used for local development and is developer specific

  1. .dev is used on MEDIDEV
  2. .qat is used on MEDIQAT
  3. .prod is used on MEDIUK

.local is reserved for local development environments

The files in use are /app/Bootstrap/DetectEnviroment.php and /app/Bootstrap/LoadEnvironment.php These are added to app/Http/Kernel.php

 protected $bootstrappers = [
        \App\Bootstrap\DetectEnvironment::class,
        \App\Bootstrap\LoadEnvironment::class,
        \Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables::class,
        \Illuminate\Foundation\Bootstrap\LoadConfiguration::class,
        \Illuminate\Foundation\Bootstrap\HandleExceptions::class,
        \Illuminate\Foundation\Bootstrap\RegisterFacades::class,
        \Illuminate\Foundation\Bootstrap\SetRequestForConsole::class,
        \Illuminate\Foundation\Bootstrap\RegisterProviders::class,
        \Illuminate\Foundation\Bootstrap\BootProviders::class,
    ];

and for console commands These are added to app/Helpers/Kernel.php

protected $bootstrappers = [
        \App\Bootstrap\DetectEnvironment::class,
        \App\Bootstrap\LoadEnvironment::class,
        //\Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables::class,
        \Illuminate\Foundation\Bootstrap\LoadConfiguration::class,
        \Illuminate\Foundation\Bootstrap\HandleExceptions::class,
        \Illuminate\Foundation\Bootstrap\RegisterFacades::class,
        \Illuminate\Foundation\Bootstrap\SetRequestForConsole::class,
        \Illuminate\Foundation\Bootstrap\RegisterProviders::class,
        \Illuminate\Foundation\Bootstrap\BootProviders::class,
    ];

This permits the console to use the relevant env file also

Apache Web Server Configuration

For the Apache Web Server a line is added to the configuration file in /www/laravel/conf/httpd.conf This line is SetEnv APPLICATION_ENV dev for example on MEDIDEV and the corresponding env file created .env.dev

Apache then needs to be restarted for the new environment file to be activated.

nginx configuration

The config file for valet etc is changed to add these variables

location ~ [^/]\.php(/|$) {
  fastcgi_split_path_info ^(.+\.php)(/.+)$;
  fastcgi_pass "unix:/Users/markgregory/.config/valet/valet.sock";
  fastcgi_index "/Users/markgregory/.composer/vendor/laravel/valet/server.php";
  include fastcgi_params;
  fastcgi_param APPLICATION_ENV "local";
  fastcgi_param APP_ENV "local";
  fastcgi_param SCRIPT_FILENAME "/Users/markgregory/.composer/vendor/laravel/valet/server.php";
  fastcgi_param PATH_INFO $fastcgi_path_info;
  }

Again nginx should be restarted with nginx -s reload or for valet valet restart

Debugging

When setting this up if the variables are not added you will receive a warning and the app will not start, for debug only you can add normal entries to .env but do not commit these to the SVM .env is only committed to show it's not in use.