Service Container vs Service Provider in Laravel

Hello artisan, PHP is the most used server-side programming language on the web. In fact, 75 to 77% of all websites rely on PHP to some degree. Laravel is one of the most popular frameworks based on PHP.

Laravel provides many pre-built features that users can use easily. In this article, I’ll explain the main difference between a Service Container and Service Provider.

Server Container vs Service Provider

As per the official definition from Laravel,

  • Service Container is a powerful tool for managing class dependencies and performing dependency injection.
  • Service Providers are the central place for all Laravel Application bootstrapping. Your own application, as well as all of Laravel’s core services, are bootstrapped via service providers.

Now let’s understand both in detail,

  1. Service Container:

The service container is the place where your services are registered.

Laravel comes with a powerful IoC container, known as the service container. In the Laravel application, the app instance is the container. The app() helper also returns an instance of the container.

It has three methods: bind(), make(), and singleton(), which are used for binding servicesretrieving services, and binding classes as singletons, respectively.

Let’s understand this method by example:

app()->bind('App\Interfaces\NewPostMailService', function() {
    return new App\Services\NewPostMailService();
});
dd(app()->make('App\Interfaces\NewPostMailService'), app()->make('App\Interfaces\NewPostMailService'));
==> Above line will return you two different instances.
--------------------------------------------------------------------
app()->singleton('App\Interfaces\NewPostMailService', function() {
    return new App\Services\NewPostMailService();
});
dd(app()->make('App\Interfaces\NewPostMailService'), app()->make('App\Interfaces\NewPostMailService'));
==> Above line will return you same instance because it is registered as singleton.

You can bind any custom services you want in a container.

Now where will you put all of these method calls or bindings? The solution is the Service Provider. Let’s understand what a is Service Provider.


2. Service Provider:

Service providers provide services by adding them to the container.

In Laravel Service Providers are located in the app/Providers Directory.

By default every Laravel project comes with 5 Service Providers, out of them, AppServiceProvider is an empty class where you can register your binding in the register() method.

Each Service Provider has 2 default methods register() and boot().

register() method is used for registering new services to the application.

The best custom use of the register method is the implementation of the Repository pattern.

public function register() 
{
    $this->app->bind(OrderRepositoryInterface::class,  OrderRepository::class);
}

boot() method is used for the bootstrapping of the registered service, This method is called after all other service providers have been registered.

The best use case of the boot() method is View Composers.

public function boot()
{
     View::composer('view', function () {
        // Add data here
     });
}