Home > PHP Framework > ThinkPHP > body text

ThinkPHP detects URL routing in-depth analysis

咔咔
Release: 2020-12-01 13:41:01
Original
2080 people have browsed it

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.

ThinkPHP detects URL routing in-depth analysis
Route execution process

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.

ThinkPHP detects URL routing in-depth analysis
dispatch initial execution location

The routing test is used as shown below

ThinkPHP detects URL routing in-depth analysis
Routing test use case

Then we can print this scheduling information

ThinkPHP detects URL routing in-depth analysis
Print data
ThinkPHP detects URL routing in-depth analysis
Print result

In the above picture, the relevant values ​​​​about dispatch have been printed out

Then there will be a simple preview of the routeCheck method

ThinkPHP detects URL routing in-depth analysis
routeCheck

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

ThinkPHP detects URL routing in-depth analysis
Return a dispatch object

After knowing the meaning of the parameters, you need to go to the check method. Find out.

ThinkPHP detects URL routing in-depth analysis
Detect url routing

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.

ThinkPHP detects URL routing in-depth analysis
Print the result

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.

ThinkPHP detects URL routing in-depth analysis
Attributes of the request object

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.

ThinkPHP detects URL routing in-depth analysis
Constructor

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.

ThinkPHP detects URL routing in-depth analysis
Magic method

#__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.

ThinkPHP detects URL routing in-depth analysis
The make method in the container class

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.

ThinkPHP detects URL routing in-depth analysis
Routing domain name detection flow chart

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.

ThinkPHP detects URL routing in-depth analysis
Execute the application

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 objectis this method.

ThinkPHP detects URL routing in-depth analysis
URL routing detection

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)
ThinkPHP detects URL routing in-depth analysis
Route detection

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.

ThinkPHP detects URL routing in-depth analysis
Detecting URL routing

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 readArticles 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
ThinkPHP detects URL routing in-depth analysis
Detect domain name routing

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
ThinkPHP detects URL routing in-depth analysis
Detect route alias

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

ThinkPHP detects URL routing in-depth analysis
Get the alias route definition

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

ThinkPHP detects URL routing in-depth analysis
Get the alias route definition

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.

ThinkPHP detects URL routing in-depth analysis
Detect URL binding

Come to methodgetBindRead 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

ThinkPHP detects URL routing in-depth analysis
Get the current subdomain name

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.

ThinkPHP detects URL routing in-depth analysis
Judgment

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.

ThinkPHP detects URL routing in-depth analysis
Detect URL binding

Determine routing parameters

According to the above figure, the execution process will eventually return to thinkphp/library/think/route/Domain.phpThis 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.

ThinkPHP detects URL routing in-depth analysis
Detect domain name routing
ThinkPHP detects URL routing in-depth analysis
Documentation

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.

ThinkPHP detects URL routing in-depth analysis
Merge grouping parameters

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.

ThinkPHP detects URL routing in-depth analysis
Execution flow chart

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!