To parse the content of Laravel dependency injection through source code

Source: Internet
Author: User
Tags php framework
This article mainly introduces how to parse the Laravel dependency injection through the source code, the article through the sample codes introduced in very detailed, to everyone's study or work has a certain reference learning value, the need for friends below with the small series to learn together.

Objective

As we all know, PHP framework is numerous, in recent years, a framework known for its elegance, is gradually known to the domestic phper, and began to use, but Larave has a very obvious disadvantage is that his document content is poor.

This article will give you a detailed introduction about Laravel dependency injection of relevant content, share out for everyone to reference the study, the following words do not say, come together to see the detailed introduction bar.

The constructor method or member method of the controller in Laravel can use dependency injection in the way of type constraints, such as:

Public function Store (Request $request) {//todo}

The type constraint is used here $request parameter, and request is a class: \illuminate\http\request, which indicates that the parameter must be of this class or subclass.

This article through the analysis of the source code of Laravel, see why the method does not need to pass in the instance can directly use the Request it? It's just that the framework automatically helps us instantiate and pass the argument.

1. Route definition

Starting at the source, you define such a route in the routing definition file:

Route::resource ('/role ', ' Admin\rolecontroller ');

This is a resource-based route, and the Laravel will automatically generate and delete the routed entry that is changed.

The store method at the beginning of this article is the method of a controller, and the Action of the visible route definition in the diagram is also: App\http\controllers\admin\rolecontroller@store

Routing method parsing

The controller and method are found according to the route definition, and the specific method is implemented in the dispatch method.

(File: vendor/laravel/framework/src/illuminate/routing/controllerdispatcher.php)

Public Function Dispatch (Route $route, $controller, $method) {$parameters = $this->resolveclassmethoddependencies ( c1/> $route->parameterswithoutnulls (), $controller, $method);  if (method_exists ($controller, ' callaction ')) {  return $controller->callaction ($method, $parameters);}  return $controller->{$method} (... array_values ($parameters));}

First, the Resolveclassmethoddependencies method, "as the name implies", is to obtain the dependent object based on the method parameters of the class, and then invoke the class's method and inject the object arguments.

If there is more than one dependent object, foreach parses it sequentially as a parameter injection.

Get the code for the dependent object example:

protected function resolveclassmethoddependencies (array $parameters, $instance, $method) {if (! method_exists ($ instance, $method)) {  return $parameters;}  return $this->resolvemethoddependencies (  $parameters, New Reflectionmethod ($instance, $method));}

The point here is to use the reflection of PHP, note the Relectionmethod method, it gets to the class method parameter list, you can know the parameters of the type constraints, parameter names and so on.

The $instance parameter here is the Rolecontroller controller class, $method parameter is the method name Strore.

2. Obtaining an example of a dependent object

This dependent object can be instantiated by obtaining the constraint type of the dependent object from the parameter of the method.

protected function transformdependency (Reflectionparameter $parameter, $parameters) {$class = $parameter->getclass ();  If the parameter has a type-hinted class, we'll check to see if it's already in//the list of parameters. If It is we'll just skip it as it is probably a model//binding and we don't want to mess with those; Otherwise, we resolve it here. if ($class &&! $this->alreadyinparameters ($class->name, $parameters)) {  return $parameter Isdefaultvalueavailable ()   ? $parameter->getdefaultvalue ()   : $this->container->make ($class, name); }}

Gets the object from the container according to the class name, and the process of binding the object instance is defined first in the service provider.

Then the instantiated object is passed into the store method, and the dependent object can be used.

3. About PHP Reflection

Give an example of using Reflectionmethod.

Class demo{private $request; public function Store (request $request) {}}

Print out the contents of new Reflectionmethod (Demo::class, ' store ')

You can derive the parameter list of this method, the constraint type of the parameter, such as Typehint,illuminate\http\request.

Depending on the class name, you can get an instance from the service provider that was initially bound by the user.

Summarize

Articles you may be interested in:

An example of a sequential list of PHP data structures and a chain-linear table

A detailed explanation based on the difference between the comma and the dot number in PHP

Example of PHP implementation of the 1 number algorithm in statistical binary

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.