Home >PHP Framework >ThinkPHP >ThinkPHP routing parameters and resource routing analysis
“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.
”
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.
Remember above that in the file thinkphp/library/think/route/RuleItem.php
we Have you seen the use of routing parameters?
And the document also provides many supported parameters, as shown below.
How to use this routing parameter!
Then use the route we used before, and set up URL suffix detection to support matching multiple suffixes.
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.
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.
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.
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.
The request address when passing parameters.
Request address without passing parameters.
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
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.
But an error will be reported when letters are passed in.
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.
The setting of resource routing is also very simple.
It is also more convenient to use the command line to create files.
The created controller itself is a resource routing file.
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.
Then you will come to thinkphp/library/think/route/Resource.php
Class
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.
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.
blog/<name> or blog/<name></name></name>
object(think\route\Domain)
, the first article of the routing article is required. Section 3, and fullName will be assigned in this step. Then return to the upper layer to see the next thing.
After setting the group routing rules, assign values to some attributes, mainly the following three.
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.
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
Then you will come 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.
Until now, I will briefly talk about resource routing.
Finally, Kaka will give you the execution brain map. Draw it
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
There is such a line of code in the constructor, which is to get the requested domain name
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
$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.
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!
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
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.
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.
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.
When you come to the initialization application, you can see about 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.
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->route
Why can the Route class be executed.
First of all, what we need to know is that the App class inherits the Container class.
Then at the end of the Container class Several magic methods exist.
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!
Then continue our march and come tothinkphp/library/think/Route.php
Import 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
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
At this time, make an access
Yes, that is In this way, it is so simple to use
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
This method is also executed
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!