Home >PHP Framework >ThinkPHP >How does thinkphp load routes?
ThinkPHP URL Routing Introduction
Simply put, URL routing allows you to customize the URL you need under certain rules to achieve Beautify the URL, improve user experience, and also benefit the purpose of search engine inclusion.
Example
The original URL is:
http://www.5idev.com/index.php/Products/Show/category/ 5/id/123
The original intention of this URL is to display the product with id 123 in the 5th category. After URL routing rewriting, the URL can be:
http://www.5idev.com/index.php/product/5/123
If Rewrite of the .htaccess file is used If the rule hides the entry file, the above URL can be further simplified to:
http://www.5idev.com/product/5/123
This URL address is Relatively simple and easy to handle.
Tip: Using Apache’s URL Rewrite rules can also achieve URL customization, which will not be discussed here. If you are interested, please see the articles related to Apache Rewrite.
ThinkPHP URL routing configuration
To use the URL routing function in ThinkPHP, you need to make the following configuration:
Enable the routing function in the project configuration file Conf/config.php ( Set to true):
'URL_ROUTER_ON' => true,
The routing rule definition
is different from the 2.x version, 3.0 Routing rules are defined in the project configuration file config.php in array format. The specific definition rules are divided into rule routing and regular routing. The rule routing syntax is as follows:
Format 1: 'Routing rule'=>'[Group/Module/Operation]?Extra parameter 1=value 1&Extra parameter 2=value 2...'
Format 2: 'Routing Rules' =>array('[Group/Module/Operation]','Extra Parameter 1=Value 1&Extra Parameter 2=Value 2...')
Format 3:'Routing Rule'= >'External address'
Format 4:'Routing rule'=>array('External address','Redirection code')
##Syntax Description
Routing rules are the rules that we want to display in the URL. The following element value part is the actual URL address and parameters. If the routing rule starts with:, it means a dynamic variable, otherwise it is a static address format 2 Additional parameters can be passed in arrays or strings. Routing rules support the definition of numeric constraints for variables, for example: 'product/:id\d'=>'Products/Show' routing rules support exclusion of non-numeric variables, for example 'news/: cate^add|edit|delete'=>'News/category' routing rule supports complete matching definition, for example: the static address part in the 'product/:id\d$'=>'Products/Show' routing rule does not If you want to reference dynamic variables in case-sensitive external addresses, use :1, :2. Rule routing can support full dynamic and dynamic and static definitions, for example, ':user/blog/:id'=>'Home/Blog/user 'These rules and syntax descriptions are relatively obscure. There are examples below to help you understand the above routing rules and syntax descriptions. If the route enabling function is defined in the configuration file, when the system performs Dispatch parsing, it will determine whether the current URL has a defined route name. If so, it will perform URL parsing according to the defined routing rules.ThinkPHP URL Routing Example
Take the example at the beginning of this article as an example to see how the route is defined. Define the following rules in the project configuration file Conf/config.php: //Route definition'URL_ROUTE_RULES'=> array(
'product/:category\d/:id\d '=>'Products/Show', //Rule routing
),
For more ThinkPHP related technical articles, please visit the ThinkPHP usage tutorial column to learn!
The above is the detailed content of How does thinkphp load routes?. For more information, please follow other related articles on the PHP Chinese website!