Home>Article>PHP Framework> How does ThinkPHP routing address instantiate the controller?
#The request address of this article is the configured domain name.
You can know from the above that the value of$instance
isapp\ 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']->controller
When 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.
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 isindex
.
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 method of indexkaka does not exist. Is it clearly visible? It means that this parameter is appended to all method names. A kaka.
After the expansion of the code to obtain the current operation name is completed, the next step isif (is_callable ([$instance, $action])) {
, here you can see our old friendis_callable
.
As for the two parameters of is_callable here, you know what they are from the above. The first parameter is the instance ofapp\index\controller\Index
, and the second parameter isindex
Execute the operation method.
Then the role ofis_callable
is to detect whether theindex
method name in theapp\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 thisis_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 empty array.
In order to test this piece of code with parameters, we need to make a little change to the routing address.
The route was not used before, but the default address was used directly. This route will be used next Address
Use this routing address to print the data, you can see that it is us Set routing parameters.
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.
Come to thisroute
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.
In the previous routing article, the parameters will be merged when the routing parameters are obtained. to the route attribute of the request.
So$this->route
is the stored route All parameters of the rule, 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, as well That is to say, this$vars
is the obtained routing parameter.
Second case: There is no executable method in the class
When the first judgment is executedis_callable
determines 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, then request it again and see what happens.
It can be seen from the printing results that when the accessed method does not exist, the_empty
method will be executed.
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.
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.
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
Remember the return value when starting route detection What is it? Please see the picture below
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 theRecord 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
By printing here when instantiating the controller, you will find that the value returned here isindex
. This value is set in the controller. Next, go to the controller to view it. one time.
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 route to the module/ Controller/action this method.
This methoddispatchModule
Finally also instantiates a class, next you need to Class for further study
According to the code trace, you can see that it is actuallythink\route\dispatch\Module
This class
When you come to the classModule
, you will find that it inheritsDispatch
Class
Inthinkphp/library/think/route/Dispatch.php
this In the controller of the class, you will find that the variabledispatch
is set.
Let’s look back at this timeRouting to module/controller/operation
The method passed here What are the parameters entered? Haha
So the final value is What was just printed was in single array form.
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!
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.
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.
The next step is to$this->autoResponse($data);
This method performs in-depth analysis. This method literally means automatic response.
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.
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 classTest
, so it will return true.
Case 2
Case 2 is different from Case 1 In this case, an interface is established, and then the class implements the interface.
The final return results are all true, which means that if a class implements another interface, it will be true during judgment.
The above are the two demonstration cases given forinstanceof
, to understand them It is to determine whether an instance is an instance of a certain 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 shown below. Theis_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.
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 methodcreate
, 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 directorythinkphp/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.
Then the code will assign the return value to the$response
variable of this methodautoResponse
.
Finally, this$response
is returned, and the returned information is printed as shown below.
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.
The final return result is returned tothinkphp/library/think/route /Dispatch.php
, we start the analysis from 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 ofResponse
, so$response
will be returned directly.
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!