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

How does ThinkPHP routing address instantiate the controller?

咔咔
咔咔Original
2020-12-29 11:14:332011browse

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?

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

This also has the concept of middleware. Still the same, 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 access array 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 name will be obtained next. As for how to obtain the method name, it is executed in the init method of this class. Here we only You need to know that what is returned 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?

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?

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

How does ThinkPHP routing address instantiate the controller?

After the expansion of the code to obtain the current operation name is completed, 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
  • There are no parameters set in this case, so the final $vars is 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?

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

How does ThinkPHP routing address instantiate the controller?

Use this routing address to print the data, you can see that it is us Set routing parameters.

How does ThinkPHP routing address instantiate the controller?

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

at 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. Use the corresponding method to obtain the parameters according to the obtained request method. 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 is a request made using routing, the framework here specifically encapsulates a request parameter for routing.

How does ThinkPHP routing address instantiate the controller?

Come to this route method, and you will understand when you see the comments. It is used to obtain routing parameters, but you still need to enter the input layer.

How does ThinkPHP routing address instantiate the controller?

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

How does ThinkPHP routing address instantiate the controller?

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

How does ThinkPHP routing address instantiate the controller?

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

How does ThinkPHP routing address instantiate the controller?

The results returned here will be returned to the place where we started parsing above, as well That is to say, this $vars is the obtained routing parameter.

How does ThinkPHP routing address instantiate the controller?

Second case: There is no executable method in the class

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

How does ThinkPHP routing address instantiate the controller?

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

How does ThinkPHP routing address instantiate the controller?

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

How does ThinkPHP routing address instantiate the controller?

It can be seen from the printing results that when the accessed method does not exist, the _empty method will be executed.

How does ThinkPHP routing address instantiate the controller?

So how is this method executed? This execution method is achieved by using the reflection mechanism. An article was previously published to explain 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?

After the execution of the three situations

After the analysis of the three situations is completed, the statistical method will be executed in the end.

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?

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 is the set routing executed?

Next use this route as a case

How does ThinkPHP routing address instantiate the controller?

Remember the return value when starting route detection What is it? Please see the picture below

How does ThinkPHP routing address instantiate the controller?

How does ThinkPHP routing address instantiate the controller?

At that time, there was no detailed explanation of the following code, and the instantiated controller was directly explained. What I want to talk about now is the Record the 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 get the scheduling information of the current request

How does ThinkPHP routing address instantiate the controller?

By printing here when instantiating the controller, you will find that the value returned here is index. This value is set in the controller. Next, go to the controller to view it. one time.

How does ThinkPHP routing address instantiate the controller?

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?

How does ThinkPHP routing address instantiate the controller?

How does ThinkPHP routing address instantiate the controller?

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?

The last step in the routing section is to initiate routing scheduling, and finally call a route to the module/ Controller/action this method.

How does ThinkPHP routing address instantiate the controller?

This methoddispatchModuleFinally also instantiates a class, next you need to Class for further study

How does ThinkPHP routing address instantiate the controller?

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?

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

How does ThinkPHP routing address instantiate the controller?

In thinkphp/library/think/route/Dispatch.phpthis In the controller of the class, you will find that the variable dispatch is set.

How does ThinkPHP routing address instantiate the controller?

Let’s look back at this timeRouting to module/controller/operationThe method passed here What are the parameters entered? Haha

How does ThinkPHP routing address instantiate the controller?

So the final value is What was just printed was in single array form.

How does ThinkPHP routing address instantiate the controller?

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

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 parsed. The implementation method is $this->parseModuleAndClass, you can do your own research!

3. Execute autoResponse scheduling

In Section 4, it is only mentioned that the method in the execution controller is carried out from the place in the figure below Returned, 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?

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?

How does ThinkPHP routing address instantiate the controller?

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?

The next step is to $this->autoResponse($data);This method performs in-depth analysis. This method literally means automatic response.

How does ThinkPHP routing address instantiate the controller?

In the first line of this execution process$data instanceof Response, if you don’t understand this, you won’t be able to read it.

What you don’t know and don’t understand still needs 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?

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?

Case 2

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

How does ThinkPHP routing address instantiate the controller?

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

How does ThinkPHP routing address instantiate the controller?

The above are the two demonstration cases given for instanceof, to understand them It is to determine whether an instance is an instance of a certain 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 shown below. The is_null function is used to make the 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 until Kaka has parsed the framework source code this time, and then will take some time every day to learn some methods of the framework. Do a little dissection.

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?

Then you need to do the third step not mentioned above is parsed, that is, the code $response = Response::create($data, $type);

comes to the classthinkphp/library/think/Response.php In the method create, this method is used to create the 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?

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?

Then the code will assign the return value to the $response variable of this method autoResponse .

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

How does ThinkPHP routing address instantiate the controller?

How does ThinkPHP routing address instantiate the controller?

Then the code will still return to the upper layer and return 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?

The final return result is returned tothinkphp/library/think/route /Dispatch.php, we start the analysis from here.

How does ThinkPHP routing address instantiate the controller?

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?

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

Regardless of 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 in the huge Internet can bring you a little Please help me. 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!

Statement:
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