Home >PHP Framework >ThinkPHP >ThinkPHP routing parameters and resource routing analysis

ThinkPHP routing parameters and resource routing analysis

咔咔
咔咔Original
2020-12-01 12:01:464245browse

Routing is an important link in project development. Every project will use routing to manage interfaces. Next, this article will take you to learn routing from the source code.

1. Routing parameters and variable rules

Same code first Start here, but this time the focus is on routing parameters and variable rules.

These two points are just to briefly explain the use and simple execution process.

I won’t explain it for a deeper understanding, because these two points are not often used in the usual development process.

ThinkPHP routing parameters and resource routing analysis
Register routing rules

Remember above that in the file thinkphp/library/think/route/RuleItem.php we Have you seen the use of routing parameters?

ThinkPHP routing parameters and resource routing analysis
About routing parameter processing

And the document also provides many supported parameters, as shown below.

ThinkPHP routing parameters and resource routing analysis
Parameters supported by the document

How to use this routing parameter!

Then use the route we used before, and set up URL suffix detection to support matching multiple suffixes.

ThinkPHP routing parameters and resource routing analysis
Routing case

So how should we access the route we set up!

If you access according to normal routing rules, an error will be reported. Please continue reading for the correct access method.

ThinkPHP routing parameters and resource routing analysis
Access results

The correct request address should be http://www.source.com/index.php/hello/1. html, that is, the suffix html we set needs to be spliced ​​to the request address.

ThinkPHP routing parameters and resource routing analysis
Access results

The specific execution process of this block can be simply looked at using the code tracker. Kaka will not demonstrate it here.

Regarding the routing parameters, I will show you a case here, and it is basically over.

Global routing parameters

The last description of routing parameters is the global routing parameters.

Go directly to the document to see it.

ThinkPHP routing parameters and resource routing analysis
Global routing parameters

Here, Kaka sets up two routing rules for testing, one parameter is optional and one is required, and the routing rules are set to add file suffixes.

ThinkPHP routing parameters and resource routing analysis
Routing file

The request address when passing parameters.

ThinkPHP routing parameters and resource routing analysis
Access result

Request address without passing parameters.

ThinkPHP routing parameters and resource routing analysis
Access results

The above is what is written for the routing parameters.

There is no particularly in-depth explanation. It basically explains how to use it. This thing exists, so just understand it.

Variable rules

The same variable rules, this is even less available in Kaka’s daily development work.

Variable rules Kaka believes that the only benefit is to filter parameters.

That is to say, variable rules are behaviors that only exist when routing rules have parameters.

Kaka Here I will provide you with a simple case.

Demo case

ThinkPHP routing parameters and resource routing analysis
Routing file demonstration case

Append parameters after the routepattern

Then let’s take a look at how to write this request .

When the parameter is a number, the incoming parameter can be output.

ThinkPHP routing parameters and resource routing analysis
Requesting the address for the first time

But an error will be reported when letters are passed in.

ThinkPHP routing parameters and resource routing analysis
Print results

So the variable rule is to filter the parameters after the routing rule, which is processed using regular rules.

So far, this is a brief introduction to routing parameters and variable rules.

Although the content is very simple, most of them are use cases to introduce how to use it, and there is no in-depth study of the source code.

First of all, I rarely use it during daily development.

Second, we need to understand it further in the following article. Here is just a general understanding.

In fact, it is estimated that many people still don’t understand why the internal content can appear when entering the routing address. We will have an in-depth understanding of this later.

2. Resource routing

The setting of resource routing is also very simple.

ThinkPHP routing parameters and resource routing analysis
Routing file

It is also more convenient to use the command line to create files.

ThinkPHP routing parameters and resource routing analysis
Use commands to create files

The created controller itself is a resource routing file.

ThinkPHP routing parameters and resource routing analysis
Resource routing controller

Then the resource method of the Route file will still be executed.

This method will also have routing rules, routing addresses, routing parameters, and variable rules.

ThinkPHP routing parameters and resource routing analysis
Register resource routing

Then you will come to thinkphp/library/think/route/Resource.phpClass

Do some simple attribute assignments in this class in time.

Then there is the place circled by Kaka. Next, Kaka will explain the circled place.

ThinkPHP routing parameters and resource routing analysis
thinkphp/library/think/route/Resource.php

Since the resource class inherits the RuleGroup class, it will jump to thinkphp/library/think /route/RuleGroup.php class.

Come to the action performed by this method.

  • The routing rules will be simply processed. If there are parameters in the route, the routing rules need to be converted into blog/<name> or blog/<name></name></name>
  • The next step is to explain $this->parent, why here is object(think\route\Domain), the first article of the routing article is required. Section 3, and fullName will be assigned in this step.
  • The last step is to assign the value to fullName. In fact, it is to assign the routing rule to fullName.
ThinkPHP routing parameters and resource routing analysis
Set the routing rules for the group

Then return to the upper layer to see the next thing.

ThinkPHP routing parameters and resource routing analysis
Return to the upper-level file and continue reading the source code

After setting the group routing rules, assign values ​​to some attributes, mainly the following three.

  • Routing variable rules
  • Routing parameters
  • REST routing method definition

There is nothing to say about attribute assignment, you just need to understand what the corresponding attribute does.

The next step is to use the request method as the key value and $this as the value to return an instance of this class, and detect whether this routing method is an annotation routing.

The returned result is printed out for everyone to take a brief look at. The content has not been intercepted, so you just need to know what the value is.

ThinkPHP routing parameters and resource routing analysis
Return results

After the above execution, it will return to the upper layer to execute the lazy method

First know what the parameter passed in is: whether the route is delayed in parsing

ThinkPHP routing parameters and resource routing analysis
The code that returns to the upper layer after execution

Then you will come lazy method

ThinkPHP routing parameters and resource routing analysis
lazy method

In this method parseGroupRule will be called, and the parameter passed in is the routing rule of the group

The content of this section is about routing grouping, so we won’t discuss it here.

ThinkPHP routing parameters and resource routing analysis
Group routing rules

Until now, I will briefly talk about resource routing.

Finally, Kaka will give you the execution brain map. Draw it

ThinkPHP routing parameters and resource routing analysis
Resource Reason Execution Process

3. Regarding when the domain name was set

In fact, there is a line of code like this in the previous section$this->parent->getDomain()

This line of code is based on the method name We all know that

is to obtain the requested domain name, but do we all know where this domain name is set?

When you request this resource route, the constructor of the Route class will be executed

ThinkPHP routing parameters and resource routing analysis
Resource routing

There is such a line of code in the constructor, which is to get the requested domain name

ThinkPHP routing parameters and resource routing analysis
Constructor

You will come to the host method

In the host method, this parameter is true

You only need to pay attention here$this->server(' HTTP_HOST') is enough, which is used to obtain the domain name address

. You can see it in the second place circled by Kaka. The first condition is true, but the second condition is not true. ’s

, so it will directly return to the $this->host

ThinkPHP routing parameters and resource routing analysis
host method
## obtained in the previous step. #Print

$this->host, I will not explain why it is executed twice. If you have any questions, you can use the function debug_backtrace to check.

ThinkPHP routing parameters and resource routing analysisPrint results
The method of initializing the default domain name will be executed immediately

That is, the domain name is initialized here

ThinkPHP routing parameters and resource routing analysis
Initializing the default domain name method

At this time, there will still be questions, which is obviously thinkphp/library/think/route/Resource.php It is executed in the class!

Setting the domain name address is in the class thinkphp/library/think/Route.php!

As soon as you think about this, you can understand that it is because of the inheritance relationship!

ThinkPHP routing parameters and resource routing analysis
thinkphp/library/think/route/Resource.php

The setting of this domain name should be very clear at this point, if you still don’t understand Just read more articles written by Kaka!

Kaka It takes several hours to write an article, but it only takes a few minutes for you to read it. What can you read in a few minutes?

Take your time with the code, what you want is quality, not efficiency!

So far, we have finished explaining when the domain name was set. Next, we will explain the routing configuration-array configuration

4. Routing configuration - Interpretation of array configuration

In fact, it is estimated that many people will be a little confused after reading this.

Most people don’t understand, what does this mean, where is this thing!

In fact, this configuration is only available after 5.1. This configuration does not exist in the previous version of Kaka.

The location of this configuration is actually inside the Route we have been studying. I didn’t expect it! It's just that powerful.

is the return in the routing configuration.

ThinkPHP routing parameters and resource routing analysis
Routing configuration file

Let’s review the loading of the routing file!

The first is the entry file, there is no doubt about this.

The circled place will return the instance of App. If you don’t understand, you can read the article about containers.

So this line of code will be executed to the run method of the App class.

ThinkPHP routing parameters and resource routing analysis
Entry file

Then come to thinkphp/library/think/App.php and take a look at the run method of this class.

What you can see directly is the initialization application.

ThinkPHP routing parameters and resource routing analysis
Initialization application

When you come to the initialization application, you can see about route initialization

ThinkPHP routing parameters and resource routing analysis
Route initialization

Come to the routeInit method

When importing the routing configuration, there is a judgment on it. The judgment here is based on the array configuration in the routing configuration file.

ThinkPHP routing parameters and resource routing analysis
Import routing configuration

At this point you should have a certain understanding of when the routing configuration is loaded.

But there is no mention in the document about configuring routing in array mode, which means there is no use case.

So we need to track according to the import method.

Just find out how to use it.

Here, Kaka is explaining this line of code $this->route->import($rules);.

In fact, $this->routeWhy can the Route class be executed.

First of all, what we need to know is that the App class inherits the Container class.

ThinkPHP routing parameters and resource routing analysis
Inheritance relationship

Then at the end of the Container class Several magic methods exist.

ThinkPHP routing parameters and resource routing analysis
Container Magic Method

This __get magic method will be executed when the Route class that does not exist is called in the App.

Then the make method in the __get magic method will be executed.

Regarding this method, Kaka has said it no less than three times. You can read the article about containers!

ThinkPHP routing parameters and resource routing analysis
The make method in the container class

Then continue our march and come tothinkphp/library/think/Route.phpImport the routing rules of the configuration file, method import

You can see several familiar variable rules in this method

Then use these values ​​​​for a brief test

ThinkPHP routing parameters and resource routing analysis
Detect the imported configuration file The routing rules

Just explained the resource routing in the second section, then use the check resource routing as a condition to test

Be sure to set the previously set resources The routing configuration is turned off, otherwise the test cannot be carried out

ThinkPHP routing parameters and resource routing analysis
Routing configuration file

At this time, make an access

Yes, that is In this way, it is so simple to use

ThinkPHP routing parameters and resource routing analysis
Access results

In fact, the array method configuration routing is the same as the resource routing, but the order of execution That’s it

In the end, the routing rules are passed to the resource method through a loop

ThinkPHP routing parameters and resource routing analysis
The resource is executed

This method is also executed

ThinkPHP routing parameters and resource routing analysis
resource method

This section will briefly talk here. The main thing is that you need to have a certain understanding of route initialization

and learn to use routing arrays Method configuration

This method is not often used in the previous version, 5.1 or the released version 6.0, and it can also be said that it will not be used at all

I mention it here just to review the routing initialization process and to know what the array configuration routing does

If you have any questions, you should solve them instead of leaving them unsolved

The above is the detailed content of ThinkPHP routing parameters and resource routing analysis. 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