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

gitwithravish's avatar

Service Container - Real World Example

What is the real world use of a service container and service provider? All the tutors talk on the video is how to make it work and how to bind values from a static location. Can anyone explain a real world example of this concept in such a way that even average skilled developers can understand it?

0 likes
3 replies
Talinon's avatar

@ravish I remember struggling wrapping my mind around this at first, too. There is a lot going on, perceivably by magic. Although, once you understand it, it's quite simple.

I just read every blog/video I could find on it.. then went back to the manual and read it until it made sense. Playing around with a pet project also helped to hammer home the concepts.

Here are some resources that may help:

https://m.dotdev.co/understanding-laravel-service-container-bd488ca05280

https://stackoverflow.com/questions/37038830/what-is-the-concept-of-service-container-in-laravel

https://laracasts.com/series/laravel-6-from-scratch/episodes/38

https://laracasts.com/series/laravel-6-from-scratch/episodes/39

Then go back and read this entire manual, and hopefully it will make more sense:

https://laravel.com/docs/7.x/container

2 likes
martinbean's avatar

@ravish The service container is a central location for services in your application. Service providers are used to add (provide) things (services) to the container.

The service container is good for binding concrete implementations to interfaces. Then, in your application, you can request things by interface and get an implementation. The benefit of this is, you‘re writing “SOLID” code if you’re coding to interfaces. Alternative implementations can be bound if needs be, and your application will still work so long as the new implementation adheres to the same interface. This is handy for testing, where you can swap implementations of things for mocks or dummy implementations.

Another use is to bind already-configured services to the service container. I blogged about that here: https://martinbean.dev/blog/2017/11/27/binding-configured-services-to-laravels-container/

Please or to participate in this conversation.