What is the relationship between "service provider" and "service container" in Laravel? What is the relationship between "service provider" and "service container" in Laravel?
Reply content:
What is the relationship between "service provider" and "service container" in Laravel?
Container is the carrier of all services. provider is used to provide methods for registering services to container.
This is actually a relationship between the three.
I have been watching this for the past two days, just like my understanding as a cainiao;
Laravel uses php to provide services.Dependency Injection, The implementation of dependency injection is usedReflection mechanism.
1. First put the required class B instantiated object into an anonymous function, and then put the class and anonymous function into the container, assuming the container is an array
2. Assume that the execution of Class A depends on Class B. In the constructor parameters of Class A, the type of the specified parameter is B.
3. When Class A is executed and Class A depends on Class B through reflection, it will execute Class B's anonymous function from the container, get an instantiated object, and pass it to class A for further execution.
The service provider is used to register and store the classes required in the future to the container.
The service container is used to store the required classes. When it is used somewhere, it is obtained from the container.
These functions reduce the dependencies between modules. When used, it is automatically added.
Reference: https://www.insp.top/article/learn-laravel-container