The request address of this article is the configured domain name.
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']->controller
When 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.
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.
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.
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.
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 index
Execute 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
$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.
The routing was not used before, but the default address was used directly. This routing address will be used next
Use this routing address to print the data, and you can see that it is the routing parameters we set.
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.
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.
In the previous routing article, when the routing parameters are obtained, the parameters will be merged into the route attribute of the request.
So $this->route
is all the parameters of this routing rule stored, including Routing parameters.
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.
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.
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.
First request an unset routing address and see what will be returned.
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.
You can see from the printing results that when the accessed method does not exist, it will be executed_empty
this way.
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.
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.
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
Do you still remember what the return value is when you start routing detection? ? Please see the picture below
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
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.
Come to the init method to print the result and view the result, using the routing address
Do you know why the value here changed?
The value printed above is the picture below, why is it the picture above here!
The last step in the routing section is to initiate routing scheduling, and finally call a routing to module/controller/operation method .
This methoddispatchModule
Finally, it instantiates a class. Next, we need to study this class in depth
According to the code trace, you can see that it is actually think\route\dispatch\Module
This class
When you come to the Module
class, you will find that it inherits the Dispatch
class
In the controller of thinkphp/library/think/route/Dispatch.php
you will find the dispatch
This variable is set.
At this time, let’s look back at Routing to module/controller/operation
What are the parameters passed in to the method here? Haha
So the final value is just printed in the form of a separate array.
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!
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.
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.
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.
Then we will go into depth about $this->autoResponse($data);
Analysis, this method literally means 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.
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.
Case 2
Case 2 is different from Case 1. An interface is established , and then the class implements the interface case.
The final return results are all true, which means that if a class implements another interface, it will be true when judging.
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 Response
This 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.
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.php
create
, 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.
When you come to the constructor of this class, you will mainly do a few things.
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.
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.
The final return result is returned to thinkphp/library/think/route/Dispatch.php
, we This is where the analysis starts.
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.
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!