Check if the specific service provider is loaded or not in Laravel

Amit Merchant · July 20, 2020 ·

Sometimes, you want to load some stuff based on the fact that the certain service provider is loaded in the providers array of config/app.php or not.

'providers' => [
    // Other Service Providers

    App\Providers\ComposerServiceProvider::class,
],

For instance, if you’re using some third-party package and want to display certain things if and only if the service provider of that package is loaded.

Fortunately, there’s a helper method available in Laravel that lets you do just that. This PR adds a method isProviderLoaded() in src/Illuminate/Foundation/Application.php which lets you check if the given service provider is loaded or not like so.

if ($app->isProviderLoaded(OptionalServiceProvider::class)) {
    // Do something
}

if (! $app->isProviderLoaded(RequiredServiceProvider::class)) {
    // Throw exception
}

Really handy for the aforementioned reasons! The same method is available there in Lumen as well.

Hi there! I'm Amit. I write articles about all things web development. If you like what I write and want me to continue doing the same, I would like you buy me some coffees. I'd highly appreciate that. Cheers!