Amit Merchant

Amit Merchant

A blog on PHP, JavaScript, and more

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

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.

Learn the fundamentals of PHP 8 (including 8.1 and 8.2), the latest version of PHP, and how to use it today with my new book PHP 8 in a Nutshell. It's a no-fluff and easy-to-digest guide to the latest features and nitty-gritty details of PHP 8. So, if you're looking for a quick and easy way to PHP 8, this is the book for you.

👋 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!

Comments?