What are service providers
Service providers are the central place where all Laravel application bootstrapping happens. Your own application, as well as all of Laravel’s core services, are bootstrapped via service providers. By “bootstrapping” we mean registering things, including service container bindings, event listeners, middleware, and even routes. Service providers are the central place to configure your application. Laravel uses dozens of service providers internally to bootstrap its core services, such as the mailer, queue, and cache. Many of these providers are “deferred” providers, meaning they are not loaded on every request, but only when the services they provide are actually needed. All user-defined service providers are registered in thebootstrap/providers.php file.
If you’d like to learn more about how Laravel handles a request, see the request lifecycle documentation.
Writing a service provider
All service providers extend theIlluminate\Support\ServiceProvider class. Most service providers contain a register and a boot method.
To generate a new provider, use the make:provider Artisan command. Laravel will automatically register your new provider in bootstrap/providers.php.
The register method
Within theregister method, you should only bind things into the service container. You should never attempt to register any event listeners, routes, or any other piece of functionality within the register method. You may accidentally use services provided by service providers that have not yet been loaded.
$this->app property.
The bindings and singletons properties
If your service provider registers many simple bindings, you may wish to use thebindings and singletons properties instead of manually registering each binding. When the framework loads the provider, it will automatically check these properties and register their bindings.
The boot method
If you need to register a view composer within a service provider, do so within theboot method. This method is called after all other service providers have been registered, meaning you have access to all other services that have been registered by the framework.
Boot method dependency injection
You may type-hint dependencies for yourboot method as well. The service container will automatically inject any dependencies you need.
Registering service providers
All service providers are registered in thebootstrap/providers.php file. This file returns an array that contains the class names of your application’s service providers.
In Laravel 13, service providers are registered in
bootstrap/providers.php instead of the providers array in config/app.php. Using the make:provider command automatically adds them.make:provider Artisan command, Laravel automatically adds the generated provider to the file. If you created your provider class manually, add the class to the array yourself.
Creating a custom service provider
Let’s actually create a custom service provider.1
Generate the provider
Generate the service provider with an Artisan command.
2
Bind in the register method
Add bindings to the generated provider’s
register method.3
Register it in bootstrap/providers.php
If you used
make:provider, it is registered automatically. For manual providers, add it yourself.Deferred providers
If your provider is only registering bindings in the service container, you may choose to defer its registration until one of the registered bindings is actually needed. Deferring the loading of such a provider improves the performance of your application, since it is not loaded from the filesystem on every request. To create a deferred provider, implement the\Illuminate\Contracts\Support\DeferrableProvider interface and define a provides method. The provides method should return the service container bindings registered by the provider.
Next steps
Service container
Review the details of the service container and its bindings.