Service Container vs Service Provider in Laravel

Kishan Rank
2 min readOct 12, 2022

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. And Laravel is one of the most popular framework based on PHP language.

Laravel provides lots of pre build features that user can use easily, Today in this article I’ll explain main difference between Service Container and Service Provider.

As per official definition from Laravel,

  • Service Container is a powerful tool for managing class dependencies and performing dependency injection.
  • 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.

Now let’s understand both in detail,

  1. Service Container:

Service container is the place where your services are registered.

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

It has basically three methods bind(), make() and singleton() used for binding services, retrieving services and binding class as a singleton respectively.

Let’s understand this methods 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 container.

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

2. Service Provider:

Service providers provide services by adding them to the container.

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

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

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

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

Best custom use of register method is implementation of Repository pattern.

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

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

Best use case of boot() method is View Composers.

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

That’s it! If you have any other questions, please feel free to leave your thoughts in the comments below and don’t forget to clap if you liked it!

--

--

Kishan Rank

Hey there, I’m Kishan Rank, an Software developer from India. PHP lover! https://github.com/kishanrank