Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

hristodinev's avatar

The Service container and service Providers explained

Hello everyone!

I have trouble understanding what is the difference between Service Container and Service Provider. I know that Service container is just a class where we can register different classes or dependencies. Well in that case what is the role of the service providers? Why do we need them? Sorry if my question is already asked by someone else.

0 likes
5 replies
tykus's avatar

ServiceProviders bootstrap specific parts (packages) in the application - most packages (including Laravel's core packages) need to run some code to bootstrap themselves in order to prepare them to be used by the application.

2 likes
hristodinev's avatar

@tykus If I understand correctly the Service provider register the dependencies and the Service Container is the place where all the depencies work together as a whole application.

3 likes
skliche's avatar

The service container is used for dependency injection (e.g. parameter injection in your controller classes), and binding and resolving classes (like what class to instantiate for a given contract).

Service providers are used to configure your application. Yes, service container bindings are registered there but also other important things like routes, middleware, and event listeners. The service providers are executed before the request itself is handled so everything you need is put in place and configured. Some service providers are executed every time, some of them only when needed ('deferred providers').

6 likes
martinbean's avatar

@hristodinev There are Laracasts lessons that covers both the service container and service providers:

In essence, they do exactly what they say on the tin.

The service container is where your services are registered. Service providers provide services by adding them to the container.

So you might have service providers for third-party services like Facebook, Stripe, Nexmo etc that configure the SDKs for those services with API keys and whatnot.

5 likes
hristodinev's avatar

Thank you for the replies. Slowly I start to understand these concepts.

Please or to participate in this conversation.