Service Providers

    Service providers are the central place of all Laravel application bootstrapping. Your own application, as well as all of Laravel's core services are bootstrapped via service providers.

    But, what do we mean by "bootstrapped"? In general, we mean registering things, including registering service container bindings, event listeners, middleware, and even routes. Service providers are the central place to configure your application.

    If you open the file included with Laravel, you will see a providers array. These are all of the service provider classes that will be loaded for your application. Note that many of these are "deferred" providers, meaning they will not be loaded on every request, but only when the services they provide are actually needed.

    In this overview you will learn how to write your own service providers and register them with your Laravel application.

    All service providers extend the Illuminate\Support\ServiceProvider class. Most service providers contain a register and a boot method. Within the register method, you should only bind things into the . You should never attempt to register any event listeners, routes, or any other piece of functionality within the register method.

    As mentioned previously, within the register method, you should only bind things into the . You should never attempt to register any event listeners, routes, or any other piece of functionality within the register method. Otherwise, you may accidentally use a service that is provided by a service provider which has not loaded yet.

    Let's take a look at a basic service provider. Within any of your service provider methods, you always have access to the $app property which provides access to the service container:

    1. <?php
    2. namespace App\Providers;
    3. use Illuminate\Support\ServiceProvider;
    4. class RiakServiceProvider extends ServiceProvider
    5. {
    6. /**
    7. * Register any application services.
    8. * @return void
    9. */
    10. public function register()
    11. {
    12. $this->app->singleton(Connection::class, function ($app) {
    13. return new Connection(config('riak'));
    14. });
    15. }
    16. }

    This service provider only defines a register method, and uses that method to define an implementation of Riak\Connection in the service container. If you don't understand how the service container works, check out its documentation.

    The bindings And singletons Properties

    If your service provider registers many simple bindings, you may wish to use the bindings and singletons properties instead of manually registering each container binding. When the service provider is loaded by the framework, it will automatically check for these properties and register their bindings:

    The Boot Method

    1. <?php
    2. namespace App\Providers;
    3. class ComposerServiceProvider extends ServiceProvider
    4. /**
    5. * Bootstrap any application services.
    6. *
    7. * @return void
    8. */
    9. public function boot()
    10. {
    11. view()->composer('view', function () {
    12. //
    13. });
    14. }
    15. }

    Boot Method Dependency Injection

    You may type-hint dependencies for your service provider's boot method. The will automatically inject any dependencies you need:

    All service providers are registered in the config/app.php configuration file. This file contains a providers array where you can list the class names of your service providers. By default, a set of Laravel core service providers are listed in this array. These providers bootstrap the core Laravel components, such as the mailer, queue, cache, and others.

    To register your provider, add it to the array:

    1. 'providers' => [
    2. // Other Service Providers
    3. App\Providers\ComposerServiceProvider::class,

    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 will improve the performance of your application, since it is not loaded from the filesystem on every request.

    To defer the loading of a provider, implement the \Illuminate\Contracts\Support\DeferrableProvider interface and define a method. The provides method should return the service container bindings registered by the provider: