Service Providers

    Service providers are the central place of all Lumen application bootstrapping. Your own application, as well as all of Lumen'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 Lumen, you will see a call to $app->register(). You may add additional calls to this method to register as many service providers as your application requires.

    All service providers extend the Illuminate\Support\ServiceProvider class. This abstract class requires that you define at least one method on your provider: register. 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.

    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 Boot Method

    So, what if we need to register a view composer within our service provider? This should be done within the boot 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:

    1. namespace App\Providers;
    2. use Queue;
    3. use Illuminate\Support\ServiceProvider;
    4. class AppServiceProvider extends ServiceProvider
    5. {
    6. // Other Service Provider Properties...
    7. * Bootstrap any application services.
    8. * @return void
    9. */
    10. public function boot()
    11. {
    12. Queue::failing(function ($event) {
    13. });
    14. }

    All service providers are registered in the bootstrap/app.php file. This file contains a call to the $app->register() method. You may add as many calls to the register method as needed to register all of your providers.