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...
For further actions, you may consider blocking this person and/or reporting abuse
Thanks, you made it super easy
Thanks a lot for letting me know @bawa_geek I almost thought maybe no one's understanding anything 😅
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
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
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 ofSocialMediaServiceInterface::class
?No particular reasons to be honest. I didn't even noticed until you told me.
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.
I am wondering the same thing. And what if somebody clicks on a "share on all social media" ?
@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...
Very informative and expository. Thank you
Very nice, thanks for the brief summary 💪🏼
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.
Great explanation! 😃
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.
thanks for making the idea clear