Service Providers

Introduction

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 bootstrap/app.php 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.

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

Writing Service Providers

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 service container. You should never attempt to register any event listeners, routes, or any other piece of functionality within the register method.

The Register Method

As mentioned previously, within the register 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. Otherwise, you may accidently use a service that is provided by a service provider which has not loaded yet.

Now, let's take a look at a basic service provider:

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

This service provider only defines a register method, and uses that method to define an implementation of Riak\Contracts\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. <?php
  2. namespace App\Providers;
  3. use Illuminate\Support\ServiceProvider;
  4. class EventServiceProvider extends ServiceProvider
  5. {
  6. /**
  7. * Perform post-registration booting of services.
  8. *
  9. * @return void
  10. */
  11. public function boot()
  12. {
  13. view()->composer('view', function () {
  14. //
  15. });
  16. }
  17. /**
  18. * Register bindings in the container.
  19. *
  20. * @return void
  21. */
  22. public function register()
  23. {
  24. //
  25. }
  26. }

Registering Providers

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.