(Laravel Architecture) What is a service container?

Tram Ho

I. Service Container Concept

As the Laravel docs describe:

The Laravel service container is a powerful tool for managing class dependencies and performing dependency injection.

Translated as:

Laravel service container is a powerful tool for managing class dependencies and implementing dependency injection.

So the question pops up in my head, what is “class dependencies” and “dependency injection”?. It’s a hassle, isn’t it? So I’ll go through “class dependencies” and “dependency injection” first, understanding these concepts will make it a lot easier to understand.

II. Class dependencies

When a Class A uses some function of Class B, it is said that: Class A has a dependency relationship with Class B.

Illustrated code

We see that in the crawl() function of the Crawler class, the get() function of the Http class is used.

III. Dependency injection

Dependency injection(DI) is a programming technique bla bla … if you read it, it will be a bit confusing. We understand simply ntn. DI is a technique that helps us to implement by “injecting” a class that depends on the main class. Regarding injection, we have 3 ways:

1. Constructor Injection

Injection via constructor (function __contstructor)

2. Setter Injection

Inject via setter/gettter . function

3. Interface Injection

Inject through interface

In Php, we often encounter the first case ie Constructor Injection. So our examples will use the constructor injection implementation. Here is an example of pure php code:

As we can see to implement DI we need to do 3 things:

  1. Declare a property $http
  2. In the constructor of the Crawler class, we implement injecting the instance of the Http class into the $http . property
  3. When using Http function in Crawler class, just call $this->http->get()

Here is the code in Laravel:

IV. Back to Service Container

Now that we understand the two concepts of “class dependencies” and “dependency injection”, let’s return to Laravel! Repeat the concept mentioned at the beginning of the article.

Laravel service container is a powerful tool for managing class dependencies and implementing dependency injection.

Explanation: Laravel provides a so-called “Service container” which is very powerful. Soon by code I will show you this powerful tool of laravel. This tool was born for the purpose of implementing DI, that is, it will receive dependent classes and have a way to pull these dependent classes to use. Thus, the Service container is a place to store and deploy dependent classes. Why is it important in the design of Laravel Architecture, because this is where all the classes big and small in Laravel are managed. It is extremely important.

The actual implementation problem in the form of a scenario:

Coder : Hey Laravel, I have a few functions of this class that I want to use in Laravel, do you have a way?

Laravel Architecture : Yes, you, you throw that class into the Service container, let it manage, when you need to use it, it has a method for you to pull out and use.

Coder : Ok man, I get it. So how to implement coding?

Laravel Architecture : Here, read it, easy to understand

A little explanation:

  1. Create a service container, what you call a powerful tool
  2. Binding a class FirstClass to the service container and name it “class1”
  3. Resolve or otherwise pull out the instance of that class
  4. Using functions of “class1”

And this is the result

At this point, everyone understands what a Service container is, what it is used for and how to use it. In the next post, I will talk a little more about how to use Service containers for standards and what types of binding!

Share the news now

Source : Viblo