Detailed explanation of dependency injection you use in laravel framework

  • 2021-10-25 06:05:13
  • OfStack

Preface

It has been about 2 months since Laravel was developed. Writing Java directly before 1 is like finding the common ground between Java and PHP. Understanding PHP with some principles of Java will find that there are still many commonalities. Dependency injection of Java is already a very common concept, and Spring framework mainly solves this point. In laravel framework of PHP, dependency injection also appears.

Dependency injection is an important embodiment of inversion of control and object-oriented characteristics, so what is dependency in dependency injection, which can be understood by many people who develop with Java. Generally speaking, dependency is a kind of connection, the connection between variables and implementations. For an understanding of dependency injection, the portal: https://www.ofstack.com/article/143573. htm

Next, let's talk about dependency injection in Laravel.

I have heard that dependency injection is one of the characteristics of Laravel before, and 1 is looking for dependency injection and Spring.

Laravel provides a variety of dependency injection methods. First of all, we will implement the injection of constructor or method parameters. This dependency injection method is relatively simple and does not need to be configured. As long as the type of the class is written in the parameter of the method, at this time, the instance of the class will be injected into this parameter, and when we use it, we can use it directly, instead of going to the instance of new, the process of new has been done by the framework for us.

For example:


class Test
{
 // This is 1 Category  
}

class TestController extend Controller
{
 public function __contract(Test $test)
 {
 print_r($test);
 }
}

Instead of doing any assignment to the $test variable, Laravel will help us assign an instance of Test to the $test variable, which is a use of dependency injection. Our dependent Test is thus injected into the parameters. When we usually use Laravel controller to receive page parameters, it is dependency injection.

Then, one of the common interface-oriented programming methods we use in Java, Laravel framework also provides this interface-oriented programming method. This method is very simple. First of all, all our calls are interface-oriented. Let's write an interface here first:


 interface TestInterface
 {
 // Method of interface 
 }

Once the interface is defined, we will define the implementation class of the interface:


 class TestService implements TestInterface
 {
 // The method of implementation 
 }

How to complete the dependency? The implementation of Laravel is very simple, and the implementation and interface can be bound by one method. We look for the file app/Providers/AppServiceProider. php under the framework of Laravel. After opening it, it looks like this:


class AppServiceProvider extends ServiceProvider
{
 public function boot()
 {
 35 }

 public function register()
 {
 //
 }
}

Bind the register method drop interface to the implementation, and add:


 $this->app->bind(
 'Interface\TestInterface',
 'Service\TestService'
 );

In this way, in the parameters of the method, as long as the interface is written, the dependency can be injected.

Summarize


Related articles: