Preface
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.
Simple analysis of initial understanding of routing Let’s talk about appearance by defining routing Routing definition What exactly does $this->group in the rule method do? Routing rule preprocessing Quick access to parse and generate routing identifiers
But there is still a lot to explain about routing. Next, we will analyze the following content.
Routing configuration (that is, in the return in the route file) First introduction to dispatch route-check detects URL routes . . . . . . . . . . . . .
The following will be explained in detail one by one.
I also give you an execution diagram about routing for your reference.
1. First understanding of dispatch and route-check to detect URL routing
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.
2. How to find the request class
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.
3. Detect domain name routing
#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.
$path : string(4) "blog" $must : bool(false)
The following things will be done when detecting URL routing.
pathinfo separator: Change / in the url to | Whether the route matches completely Detect domain name routing Default route analysis
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.
$this->request: Through the __get magic method of the container class, the make method of the container class is executed, and finally the instance object of the request is returned. This column cannot be read Articles in Section 6
$url: string(4) "blog" $completeMatch: Whether the routing is complete Match
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 routing alias: checkRouteAlias Detect URL binding: checkUrlBind -
Determine routing parameters Add domain name middleware Detect group routing: parent::check
Detect route alias: checkRouteAlias
Parameter explanation
$request: request class Example $url: The passed blog
There are two knowledge points that need to be clarified in this method
strpos: Find the position of the first occurrence in the string strstr: strstr returns a pointer pointing to the position of the first occurrence of string2 in string1, strstr( "Helloworld!","world");?>\nOutput:\nworld! The URL address will be processed first: return to blog Get the alias route definition NULL Take resource routing blog as an example and return false
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
$request: instance of request class $url: The passed blog
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
$request: An instance of the request class $url: The passed blog $completeMatch: Whether the route completely matches
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.
4. Summary
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!

The article discusses ThinkPHP's built-in testing framework, highlighting its key features like unit and integration testing, and how it enhances application reliability through early bug detection and improved code quality.

Article discusses using ThinkPHP for real-time stock market data feeds, focusing on setup, data accuracy, optimization, and security measures.

The article discusses key considerations for using ThinkPHP in serverless architectures, focusing on performance optimization, stateless design, and security. It highlights benefits like cost efficiency and scalability, but also addresses challenges

The article discusses implementing service discovery and load balancing in ThinkPHP microservices, focusing on setup, best practices, integration methods, and recommended tools.[159 characters]

ThinkPHP's IoC container offers advanced features like lazy loading, contextual binding, and method injection for efficient dependency management in PHP apps.Character count: 159

The article discusses using ThinkPHP to build real-time collaboration tools, focusing on setup, WebSocket integration, and security best practices.

ThinkPHP benefits SaaS apps with its lightweight design, MVC architecture, and extensibility. It enhances scalability, speeds development, and improves security through various features.

The article outlines building a distributed task queue system using ThinkPHP and RabbitMQ, focusing on installation, configuration, task management, and scalability. Key issues include ensuring high availability, avoiding common pitfalls like imprope


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Linux new version
SublimeText3 Linux latest version

Atom editor mac version download
The most popular open source editor

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Mac version
God-level code editing software (SublimeText3)