What is laravel container? What is the Laravel service container?
In the Laravel documentation, there is an introduction to the Laravel service container:
The Laravel service container is a tool used to manage class dependencies and perform dependency injection. The fancy term dependency injection essentially means that a class's dependencies are "injected" into the class through the constructor or, in some cases, the "setter" method.
To highlight the key points, "Laravel Service Container" is a tool used to manage class dependencies and perform dependency injection.
Through the relevant explanations in the previous section "Basic Concepts of Dependency Injection", it is not difficult for us to draw such a simple conclusion that "Laravel Service Container" is a "Dependency Injection Container".
In fact, the service container as a "dependency injection container" to complete the registration, binding and parsing of the dependencies required by Laravel is only one of the core functions of the "Laravel service container"; in addition, the "Laravel service container" also plays the role of Features of the registration process for Laravel applications.
Excerpt from the article "Deep Digging into the Laravel Life Cycle" about the service container:
Creating an application instance means instantiating the Illuminate\Foundation\Application service container. We will later call it APP container. When creating an APP container, you will mainly complete the registration work of basic services such as registering the basic path of the application and binding the path to the APP container, registering the basic service provider to the APP container, and registering the core container alias to the APP container.
So to understand the Larvel service container, you must study the constructor of Illuminate\Foundation\Application:
/** * Create a new Illuminate application instance. * * @see https://github.com/laravel/framework/blob/5.6/src/Illuminate/Foundation/Application.php#L162:27 * @param string|null $basePath * @return void */ public function __construct($basePath = null) { if ($basePath) { $this->setBasePath($basePath); } $this->registerBaseBindings(); $this->registerBaseServiceProviders(); $this->registerCoreContainerAliases(); }
Yes, the constructor of the Application class completes a total of 3 operations:
Register the "App instance (i.e. Laravel service container)" itself to the "Laravel service container" through the registerBaseBindings() method;
Register the basic service provider of the Laravel framework through registerBaseServiceProviders();
Register the specific "dependency injection container" and its alias to the "Laravel service container" through registerCoreContainerAliases().
The "registration" mentioned here is ultimately the execution of the "bind" operation of the "Laravel service container" to complete the binding of the interface to the implementation.
For the sake of the table name, let’s take a look at the registerBaseBindings() method:
/** * Register the basic bindings into the container. 注册 App 实例本身到 App 容器 * * @return void */ protected function registerBaseBindings() { static::setInstance($this); $this->instance('app', $this); $this->instance(Container::class, $this); $this->instance(PackageManifest::class, new PackageManifest( new Filesystem, $this->basePath(), $this->getCachedPackagesPath() )); }
We know that the instance() method will bind the object instance $this to the container’s app and Container::class interface. Subsequently, the implementation class obtained through app()->make('app') or app()->make(Container::class) is the $this (i.e. Laravel service container instance) object. For how to use instance, you can check the Laravel service container parsing documentation, but I will also give relevant usage instructions below.
I believe everyone has a clearer understanding of "Laravel Service Container" at this point.
For more Laravel related technical articles, please visit the Laravel Framework Getting Started Tutorial column to learn!
The above is the detailed content of what is laravel container. For more information, please follow other related articles on the PHP Chinese website!