Due to the length of the article, I will write it in a new article.
In the previous article, I explained the following content to you.
But there is still a lot to explain about routing. Next, we will analyze the following content.
The following will be explained in detail one by one.
I also give you an execution diagram about routing for your reference.
The content of this part is inside the execution application. Next, Kaka will give you a brief introduction.
There is no explanation of the source code in this section, it is just used to pave the way for the rest of the article, so it is necessary to know what dispatch is all about.
The following figure shows the process of returning to the upper layer to continue execution after routing initialization.
Then the route detection will be executed.
The routing test is used as shown below
Then we can print this scheduling information
In the above picture, the relevant values about dispatch have been printed out
Then there will be a simple preview of the routeCheck
method
In the method above, just make it clear that the cache will be processed in this step and a Dispatch object will be returned.
You can simply take a look at the source code of this piece, it is not very important.
route-check Detect URL routing
But the content of this piece still needs to be briefly looked at.
Before looking at it, you need to clarify what the two parameters passed in are.
Parameter 1: routing rules Parameter two: Check whether mandatory routing is configured
After knowing the meaning of the parameters, you need to go to the check method. Find out.
In this method, regarding the automatic detection of domain name routing, let’s print what the data looks like.
In fact, the result returned is the same as the previous resource routing mounting method.
Then it will be passed pathinfo separator: Change the / in the url to |
and get the route from the configuration file to see if it completely matches
Use the default route for final execution
Details here I won’t go into in-depth analysis. There are too many details about routing. If you focus on the details one by one, it will take a lot of time.
So the content of this section ends here. You only need to know what is executed and what is returned in the end.
In the previous section$result = $ domain->check($this->request, $url, $completeMatch);
will execute the content of this block.
We don’t care what this method performs here.
Rather, you need to care about whether $this->request
is found and executed.
The first thing you can see is that the request attribute exists in the Route class.
Then come to the constructor of Route, where you will find a new world.
ArrayAccess is used here to access the object like an array, but the request attribute does not exist in $app, so the __get magic method in the container class will be executed. What is called in the __get method is the container. The make method, the first parameter is request, and the instance of request will eventually be returned.
The $app here is actually the App instance that comes in through dependency injection.
After reading so much source code, I must know that the App class inherits the Container class, which is the container class.
There are several magic methods at the bottom of the container class.
You only need to pay attention to the __get method here.
#__get method is a function that will be executed when accessing a non-existent property.
That is to say, the make method will eventually be executed.
This method will go through a series of operations and eventually return an instance of Request.
And store this instance in the container, and you can get it directly the next time you use it.
About the make method in the container class, it is a particularly important method in the container class and is also the soul method.
Instances of the entire framework are returned through the container, so there is no need to say more about the importance of this method.
Kaka has had a very in-depth understanding of containers before and presented it to everyone in the form of articles.
#I will first draw the process for you, and then follow this step according to the process Just click the rhythm.
The first thing to confirm is that domain name routing detection is performed in the executing application.
The upper-level execution process is where the entry file is.
First the code will be executed into the routeCheck
method, then look at this file first.
Look at the comments first. The explanation of this method is URL routing detection.
In this method, the routing cache will first be detected. This content is about Cache.
The most important thing in this method is routing detection. Returning a Dispatch object
is this method.
The next step is to look at this method.
The first thing to clarify is what the two parameters passed in are.
The following things will be done when detecting URL routing.
The next step is to conduct an in-depth analysis of the domain name routing process.
The first two executions are just some string processing, just take a look and know what will be returned in the end.
Also clarify the meaning of the three parameters in the execution of detecting domain name routing.
Articles in Section 6
comes to$result = $domain->check($this->request, $url, $completeMatch);
here, that is, this The point of the festival.
In this method, the following processes will be executed, and important execution processes will be analyzed in depth.
Detect route alias: checkRouteAlias
Parameter explanation
There are two knowledge points that need to be clarified in this method
There is a method in detecting routing aliases that you need to take a look at
The parameter is the blog passed in in the picture above
Coming to this method, the first thing to make clear is that this method is in the classthinkphp/library/think/Route.php
中
And this class uses all classes under think\route
This method will get the blog routed from the detection and then obtain it from the alias attribute in the Route class , if it does not exist, NULL will be returned
The use of this alias will be mentioned below
Come Detect the last part of the alias routereturn $item? $item->check($request, $url): false;
This is the line of code. As you can see from the picture above, this item is NULL
And eventually return this NULL.
Detect URL binding: checkUrlBind
Parameter description
In this method, only the places circled in the picture below are explained in detail.
Come to methodgetBind
Read route binding, you can see that Kaka has passed in The parameters are printed.
This method is in the class thinkphp/library/think/route/Domain.php
. Remember that this class is used when setting routing rules in $This->group. I don’t know. You can read the first section of the routing article.
At the same time, in this method, subDomain
the current subdomain name will be obtained.
This method will eventually return www, mainly look at the first circled part.
Get the current domain name through the host method in the request class, and then split it.
Return data: array(1) { [0] =>\n string(3) "www"\n}
Assign a value to the subdomain name: $this->subDomain
Return the final result and return the subdomain name: www
Then it will return to the upper layer, where the judgment and acquisition will be made The current subdomain of WWW.
Some are all judgment processing. The first judgment will definitely not be established, because only www is returned, not.
The following judgments are based on routing binding. , you just need to know that NULL will always be returned.
We know that NULL is returned at the bottom layer, so the judgment here will also not be established, so the final result is returned to the upper layer It's false.
Determine routing parameters
According to the above figure, the execution process will eventually return to thinkphp/library/think/route/Domain.php
This method check
detects domain name routing.
Then start judging the routing parameters.
If there is no routing parameter, it will be skipped and not executed.
Exists routing parameters: Execute method setRouteVars: Set routing variables. This parameter can only be used in framework version 5.1.5 or above. Since the version used by Kaka is a bit low, I will not explain it in detail.
Add domain name middleware
Regarding middleware, I will not explain it here, because a new article will be opened later to explain it in detail. This article still focuses on routing!
Detect group routing
Then you will come to the last process of detecting domain name routing and execute the codereturn parent::check($request, $url , $completeMatch);
will jump to the class file: thinkphp/library/think/route/RuleGroup.php
, because the Domain class inherits the RuleGroup class.
Parameter Description
In this method, Kaka will only explain one of the processes here in detail, which is to merge the grouping parameters.
Because this method is also a main line throughout the execution process, and the rest are methods for detection and judgment.
I have spent two articles on routing and it is not over yet. After reading the source code for so long, routing is the most complicated and difficult to understand.
The classes are linked one by one. The routing will be temporarily understood here, and other content will be added later when reading other source codes.
You must read carefully the flow chart mainly executed in the routing article.
What is finally returned through the group attribute when registering routing rules is the Domain class. The content here must be clear.
Mainly know the configuration process of domain name in routing and when the domain name is configured.
You must have a clear idea about the return array in the routing file and the process of importing the routing file.
Then let’s review the ArrayAccess we learned before and access objects like arrays.
Magic method __get method in the container. In this magic method, there is the make method, which is mainly used to return an instance of a class and store it in the container.
This is about the routing aspect for the time being. It is expected that the routing will be finished in one article.
“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 Nuoda Internet can bring you a little bit of help .I’m Kaka, see you next time.
”
The above is the detailed content of ThinkPHP detects URL routing in-depth analysis. For more information, please follow other related articles on the PHP Chinese website!