Environment specific Service Providers and Facades in Laravel
I often work with 3rd party app in my Laravel Projects, some of them are for example barryvdh/laravel-debugbar, barryvdh/laravel-translation-manager or aaronlord/laroute.
Now I show you how you can load only specific facades an service providers for development.
Let’s build this up.
In Laravel there is a .env file which has a variable called `APP_ENV`.
That’s exactly what we need. If the value is ‘local’ we are in development if its ‘testing’ or ‘production’ we are naturally not (More about environments in Laravel).
Create a new ServiceProvider called `LocalEnvironmentServiceProvider` with artisan:
php artisan make:provider LocalEnvironmentServiceProvider
Then extend it like the following example (change the namespace for your app):
Notice: The service provider use the following syntax to check if we are in development.
if (App::environment('local')) { // .. }
Now you just have to add this Service Provider in your app.php under providers and…
'providers' => [ // .. /* * Dev Only Providers: */ App\Providers\LocalEnvironmentServiceProvider::class, // .. ],
…thats it! Now if you want to use a serivce provider or a facade alias only in dev add it to the LocalEnvironmentServiceProvider.
Leave a Reply