Home > PHP Framework > ThinkPHP > How does ThinkPHP routing address instantiate the controller?

How does ThinkPHP routing address instantiate the controller?

咔咔
Release: 2020-12-04 13:32:40
Original
168 people have browsed it

1. Execute the method in the controller

The request address of this article is the configured domain name.

How does ThinkPHP routing address instantiate the controller?
Request address

You can know from the above that the value of $instance is app\index\controller\Index instance.

This also has the concept of middleware. Still so, middleware will be mentioned separately in the following article and will not be explained here.

Here$this->app['middleware']->controllerWhen using this code, can you still remember whether it is ArrayAccess or __get directly?

Here we are using the form of array access to access the object, so we are using the form of ArrayAccess. The two concepts must be clearly distinguished.

How does ThinkPHP routing address instantiate the controller?
The method of executing the controller through reflection

The method name will be obtained next. As for how to obtain the method name, it is in the init of this class. The method is executed, you only need to know that the returned value is index.

What you need to pay attention to here is this line of code$this->rule->getConfig('action_suffix'), what is obtained here is the operation method suffix.

How does ThinkPHP routing address instantiate the controller?
Operation method suffix

What would it look like if we set a value for this operation method suffix now!

Add a kaka value and access it to see what the result will be.

How does ThinkPHP routing address instantiate the controller?
Added kaka for testing

When accessing at this time, it will prompt that the indexkaka method does not exist. Is it clearly visible? It means that this parameter is in Append a kaka to all method names.

How does ThinkPHP routing address instantiate the controller?
Access results

After the code for obtaining the current operation name is expanded, the next step is if (is_callable([$instance, $ action])) {, here you can see our old friend is_callable.

As for the two parameters of is_callable here, you know what they are from the above. The first parameter is the instance of app\index\controller\Index, and the second parameter is indexExecute the operation method.

Then the role of is_callable is to detect whether the index method name in the app\index\controller\Index class can be executed.

Obviously a true will be returned here, because there is an index method in the index class.

Before doing the test, you must cancel the method name suffix just configured in the app configuration file.

Through this is_callable, it can be judged that there will be three situations. Next, Kaka will analyze it from three aspects.

The first situation: There is an executable method in the class

  • First return a ReflectionMethod class
  • Get the method name index
  • If the method name suffix is ​​not set, return empty
  • Set the current operation name
  • In this case, no parameters are set, so the final $vars is an empty array.

In order to test this piece of code with parameters, we need to make a little change to the routing address.

How does ThinkPHP routing address instantiate the controller?
Test part of the code

The routing was not used before, but the default address was used directly. This routing address will be used next

How does ThinkPHP routing address instantiate the controller?
Use the new routing address for testing

Use this routing address to print the data, and you can see that it is the routing parameters we set.

How does ThinkPHP routing address instantiate the controller?
Data printing

This method of obtaining request variables will enter $this->request->param();This line of code

How the framework obtains parameters

Access address: http://www.source.com/index.php/hello/hello

As we know above, the parameters are obtained through $this->request->param(), so how do we obtain the parameters in the framework?

According to the process code, the code will be executed as shown below. According to the obtained request method, the corresponding method is used to obtain the parameters. What needs to be made clear here is that we are using the get request.

So the code will be executed to $this->param. The current request parameters and the parameters in the URL address are merged here. Pay attention to the circled place here.

Since Kaka uses routing to make requests, the framework here specifically encapsulates a request parameter for routing.

How does ThinkPHP routing address instantiate the controller?
Get the parameters of the current request

Come to this route method, and when you see the comments, you understand that it is used to get the routing parameters, but still You need to enter the input layer.

How does ThinkPHP routing address instantiate the controller?
Get routing parameters

In the previous routing article, when the routing parameters are obtained, the parameters will be merged into the route attribute of the request.

How does ThinkPHP routing address instantiate the controller?
Set routing variables

So $this->route is all the parameters of this routing rule stored, including Routing parameters.

How does ThinkPHP routing address instantiate the controller?
Print results

At this time, the execution process will be executed to obtain variables that support filtering and default values. In the above $this->route The parameter passed in is false, so this block will be returned directly.

获取变量 支持过滤和默认值
Getting variables supports filtering and default values

The results returned here will be returned to the place where we started parsing above, that is to say, this$vars is the obtained routing parameters.

How does ThinkPHP routing address instantiate the controller?
Returned routing parameter results

Second case: There is no executable method in the class

When the first judgment is executed is_callable When it is judged that the method in the class is not executable, the second situation will be executed.

How does ThinkPHP routing address instantiate the controller?
The second execution case

First request an unset routing address and see what will be returned.

How does ThinkPHP routing address instantiate the controller?
Error return

According to the prompts given by the code, we go to the index controller to create an _empty method, and then make the request Once, see what happens.

How does ThinkPHP routing address instantiate the controller?
Set the _empty method in the controller

You can see from the printing results that when the accessed method does not exist, it will be executed_empty this way.

How does ThinkPHP routing address instantiate the controller?
Print results

So how is this method executed? This execution method is achieved by using the reflection mechanism. There is a special article to explain about reflection Kaka, but you still need to read and view the document.

The third situation: there is no executable method or _empty method in the class

This situation is relatively simple, that is, the error message is returned directly , the exception handling here will also be discussed later.

How does ThinkPHP routing address instantiate the controller?
The third situation

After the execution of the three situations

After analyzing the three situations, everything will happen in the end To perform statistical methods.

The method of calling the reflection execution class supports parameter binding, which means that the closure execution process here is completed here.

The following automatic requests are explained in detail in Section 5.

How does ThinkPHP routing address instantiate the controller?
Statistical execution

2. How to instantiate the controller with the routing address

In the previous section, we explained routing for three or four periods. The final explanation was about routing scheduling. So how to execute the set routing!

Next use this route as a case

How does ThinkPHP routing address instantiate the controller?
Case route

Do you still remember what the return value is when you start routing detection? ? Please see the picture below

How does ThinkPHP routing address instantiate the controller?
Route detection
How does ThinkPHP routing address instantiate the controller?
Print result

There was no connection at that time The code is explained in detail and directly explains the instantiation controller. What I want to talk about now is the record current scheduling information line of code.

Here$this->request is used to execute the magic method of the container class when accessing a non-existent property, and finally returns an instance through the container.

So the code will be executed to the position shown below to set or obtain the scheduling information of the current request

How does ThinkPHP routing address instantiate the controller?
Set or get the scheduling information of the current request

By printing here at the controller instantiation, you will find that the value returned here is index, this value is set in the controller, then go to the controller to check it.

How does ThinkPHP routing address instantiate the controller?
Print data

Come to the init method to print the result and view the result, using the routing address

How does ThinkPHP routing address instantiate the controller?
Print data
How does ThinkPHP routing address instantiate the controller?
Use routing address
没有How does ThinkPHP routing address instantiate the controller?
No routing address used

Do you know why the value here changed?

The value printed above is the picture below, why is it the picture above here!

How does ThinkPHP routing address instantiate the controller?
Print results

The last step in the routing section is to initiate routing scheduling, and finally call a routing to module/controller/operation method .

How does ThinkPHP routing address instantiate the controller?
Routing dispatch

This methoddispatchModuleFinally, it instantiates a class. Next, we need to study this class in depth

How does ThinkPHP routing address instantiate the controller?
Route to module/controller/operation

According to the code trace, you can see that it is actually think\route\dispatch\ModuleThis class

How does ThinkPHP routing address instantiate the controller?
Alias ​​of class

When you come to the Module class, you will find that it inherits the Dispatch class

How does ThinkPHP routing address instantiate the controller?
Inheritance of class

In the controller of thinkphp/library/think/route/Dispatch.php you will find the dispatchThis variable is set.

How does ThinkPHP routing address instantiate the controller?
Variable assignment

At this time, let’s look back at Routing to module/controller/operationWhat are the parameters passed in to the method here? Haha

How does ThinkPHP routing address instantiate the controller?的参数
Parameters routed to modules/controllers/operations

So the final value is just printed in the form of a separate array.

How does ThinkPHP routing address instantiate the controller?
Instancing the controller

Then the next action is the same as the process of not using routing access, and there is no need to parse.

This is the end of how the routing address is instantiated.

Regarding $this->app->controller, index is passed in, and the entire class name is returned. The specific implementation process will not be discussed. It’s parsed, and the implementation method is $this->parseModuleAndClass. You can do your own research!

3. Execute autoResponse scheduling

In Section 4, only the methods in the execution controller are mentioned. It is returned from the place in the picture below, but how to return is not explained in detail.

Next, I will take a moment to explain how it is executed.

How does ThinkPHP routing address instantiate the controller?
Automatic request

The access routing address is as shown below. You can see that the returned data is the data that needs to be returned in the controller.

How does ThinkPHP routing address instantiate the controller?
The data returned by the controller
How does ThinkPHP routing address instantiate the controller?
The value of printed data

The printed value is as follows Picture location, you need to be clear here! Source code reading requires a little bit of exploration, and over time you will understand the contents.

How does ThinkPHP routing address instantiate the controller?
Printed position

Then we will go into depth about $this->autoResponse($data); Analysis, this method literally means automatic response.

How does ThinkPHP routing address instantiate the controller?
Automatic response

In the first line of this execution process$data instanceof Response, if you don’t understand this, please follow There is no way to read.

Those who don’t know and don’t understand still need to be solved. Just read the source code, and only by conquering it bit by bit can you win.

About the use of instanceof

Instanceof can determine whether an object is an instance of a certain class and whether an object implements a certain interface.

Next, Kaka will give you a simple example to demonstrate this, and you will understand what is going on.

Case 1

First create two classes. The case is as shown below.

How does ThinkPHP routing address instantiate the controller?
Demonstration case

The picture below is the print result. You can see that the first one returns true and the second one returns false.

Determine whether an object is an instance of a certain class, that is to say, $instance is an instance of class Test, so it will return true.

How does ThinkPHP routing address instantiate the controller?
Print results

Case 2

Case 2 is different from Case 1. An interface is established , and then the class implements the interface case.

How does ThinkPHP routing address instantiate the controller?二
Demonstration Case 2

The final return results are all true, which means that if a class implements another interface, it will be true when judging.

How does ThinkPHP routing address instantiate the controller?
Print results

The above are the two demonstration cases given for instanceof. The understanding of them is to determine whether an instance is An instance of a class.

Then back to the text, $data instanceof ResponseThis line of code will definitely not be established, because the data passed is the value returned by the controller.

So the code execution process will be executed to the position in the figure below. The is_null function is used to make a judgment. The judgment must be false, so the following code will be executed.

The first two points in this code will not be parsed.

The first one is to automatically identify the response output type by default. This is to determine whether it is an ajax request. The specific implementation method will wait for KaKa to parse the framework source code this time, and then take some time every day to analyze the framework. A little analysis of some methods.

The second position is to obtain the corresponding configuration information from the configuration file. It looks at the method of the rule class that is executed, but in the method is the code that is executed to obtain the configuration information.

How does ThinkPHP routing address instantiate the controller?
The location where the first crazy execution process of the code is executed

Then we need to analyze the third place not mentioned above, which is the code$response = Response::create($data, $type);

Come to the method of classthinkphp/library/think/Response.phpcreate , this method is used to create a Response object.

You only need to pay attention to the place circled by Kaka. There is no html in the directory thinkphp/library/think/response.

So the code will directly instantiate this class and then return.

How does ThinkPHP routing address instantiate the controller?
Create a Response object

When you come to the constructor of this class, you will mainly do a few things.

  • Assign the return value to the data attribute of this class
  • Set the page output type
  • Return status code
  • Set app instance object
  • Header information
How does ThinkPHP routing address instantiate the controller?
In Insert the image description here

and then the code will assign the return value to the $response variable of the autoResponse method.

Finally, this $response is returned, and the returned information is printed as shown below.

How does ThinkPHP routing address instantiate the controller?
Insert picture description here
How does ThinkPHP routing address instantiate the controller?
Print out the return information

Then the code will still go up The layer returns, returning to the original closure function.

In the place circled by Kaka, the next line of code is also about middleware. You just need to know that the final return result is the same as the result printed in the picture.

How does ThinkPHP routing address instantiate the controller?
Return to the middleware layer

The final return result is returned to thinkphp/library/think/route/Dispatch.php, we This is where the analysis starts.

How does ThinkPHP routing address instantiate the controller?
Insert picture description here

Return the returned result to $data, and then execute itreturn $ this->autoResponse($data);

You read that right, the code here should be familiar!

The result returned at this time is an instance of Response, so $response will be returned directly.

How does ThinkPHP routing address instantiate the controller?
Automatic response

Up to this point, the method in the controller is executed, and the response is parsed.

No matter whether it is the set routing rules or direct access using the module controller method, the response result will eventually be returned through the above method.

Persistence in learning, persistence in blogging, and persistence in sharing are the beliefs that Kaka has always upheld since his career. I hope that Kaka’s articles on the huge Internet can bring you a little bit of help. I’m Kaka, see you next time.

The above is the detailed content of How does ThinkPHP routing address instantiate the controller?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
1
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template