2Hats . 2 minutes
August 19, 2019

How the service container helps you to architect your Laravel projects better

Understanding how the service container works is critical in architecting scalable decoupled code using Laravel. In this article, we will explore how the service container helps us to achieve this using some practical examples.

Suppose that you have a reports page where you export users in the system in as an XML. You have a folder AppServicesUserExport where you have a UserExportInterface.

This is how the XmlUserExport class looks like:

This is how the controller looks like

 

 

Note 1: UserExportController is creating the object.

After a while, you are asked to change it to a csv export. So now you create this new CsvUserExport class

You now have to edit the Controller to use this class. This means that the Controler and the implementations are tightly coupled. How can we overcome this shortcoming?

We can bind the interface to implementation to an interface using the Service Container (https://laravel.com/docs/5.8/container#binding-interfaces-to-implementations). Add this to the ProvidersAppServiceProder.

Then update our controller like this

 

Note 2: The controller is no longer responsible for creating the export class object. It’s the service container that does it.

Whenever we want to switch an implementation, we just need to update the binding in the AppServiceProvider. So if you want to switch to a json export in future, you create the JsonUserExport class and update the AppServiceProvider like this,

Now we are adhering to the Open-Closed Principle and Dependency Inversion Principle in the SOLID principles.

But what if you want XmlUserExport on UserExportController and CsvUserExport on ReportController?
This is when we use contextual binding (https://laravel.com/docs/5.8/container#contextual-binding)

Add this to the AppServiceProvider. Ideally, we should be creating a new Service Provider, but it’s ok for our small classes.

Now the same interface on both controllers will return the implementations defined in the Service Provider.

The core idea here is the Inversion Of Control (IoC). Previously (Note 1), the  UserExportController is controlling the creation of the XmlUserExport. But in Note 2 and Note 3, we see that the Service Container is controlling the object creation. This way, the controller and the export classes are loosely coupled.

blog
Greetings! I'm Aneesh Sreedharan, CEO of 2Hats Logic Solutions. At 2Hats Logic Solutions, we are dedicated to providing technical expertise and resolving your concerns in the world of technology. Our blog page serves as a resource where we share insights and experiences, offering valuable perspectives on your queries.
Aneesh ceo
Aneesh Sreedharan
Founder & CEO, 2Hats Logic Solutions
Subscribe to our Newsletter
Arsha Content writer

    Stay In The Loop!

    Subscribe to our newsletter and learn about the latest digital trends.