Laravel 5 – Load ServiceProvider depend on .env file

Łukasz Piotrowski Comments 0

    We have many situations when We need register some ServiceProviders only on development version depends on .env file
    Ofcourse You can add entry to config/app.php. But on situation when You don't have Provider on production env it throws errors.
    This kind of situation can ocures after adding entry to require-dev sectioncomposer.json file.

    The solution is very simple and elegant. It saves our time and many trouble.


    composer.json

    For testing purpose add package to composer.json to section require-dev:

    "barryvdh/laravel-debugbar": "2.0.*@dev"

    then do composer update

    Setup ServiceProvider

    Setup new ServiceProvider which handle our conditional registering:

    php artisan make:provider LocalEnviromentServiceProvider

    Add entry to config/app.php

    'App\Providers\LocalEnviromentServiceProvider',

    Register own local Service Providers

    Create property to store entries:

        /**
         * List of Local Enviroment Providers
         * @var array
         */
        protected $localProviders = [
            'Barryvdh\Debugbar\ServiceProvider',
        ];

    and method:

        /**
         * Loda local service providers
         */
        protected function registerServiceProviders()
        {
            foreach ($this->localProviders as $provider)
            {
                $this->app->register($provider);
            }
        }

    Finally run it on boot() method:

        /**
         * Bootstrap the application services.
         * @return void
         */
        public function boot()
        {
            if ($this->app->isLocal())
            {
                $this->registerServiceProviders();
            }
        }

    Other enviroments

    If you like to load Providers on different enviroments change:

    $this->app->isLocal()

    to

    $this->app['env'] == 'local'
    // Or
    $this->app->environment() == 'local'

    After that You can change local to other eg. testing.

    Register Facade Aliases

    Last step We handle is registering Facade Aliases. It's little more complicated but not impossible.

    Create property:

        /**
         * List of only Local Enviroment Facade Aliases
         * @var array
         */
        protected $facadeAliases = [
            'Debugbar' => 'Barryvdh\Debugbar\Facade',
        ];

    Method:

        /**
         * Load additional Aliases
         * Base file Alias load is /config/app.php => aliases
         */
        public function registerFacadeAliases()
        {
            $loader = AliasLoader::getInstance();
            foreach ($this->facadeAliases as $alias => $facade)
            {
                $loader->alias($alias, $facade);
            }
        }

    and run it on our boot() method adding:

    $this->registerFacadeAliases();

    Final ServiceProvider code

    <?php namespace App\Providers;
    
    use Illuminate\Foundation\AliasLoader;
    use Illuminate\Support\ServiceProvider;
    
    class LocalEnviromentServiceProvider extends ServiceProvider
    {
    
        /**
         * List of Local Enviroment Providers
         * @var array
         */
        protected $localProviders = [
            'Barryvdh\Debugbar\ServiceProvider',
        ];
    
        /**
         * List of only Local Enviroment Facade Aliases
         * @var array
         */
        protected $facadeAliases = [
            'Debugbar' => 'Barryvdh\Debugbar\Facade',
        ];
    
        /**
         * Bootstrap the application services.
         * @return void
         */
        public function boot()
        {
            if ($this->app->isLocal())
            {
                $this->registerServiceProviders();
    
                $this->registerFacadeAliases();
            }
        }
    
        /**
         * Register the application services.
         * @return void
         */
        public function register()
        {
        }
    
        /**
         * Loda local service providers
         */
        protected function registerServiceProviders()
        {
            foreach ($this->localProviders as $provider)
            {
                $this->app->register($provider);
            }
        }
    
        /**
         * Load additional Aliases
         * Base file Alias load is /config/app.php => aliases
         */
        public function registerFacadeAliases()
        {
            $loader = AliasLoader::getInstance();
            foreach ($this->facadeAliases as $alias => $facade)
            {
                $loader->alias($alias, $facade);
            }
        }
    }
    Top