2017 © Pedro PelĂĄez
 

library laravel-conditional-providers

Load Laravel service providers based on the current environment.

image

sebastiaanluca/laravel-conditional-providers

Load Laravel service providers based on the current environment.

  • Wednesday, July 19, 2017
  • by sebastiaanluca
  • Repository
  • 1 Watchers
  • 23 Stars
  • 3,143 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 4 Forks
  • 0 Open issues
  • 5 Versions
  • 22 % Grown

The README.md

Laravel Conditional Providers

Latest stable release Software license Build status ![Total downloads][downloads-badge] Total stars, (*1)

Read my blog View my other packages and projects ![Follow @sebastiaanluca on Twitter][twitter-profile-badge] [Share this package on Twitter][link-twitter-share], (*2)

Load Laravel service providers and facades based on the current environment., (*3)

Specify the service providers and facades to load per environment directly in your configuration file. No more need to add lengthy blocks of conditionals to your AppServiceProvider, do it all in the app configuration file like you would with any service provider and facade!, (*4)

Inspired by Matt Staufer, Sven Luijten, and others., (*5)

Table of contents

What does it solve?

Say you're using a package like barryvdh/laravel-ide-helper in your project. If you've followed its installation instructions and require it only in development environments (like you should), you'd do the following:, (*6)

composer require barryvdh/laravel-ide-helper --dev

And then add the service provider to your app's config providers array:, (*7)

'providers' => [

    Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class,

]

Now when you run composer install --no-dev in your production environment to install all but development packages, this will throw an exception. Laravel will try to load the registered service provider class which it can't find, because the package is not installed., (*8)

[Symfony\Component\Debug\Exception\FatalThrowableError]
Class 'Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider' not found

Of course you can —per the instructions— conditionally load the provider manually in the register method of the app/Providers/AppServiceProvider.php file:, (*9)

public function register()
{
    if ($this->app->environment() !== 'production') {
        $this->app->register(\Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class);
    }
}

But you'd have to do this for each development package and each environment you don't want it loaded in, which is hardly maintainable and pollutes your application-specific code., (*10)

Enter Laravel conditional providers to easily do all of this in your main application config file!, (*11)

Requirements

  • PHP 7.3 or higher
  • Laravel 6.0 or higher

How to install

composer require sebastiaanluca/laravel-conditional-providers

How to use

Conditional providers

Disable auto-discovery of the package's service provider by adding it to your composer.json's relevant section:, (*12)

"extra": {
    "laravel": {
        "dont-discover": [
            "barryvdh/laravel-debugbar"
        ]
    }
},

Once you're set up, simply add a providers array per environment to your config/app.php file:, (*13)

'providers' => [

    // Contains your global providers which will load in any environment

],

'local_providers' => [

    // Contains your 'local' environment providers

    // Mostly used to load debug helpers, optimization tools, et cetera

    Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class,
    Barryvdh\Debugbar\ServiceProvider::class,

],

'production_providers' => [

    // Contains your 'production' environment providers

    // Great for when you only want to get analytics or
    // bug reports in production and disable the provider
    // entirely when developing.

],

Each providers key is optional and can be empty —so you could just use the local_providers array or none at all., (*14)

The example above will do the following in a local environment:, (*15)

  • Load every provider from providers
  • Load every provider from local_providers
  • Ignore everything in production_providers

All done! Now your app service provider is clean and you get a better view on what's loaded and when, with the added benefit of enabling or disabling packages based on environment., (*16)

Conditional aliases

In addition to conditionally loading providers, this workflow is also available for aliases/facades. Add a facades/aliases array per environment to your config/app.php file like so:, (*17)

'aliases' => [

    // Contains your global aliases which will load in any environment

],

'local_aliases' => [

    // Contains your 'local' environment aliases/facades

    'Debugbar' => Barryvdh\Debugbar\Facade::class,

],

'production_aliases' => [

    // Contains your 'production' environment aliases/facades

],

That's it! This will load the Debugbar facade only in the local environment., (*18)

License

This package operates under the MIT License (MIT). Please see LICENSE for more information., (*19)

Change log

Please see CHANGELOG for more information what has changed recently., (*20)

Testing

bash composer install composer test, (*21)

Contributing

Please see CONTRIBUTING and CONDUCT for details., (*22)

Security

If you discover any security related issues, please email hello@sebastiaanluca.com instead of using the issue tracker., (*23)

Credits

About

My name is Sebastiaan and I'm a freelance Laravel developer specializing in building custom Laravel applications. Check out my portfolio for more information, my blog for the latest tips and tricks, and my other packages to kick-start your next project., (*24)

Have a project that could use some guidance? Send me an e-mail at hello@sebastiaanluca.com!, (*25)

The Versions

19/07 2017

dev-develop

dev-develop https://github.com/sebastiaanluca/laravel-conditional-providers

Load Laravel service providers based on the current environment.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel environment service provider service providers conditional

19/07 2017

dev-master

9999999-dev https://github.com/sebastiaanluca/laravel-conditional-providers

Load Laravel service providers based on the current environment.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel environment service provider service providers conditional

19/07 2017

1.1.0

1.1.0.0 https://github.com/sebastiaanluca/laravel-conditional-providers

Load Laravel service providers based on the current environment.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel environment service provider conditional

13/07 2017

1.0.0

1.0.0.0 https://github.com/sebastiaanluca/laravel-conditional-providers

Load Laravel service providers based on the current environment.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel environment service provider conditional

24/01 2017

0.1.0

0.1.0.0 https://github.com/sebastiaanluca/laravel-conditional-providers

Load service providers based on certain conditions.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel service providers