DEV Community

Cover image for Laravel Service Container and Service Providers Explained

Laravel Service Container and Service Providers Explained

Farhan Hasin Chowdhury on May 23, 2021

Laravel's service container is one of the most important pieces of the framework yet it gets so little attention from a lot of developers. Being in...
Collapse
 
bawa_geek profile image
Lakh Bawa

Thanks, you made it super easy

Collapse
 
fhsinchy profile image
Farhan Hasin Chowdhury

Thanks a lot for letting me know @bawa_geek I almost thought maybe no one's understanding anything 😅

Collapse
 
bawa_geek profile image
Lakh Bawa

your custom Container class made it super clear, however I am trying to understand where we might wan tto use that app()->make(someClass); how can it help.

I use dependency injection already, but not sure how iOC container can help

Thread Thread
 
fhsinchy profile image
Farhan Hasin Chowdhury • Edited

The IoC container helps by performing the instantiation for you.

Assume one of your classes depends on another class and a bunch of other data.

Now every time you want to instantiate an object from that class, you'll have to use new and carefully pass all the parameters.

Assume that one of those dependencies changes during development, now you'll have to painstakingly find all the new statements and update them.

The IoC container allows you to bind a closure that can return an instance of that class. Now anytime you want an object, ask the IoC container. It knows how to make that object. Also if any change occurs, you'll have to change your code in only one place.

Coding with Laravel means coding with elegance. Your code should be functional and beautiful. The auto resolution feature is another example of that. If you type hint your dependencies properly, you won't even have to use app()->make(), saving you some keystrokes and making your code more readable in the process.

There are other benefits regarding testability and stuff but I'll talk about those another day hopefully. Let me know if you've further questions @bawa_geek

Collapse
 
jcarlosr profile image
Juan Ramos • Edited

Thank you for writing this. It is very useful.
I just have one question.

When you rewrite the example using the Laravel Service Container, why do you use the string 'App\Interfaces\SocialMediaService' instead of SocialMediaServiceInterface::class?

Collapse
 
fhsinchy profile image
Farhan Hasin Chowdhury

No particular reasons to be honest. I didn't even noticed until you told me.

Collapse
 
saaberdev profile image
Mahfuzur Rahman Saber

How do I retrieve right service based on request parameter ?

Lets say i have a share button and it has sub buttons such as Facebook, LinkedIn and et cetera.

if param is fb it will use fbservice and if param is linkd it will use linkedin service ??

I used it in this manner but i end up with dependency injection resolve error when i try to return back to my site which is a confirmation page within the same controller.

Collapse
 
tutanramon profile image
TutanRamon

I am wondering the same thing. And what if somebody clicks on a "share on all social media" ?

Collapse
 
fhsinchy profile image
Farhan Hasin Chowdhury

@saaberdev and @tutanramon Contextual Binding may come in handy in situations when you want to retrieve instances based on a condition - laravel.com/docs/9.x/container#con...

Collapse
 
legend_arome profile image
Onoja Abraham

Very informative and expository. Thank you

Collapse
 
chinyeins profile image
Chinyeins

Very nice, thanks for the brief summary 💪🏼

Collapse
 
fpolli profile image
fpolli

Thank you! Finally, a post that actually explains how to USE them in an app rather than just showing me again how to register one. Your explanation and example have allowed me to begin to understand this concept.

Collapse
 
aryangomes profile image
Aryan Gomes

Great explanation! 😃

Collapse
 
alamriku profile image
alamriku

isn't laravel Service Container Uses PHP Reflection API?
It would be best if a little glimpse of Reflection API use is added to this blog.
Good read.

Collapse
 
zafrin_kaisar07 profile image
Zafrin Kaisar Orin

thanks for making the idea clear