Laravel Dependency Injection — On My Own Words.
Tags: Programming, Software Development, Service Container, Laravel, Dependency Injection

Laravel Dependency Injection — On My Own Words. Arifin Follow Jun 27 · 3 min read

Photo by CDC on Unsplash

What I’m writing in this article is actually can be read on Laravel’s official documentation about Service Container. However, it somehow takes a quite long time for me to understand what’s presented in there. So I write this article to help myself to understand it in a somewhat simpler way — in my opinion. It works for me, and I hope it also works for you.

Dependency Injection

For example, suppose that:

your code needs an object from class HelpSpotService

to make an object from class HelpSpotService , you need to pass an object of class HelpSpot\API in it’s constructor.

, you need to pass an object of class in it’s constructor. to make an object from class HelpSpot\API , you need to pass an object of class HttpClient in it’s constructor.

Normally, without dependency injection technique, you will do something like this:

function yourMethod()

{

$httpClient = new HttpClient(["timeout" => 50]);

$helpSpotAPI = new HelpSpot\API($httpClient);

$helpSpotService = new HelpSpotService($helpSpotAPI); // do something with helpSpotService

$helpSpotService->doSomething();

}

If you happen to use object of class HelpSpotService in many places, then it will give you many line of codes that’s actually not part of your main application logic. And sometimes this can be overwhelming when the dependency chain is long. This is where dependency injection comes into play.

With dependency injection, instead of start with making an instance of HttpClient only to eventually create instance of HelpSpotService , you will simply say “I need an instance of class HelpSpotService , please make it one for me” to the framework, like the following.

function yourMethod()

{

$helpSpotService = $app->make('HelpSpotService'); $helpSpotService->doSomething();

}

Then Laravel will take care the object creational for you automagically.

You may ask, how can that happen? How do laravel knows how to create all the dependency that’s needed to create an object of class HelpSpotService ? Well, Laravel only knows if you define it. We describe how the dependency is resolved in Service Container.

Service Container

We define how dependencies are resolved in our Service Container by ‘register’-ing each dependency. Take a look at the following code, here is a snippet from Laravel’s documentation:

$this->app->bind('HelpSpot\API', function ($app) {

return new HelpSpot\API($app->make('HttpClient'));

});

You can write that code inside register method in app/Providers/AppServiceProvider.php . Now, wherever you need a HelpSpot\API object in your code, you can get it by simply type:

$api = $app->make('HelpSpot\API');

See that you don’t need to bother about making an HttpClient to make the object, Laravel handles it for you.

Now you maybe wonder how is that HttpClient are resolved? It’s just the same process that we make the HelpSpot\API , there should be code that defines how HttpClient are resolved. For example, the code may look like this:

$this->app->bind('HttpClient', function ($app) {

return new HttpClient(["timeout" => 50]);

});

So to make an object of HelpSpot\API , Laravel will automatically look for how it resolves and found out that it needs an object of HttpClient to be resolved first. Laravel then repeating the same process to look for how the HttpClient is resolved, returns the HttpClient object into the HelpSpot\API object creation, which eventually creating an object of HelpSpot\API and returns it to your code.

Back to the first example. In order to have a dependency injection for HelpSpotService , you need to define how HelpSpotService are resolved like the following.

$this->app->bind('HelpSpotService', function ($app) {

return new HelpSpotService($app->make('HelpSpot\API'));

});

That’s it. I hope that makes sense and now you get the sense on how it works.