Get "PHP 8 in a Nuthshell" (Now comes with PHP 8.3)
Amit Merchant

Amit Merchant

A blog on PHP, JavaScript, and more

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

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, 8.2, and 8.3), 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.

Like this article? Consider leaving a

Tip

👋 Hi there! I'm Amit. I write articles about all things web development. You can become a sponsor on my blog to help me continue my writing journey and get your brand in front of thousands of eyes.

Comments?