Home  >  Article  >  PHP Framework  >  ThinkPHP detects whether URL variables and rule routing match

ThinkPHP detects whether URL variables and rule routing match

咔咔
咔咔Original
2020-12-01 12:27:201937browse

This article continues to explain the routing source code. If you see this article, you can first read the routing articles written before, a total of two articles.

"

Preface

After interpreting the routing source code in the first two articles, I believe everyone has a good understanding of routing. I have a certain understanding.

This article will continue to explain the ThinkPHP routing source code analysis, which is also the end of the routing content. It will end with an article about routing scheduling. I hope everyone has a good understanding of routing. Bar!

Regarding routing, Kaka feels that it is the most difficult core point in source code reading in the entire framework, and it also consumes a lot of time.

Because there are many nested classes, why not follow common sense, such as $this->group.

Although it is a simple calling relationship, there are many functions performed in the source code.

Generally, the source code will think that this group is a simple class. In fact, it is not the case. The final result returned is a bit surprising. It is the Domain class that is returned.

So everything about the framework needs to be understood carefully. Reading the source code is mainly to improve your understanding of the framework and the design ideas of the framework.

Let’s follow the steps, first look at the execution flow chart of the mining machine, and then you can read the article clearly based on the flow chart.

All source code reading in the later period will be directly added here for supplementation.

ThinkPHP detects whether URL variables and rule routing match
Framework execution process

1. Detect routing-merge grouping parameters, check grouping routing

The last article talks about the position in the picture below. This position is still empty for the time being. This empty position is the merge grouping parameter that will be discussed next.

Parameter merging is actually merging routing parameters and default parameters.

ThinkPHP detects whether URL variables and rule routing match
Execution location

In order to clearly show the execution process, Kaka has circled the execution process.

executable file:

  1. thinkphp/library/think/App.php -> $dispatch = $this->route->check($path, $must);
  2. thinkphp/library/think/Route.php -> $result = $domain->check($this->request, $url, $completeMatch);
  3. thinkphp/library/think/route/Domain.php -> $result = $this->checkRouteAlias($request, $url); -> return parent::check($request, $url, $completeMatch);
  4. thinkphp/library/think/route/RuleGroup.php -> $this->mergeGroupOptions();

对应执行关系:

  1. Route detection returns a Dispatch object
  2. Detect domain name routing
  3. Detect alias routing-> Detect grouping Routing
  4. Merge grouping parameters
ThinkPHP detects whether URL variables and rule routing match
Route detection execution process

You can see this The title of this section is Detecting routing parameters and checking packet routing, so there is still a lot of content in detecting routing.

It’s just that Kaka only focuses on merging group parameters and checking group routing. Finally, other content does not penetrate the entire line, so we will not discuss it in depth.

The next article will talk about part of the controller, but not all of it will be written!

Merge grouping parameters

Let’s talk about the content of this piece first.

ThinkPHP detects whether URL variables and rule routing match
Merge grouping parameters

Before looking at this piece of content, you need to check the $this->parent attribute. See What is this value set to?

You can know that it is the instantiated class of Domain by printing it with debug_backtrace().

ThinkPHP detects whether URL variables and rule routing match
Print results

Then we will go to the mergeGroupOptions method for detailed explanation.

  • Executed file: thinkphp/library/think/route/RuleGroup.php 164 lines
  • ##$this->parent: class think\route\ Domain
  • Get the routing parameter definition. If the routing parameter does not exist, it is 'merge_rule_regex' => bool(false). Anyway, the routing parameter
    # will be appended.
  • ##Merge grouping parameters: $this->mergeOptions: Routing parameters 'after', 'model', 'header', 'response', 'append', 'middleware' that need to be merged with the grouping
  • Merge parameters through array_merge
  • and lock the lockOption parameter
  • and return the merged result, The final return result$this->option
  • The return result is shown in the figure below

ThinkPHP detects whether URL variables and rule routing matchThe final return result

Return resultThinkPHP detects whether URL variables and rule routing match
In the final analysis, it is to merge the routing parameters. Please see the figure below for the officially supported routing parameters. Please pay attention to the supported version number.

Under normal circumstances we do not use routing parameters. We only mention them here to let everyone know that they exist. If you insist on using them, be sure to read the version number clearly, otherwise you will encounter A lot of trouble.

Supported routing parametersThinkPHP detects whether URL variables and rule routing match
Check group routing

File: thinkphp/library/think/route/ RuleGroup.php Line 183.

Here we first need to clarify what the value of the variable $rules is.

Detect group routingThinkPHP detects whether URL variables and rule routing match
When you print the value of $rules, you can see that there are two situations.

The first case is not resource routing.

The second case is resource routing.

ThinkPHP detects whether URL variables and rule routing match
The value of $rules

This is because Kaka only sets two routes in the routing file, one resource route and one non-resource route.

ThinkPHP detects whether URL variables and rule routing match
Routing configuration file

According to the data circled in the above figure, you can know that the value of $item is divided into two situations when the loop is executed.

  • Execute the check method in think\route\Resource Object
  • Execute think\route\RuleItem The check method in Object

According to the print result of the artifact, you can see that it is also executed when routing resourcesthinkphp/library/think/route/RuleGroup.php The check method of the class.

ThinkPHP detects whether URL variables and rule routing match
Artifact Print

Why resource routing executes the check of thinkphp/library/think/route/RuleGroup.php

Because the Resource class inherits the RuleGroup class.

And the value of $item is an instance of the Resource class, so the check method will be executed.

So how important it is to have an artifact. The previous article has provided an in-depth explanation of how to use this artifact. If you don’t know how to use it yet, or don’t know how to use it, go check it out! The artifact can directly print out the execution flow of the code, which is very useful in the process of debugging source code.

ThinkPHP detects whether URL variables and rule routing match
Inheritance relationship

After executing the check method again, the final result is returned as circled in the picture below.

ThinkPHP detects whether URL variables and rule routing match
Detect route grouping

Non-resource routing execution check

File: thinkphp/library/think/route /RuleItem.php line number 231 This is the method for executing non-resource routing.

ThinkPHP detects whether URL variables and rule routing match
Non-resource routing execution method

After entering the routing rule detection method, routing parameters will still be merged.

ThinkPHP detects whether URL variables and rule routing match
Detect routing

The method of merging routing parameters has been mentioned above, so I won’t say more here.

ThinkPHP detects whether URL variables and rule routing match
Merge routing parameters

Up to this point, we have finished talking about merging group parameters under detection routing and checking group routing. If you are not clear about the idea, you can look at the mind map.

2. Check whether the URL variables and rule routing match

The following cases use normal routing and no resources are used In the case of routing, the file thinkphp/library/think/route/RuleItem.php

uses the artifact to print the data.

ThinkPHP detects whether URL variables and rule routing match
Execution process
ThinkPHP detects whether URL variables and rule routing match
Execution process

The above is the entire execution process, what follows is Content that needs to be parsed to check whether the URL and rule routing match.

ThinkPHP detects whether URL variables and rule routing match
Check whether the URL and rule routing match

Before starting the content here, let’s solve a problem with Kaka.

Go to the upper layer of the code above and print the return result.

ThinkPHP detects whether URL variables and rule routing match
Print content
ThinkPHP detects whether URL variables and rule routing match
Print result

Then take a look at the routing configuration file route.php.

In this file, Kaka only configures two routing addresses, only one of which is a resource route, and has variable rules set up.

ThinkPHP detects whether URL variables and rule routing match
Routing configuration file

At this time, add a routing address to the routing configuration file.

ThinkPHP detects whether URL variables and rule routing match
Routing configuration file

Then print the result at the beginning of the article.

Is there any question here about why false is returned?

ThinkPHP detects whether URL variables and rule routing match
Check whether the URL and the rule route match and print the results

Solution to why checking whether the URL and the rule route match returns false

Then you need to go to the source point to check.

The content of this section has been explained above. There are two situations for the item here. The first is executing think\route\Resource Object, and the second is Execute think\route\RuleItem Object.

will all execute the check method.

ThinkPHP detects whether URL variables and rule routing match
The source point of the execution process

It is very clear that you will know that the file must be executedthinkphp/library/think/route/RuleItem.php## The check in # is returned.

ThinkPHP detects whether URL variables and rule routing matchDetect routing
Based on these two lines of code, we can understand that the $match variable must be related.

This variable is used to detect whether the URL and rule routing match. This is back to what we said at the beginning.

So the source code is like this, link by link, just read it slowly, and you will understand it after you read more.

ThinkPHP detects whether URL variables and rule routing matchFind the relationship based on the code

Formally start parsing the code

Parameter description

  • $url: Access address
  • $option: Merge grouping parameters
  • ##$completeMatch: Whether the route completely matches
  • Execution file: File: thinkphp/library/think/route/RuleItem.php

Let’s take a closer look at some of the specific executions.

ThinkPHP detects whether URL variables and rule routing match
Parsed source code

First, a judgment will be made whether the route completely matches. During the process of interpreting the route, many such judgments appeared. Later, Kaka will A separate article will be written to explain why judgment is needed.

Then the code will be executed to merge the routing rules. This process merges the routing rules.

Come to the method getPattern. Since the routing rule is not set, it returns directly. The initial value of this variable of the routing rule is an empty array, so it is still an empty array after merging.

I don’t know if you will have any questions about the second line of code, what exactly is executed.

Dependency injection is performed in the constructor of this class to inject the // routing instance: think\Route.

Tracking according to the code is to obtain the configuration information from the configuration information, and the returned result is string(1) "/"

ThinkPHP detects whether URL variables and rule routing match
Merge routing rules

Then process the routing rules and change all | to /

ThinkPHP detects whether URL variables and rule routing match
Process the routing rules

According to One thing that can be made clear about the $rule returned in the above picture is that the following judgment will not be executed.

ThinkPHP detects whether URL variables and rule routing match
Judgement will not be executed

One method you need to know here is preg_quote: the function is used to escape regular expression characters

So Finally, the slash variable will be returned as string(6) "\/\-\/", where all backslashes are escape characters.

preg_replace function separates strings by a regular expression.

strncasecmp() function compares two strings (not case sensitive).

preg_match_all function is used to perform a global regular expression matching.

preg_match_all function is used to perform a global regular expression matching.

There are several functions in this method that are very unfamiliar to most people. You can only check the information yourself, and Kaka will not explain the use of these methods.

Summary

The above is the content of detecting whether the URl variable and the rule route match. The main function of this block is Let’s talk about a few aspects.

  • To check whether the route matches completely, the parameter used is complete_match.
  • Merge routing rules, which is the use of pattern.
  • The most important thing is that the next part performs various function processing on the address.
  • The final return is in the form of an array of routing parameters.
  • The above is the case where routing parameters exist. If there are no routing parameters, false will be returned directly.
  • Then go back and execute the parsing of the matched rule routing here.

I won’t leave the flow chart here. You can debug it according to the code debugging tool provided by Kaka, or you can look at the guide provided by Kaka above.

Routing is about to come to an end. The last remaining content is routing scheduling, how routing is scheduled, and to whom the final routing execution results are returned. These are all troublesome questions. Next, click Ka will provide a detailed analysis of routing scheduling.

This article is mainly focused on detecting routing parameters, routing variables, and rules. This content needs to be read together with the previous articles. There are serialized articles on the PHP Chinese web page. If you are interested, you can return Check it out!

Persistence in learning, persistence in blogging, and persistence in sharing are the beliefs that Kaka has always adhered to since his career. I hope that Kaka’s articles in the huge Internet can bring you a little Silk help. I’m Kaka, see you next time.

The above is the detailed content of ThinkPHP detects whether URL variables and rule routing match. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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