In the file config/app.php
there is an array key 'providers'. This contains the different service providers. Laravel divides here between: Package Service Providers, Application Service Providers and Framework Service Providers.
I just had to add an external package (Spatie Permission). Here I have to add the spatie PermissionServiceProvider into this array. It doesn't matter where you add it but I still wanted to know what is the diffenece between Framwork Service-, Application Service and Package Service. To finally put the Spatie\Permission\PermissionServiceProvider::class,
in the right place. I use Laravel 8.
CodePudding user response:
The categorisation of service providers mentioned is only with respect to the source of the provider. All service providers work the same way, regardless of source. Here's a breakdown of what these are:
Application service providers are your own providers typically in the
\App\Providers
namespace. The Laravel boilerplate app comes with 5 by default (listed here) and they are boilerplate in the sense that they provide basic app functionality but are meant to be extended and modified by the developer.Framework package providers are the ones that Laravel ships with. They are typically under the
Illuminate
namespace though the exact namespace may very depending on their purpose. These usually provide critical framework functionality and should not be removed or prevented from running.Package service providers are ones that originate from 3rd party packages like for example the one from laravel permission at
Spatie\Permission\PermissionServiceProvider::class
Creating providers
Application providers can be created using the command
php artisan make:provider ProviderName
This creates a new provider under app/Providers
but does not register it.
Registering service providers
Application and framework service providers (as well as some package service providers) are registered in config/app.php
under the providers
section. This allows the developer to control the order in which they run. Prior to Laravel 5.5 package service providers also needed to be registered in the config/app.php
.
These providers are used to build a provider cache which is stored under bootstrap/cache
which is what is actually read and used to bootstrap Laravel
Package auto-discovery
Since Laravel 5.5 the following command has been added to Laravel
php artisan package:discover
This command goes through the vendor folder looking for sections in the composer.json that contain something like:
"extra": {
"laravel": {
"providers": [
"PackageVendor\\PackageName\\ServiceProvider"
],
}
},
This is then used to add the additional providers to the bootstrap provider cache without the developer needing to manually add them in the config/app.php
This command is set up to run after a composer autoload-dump
in the boilerplate Laravel app which generally runs after most composer operations.
Running providers
All providers that are included in the bootstrap provider cache run in the order they are defined in by first calling register
on each of them and then calling boot
on each of them.
Deferred providers
Information on deferred providers can be found here. The short version is if your provider only provides bindings to the service container you can defer the running of the provider only when the bindings are actually required.