In thinkphp, routing refers to the mapping assigned to the corresponding handler according to the URL; its function is to simplify the URL access address and make correct parsing according to the defined routing type. Simply put, routing is the parsing of a path. According to the path submitted by the client, the request is parsed to the corresponding module or controller or method.
The operating environment of this tutorial: Windows 7 system, thinkphp v5.1 version, Dell G3 computer.
Let’s study the routing mechanism of ThinkPHP5.
Although the complete development manual of TP5 talks about routing very clearly, we still study what the routing of TP5 is all about in this article. After all, reading a blog is more comfortable than reading a book.
First of all, ask three questions:
Route (route) refers to the mapping assigned to the corresponding handler based on the url.
Simply put, it is a path analysis. According to the path submitted by the client, the request is parsed to the corresponding module/controller/method.
The official document is defined like this=>The role of routing is to simplify URL access addresses and make correct parsing based on the defined routing type.
Manual explanation: When we usually access our ThinkPHP project according to our habits, the regular url should be written like this, (local example) http:// localhost/project name/public/index.php/module name/controller name/method name
After entering a string of URLs, you will feel very desperate, (whispering) = > "I just want to test a small interface that allows me to type such a long list of addresses. The address is too long and I can't remember it...". At this time, we can use routing to simplify his address, and define some rules while shortening the address.
Some people may blurt out: To simplify the path. Of course, this is one of the reasons, but the reason is not that simple.
Simplifying the path is a big reason, because I thought the path was too long at first and ran over to turn it on. But later in the process of using it, I discovered the role of Route. In fact, it is not limited to simplified paths.
We can enable the forced use of routes so that everyone can only come in through the routes I defined. This has the advantage that you write some controllers that you don’t want users to access. Scripts, if you enable full routing for forced use, users will not be able to access these scripts. Otherwise, if users access these scripts and the scripts sometimes modify the database, it will be more dangerous.
Also, we can define whether it is a get operation or a post operation coming in from this path, so that there will be no situation like before. With a controller method, you can also get it from the get operation. It can be accessed, and post can also be accessed, which is very confusing. Of course, some people will say that it is not a big deal if both get and post can be accessed. In fact, it is more helpful to distinguish between these methods to embody the idea of Restful API. of.
Similarly, I won’t talk about the regulations of http and https.
At this point, do you think routing is a bit magical? It turns out that it can do so many things, so let’s study how to use it.
Before using it, let’s first understand the configuration information. It’s still my fork project , open the config file:
There are two things to pay attention to, first of all, inside the first red box By default, tp turns on the routing mode for us. In the second red box, tp turns off the forced use of routing by default. What does it mean? Even now I can access successfully through our usual address mode, and access through routing can also be successful.
When I usually do projects, I will change this to true for safety reasons, so let’s take a look at the effect
Future When turning on forced routing:
Accessing according to the regular address can be successful, so let’s turn on forced routing now:
The same address reported an error and threw a route not found error. Because we did not define this route in the routing file, it threw an error. It shows that we are now forced to use routing.
So let’s define it now. First, start with the simplest one:
Find the route file
First of all, we need to introduce the routing class under think, and then we call the get static method inside. From the method name, we can easily know that this is accessed through the get method. , so what do the first and second parameters represent? The second parameter represents the module/controller/method in the regular URL, and the first parameter represents the name you want to use to replace the following module/controller/ method.
In other words, I originally used http://localhost/xx/public/index.php/xx/test/xx to access my controller method, but now I only need http://localhost/ xx/public/index.php/xx can access my controller method.
Look at the effect:
Successfully used routing access, then, let’s take a closer look at the formulation of this rule (for example, get and The difference between post access)
I now change the get method to the post method and then I use the get method to access and see what happens:
You can see that we use get which is not accessible. Only post can be accessed. How do you feel? Is routing a fun place?
More deeply, we can also define the route like this:
The third parameter represents the access method, like what I defined here This form is accessible to both get and post operations. The last parameter represents whether to check the https protocol. If it is false, https will not be checked. If it is true, we will not be able to find it when we use http to access it. In this way, we can further ensure the security of our interface. As for the effect, I won’t demonstrate it.
What else is special about routing? Routing can even merge the parameters passed by the get operation into the url. Our original get operation should look like this: url? id=1. After routing, we can define it as url/1. In this way, even the id parameter is hidden. Everyone knows the benefits. As for the process, just go to the development manual and search for it.
Of course, routing also has some other auxiliary functions, such as closure functions and so on. I will not introduce them one by one, because this auxiliary function is not very important during the development process. It is commonly used. If you are interested, you can go to the complete development manual to read it.
The relevant introduction to routing and the benefits of using routing are introduced here.
[Related tutorial recommendations: thinkphp framework]
The above is the detailed content of what is thinkphp routing. For more information, please follow other related articles on the PHP Chinese website!