Laravel Service Provider in Simple English

Laravel Service Provider in Simple English

Photo by AbsolutVision on Unsplash

Key Concepts

First thing first, let’s clarify some of the concepts.

Services are not connected to our app instance by default

But how can we bind the Services into the Container?

In general, there are 2 ways.

  1. Using the bind() method from our $app instance.
    We can explicitly call the bind() method or singleton() method on the $app instance. Typically we would call these 2 methods in /bootstrap/app.php or /app/Http/Kernel.php or in AppServiceProvider .
  2. Using Service Provider
    Alternatively, we will create a Service Provider class and register our Service in there.

Okay here is how Service Provider works

In summary, Service Provider is a class that contains the logic on how to instantiate a Service in Laravel. Once we defined a Service Provider, the service will be registered into the Service Container. If we need an instance of the Service, we can just resolve it out from the Service Container.

But why can’t we just use the ‘new’ keyword to instantiate a class?

Fair point. Here’s the thing, using Service Provider can make our life much easier. Let’s go back to our previous example, where we are building a ride-sharing app. We need 3 services:

  1. Geolocation to retrieve the geocoordinates.
  2. Map to retrieve the information about a place.
  3. Satellite to communicate with GPS and pinpoint the exact location.

class Satellite {
// ...

}

class Map{

// ...

}

class Geolocation{

public function __construct(Map $map, Satellite $satellite)
{
// ...

}


}

$map = new Map();
$satellite = new Satellite();
$geolocation = new Geolocation($map, $satellite);

Service Provider to the Rescue

As I mentioned earlier, once we created a Service Provider, the Service should be registered into the Service Container. Since we have put the instructions on how to instantiate the Geolocation class in its Service Provider, now when we resolve the Service from the container, Laravel will simply run the logic to create the Service.

The class structure of Service Provider:

To create a Service Provider, you can simply run php artisan make:provider <your-service-provider-name> . The Service Provider will appear in app/Providers .

The register() method

This is where we will tell Laravel how to instantiate our Service, and the place where we will bind the Service into the Service Container. In the example of Geolocation , we may want to do something similar to this:

Declaring the definition of how Laravel should create the Service when resolved from the Service Container

The boot() method

Laravel will call the boot() method after every single Service Provider is registered. That means we have access to all of the Services in our app inside the boot() method. There are no hard rules on what to put inside the boot() method, but generally, we will put things like Event Listener or code that requires other Services.

Registering the Service Provider

Even we have created the Service Provider, it won’t just work. We will need to register our Service Provider in the /config/app.php config file, under the ‘provider’ key.

Registering Service Provider

$geolocation = app(\App\Providers\GeolocationServiceProvider::class);
// ...