In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this article Xiaobian introduces in detail "what is the role of the make method in laravel", the content is detailed, the steps are clear, and the details are handled properly. I hope that this article "what is the role of the make method in laravel" can help you solve your doubts, the following is to follow the editor's train of thought slowly in depth, together to learn new knowledge.
In laravel, the make method is used to parse a type from the container. The type is defined in the source code, and the result returned after parsing is an instance of type. When the container class calls the make method, if there is no registered key, it will automatically instantiate the concrete class through the reflection class.
This article operating environment: Windows10 system, Laravel6 version, Dell G3 computer.
What is the function of make method in laravel
The make method in Laravel is used to parse a type from the container. This type is defined in the source code and is not well translated into Chinese. The result returned after parsing is an instance of type.
Students who have seen the source code should know that there are make methods in both the Illuminate\ Foundation\ Application class and its parent class Illuminate\ Container\ Container class, so when executing this line of code such as index.php
1$ kernel = $app- > make (Illuminate\ Contracts\ Http\ Kernel::class)
We will first execute the make method in Illuminate\ Foundation\ Application, so let's take a look at it first. (this article takes the Kernel class make as an example.)
/ * Resolve the given type from the container. Parsing the given type * * (Overriding Container::make) from the container overrides the make method in the parent class * * @ param string $abstract given type * @ param array $parameters specifies some optional parameters * @ return mixed * / public function make ($abstract, array $parameters = []) {$abstract = $this- > getAlias ($abstract) / / call the getAlias method in the parent class / / if the type is set in the deferredServices array and the type if is not set in the instances array (isset ($this- > deferredServices [$abstract]) & &! Isset ($this- > instances [$abstract]) {$this- > loadDeferredProvider ($abstract); / then execute this method: load the service provider} return parent::make ($abstract, $parameters) defined as latency; / / call the make method of the parent class}
OK, let's take a look at the getAlias method step by step. The function of this method is to return the alias of the class. If a complete class name is given and has been set in aliases, then the alias of the class name is returned. If it is not set, the class name itself is returned. When you look at this method, you can first var_dump $app and look at the aliases array inside. It's really clever for the framework author to write this method, at least this kind of recursion is rarely used in my actual development.
/ * Get the alias for an abstract if available. * * @ param string $abstract * @ return string * * @ throws\ LogicException * / public function getAlias ($abstract) {if (! Isset ($this- > aliases [$abstract]) {return $abstract;} if ($this- > aliases [$abstract] = = $abstract) {throw new LogicException ("[{$abstract}] is aliased to itself.");} return $this- > getAlias ($this- > aliases [$abstract]);}
The next step is to judge the two arrays deferredServices and instances, in this case $kernel = $app- > make (Illuminate\ Contracts\ Http\ Kernel::class); in this case, the result is false, so the loadDeferredProvider method is not executed.
The next step is to call the make method in the parent class Illuminate\ Container\ Container
/ * Resolve the given type from the container. * * @ param string $abstract * @ param array $parameters * @ return mixed * / public function make ($abstract, array $parameters = []) {return $this- > resolve ($abstract, $parameters) / / directly call the resolve method} to read here, this article "what is the role of the make method in laravel" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it before you can understand it. If you want to know more related articles, welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.