This section applies to ThinkPHPV2.0. if you need to know the routes of other versions, this section applies to ThinkPHP V2.0. if you need to know the routes of other versions, see:
ThinkPHP 3.0 route
ThinkPHP
2.1 Route
URL routing overview
Simply put, URL routing allows you to customize the URL you need. this will beautify the URL.
To improve the user experience, it is also beneficial to search engines.
Example:
Original URL
For: http://www.phplo.com/index.php/Products/Show/Category/5/Products_id/123/status/1
After
URL after URL route rewriting
It can be:
Http://www.phplo.com/index.php/product/5/123
Of course
URL Rewrite rules can also achieve the URL customization function, which will not be expanded here. please refer to Apache Rewrite related articles.
ThinkPHP URL
Route configuration
To use the URL routing function in ThinkPHP, you need to configure the following:
In the project configuration file config. php
Which enables the routing function (set to true ):
'URL _ ROUTER_ON '=> true,
In the project configuration directory Conf
Create a route definition file named routes. php. Be sure to use UTF-8 encoding.
Routing rule definition
Routing rules are defined in routes. php.
File, in the format of array format, the specific definition rules are divided into conventional routing and generic routing, the syntax is as follows:
- Return array (
- // Regular route
- 'Route name' => array ('Module name', 'Operation name', 'parameter defining', 'additional parameter '),
- // Generic Routing
- 'Route name @ '=> array (
- Array ('Route matching regularize', 'Module name', 'Operation name', 'parameter defining', 'additional parameter '),
- ),
- ... More route name definitions
- )
In routing rule parameters, parameter definitions and additional parameters are optional and can be used as needed.
If the routing enabling function is defined in the configuration file, the system is executing
During Dispatch resolution, the system determines whether the current URL has a defined route name. if so, the system performs URL resolution according to the defined routing rules.
ThinkPHP URL
Route instance
Take the example from the beginning of this article as an example to see how the route is defined:
Return array (
//
Regular route
'Product' => array ('products', 'show ', 'Category, product_id ',
'Status = 1 '),
)
In this way, when index. php/product/5/123 is accessed
Index. php/Products/show/category/5/product_id/123/status/1.
Additional instructions
ThinkPHP
Version 2.0 does not support upper-case routing names, that is, product is acceptable.
The route is invalid.
Do not configure the route name as the module name.
The parameter definition content should be dynamic parameters, URL
The actual parameter values must be input in order.
The content of the additional parameter should be a fixed parameter value. when ThinkPHP parses the route, it will automatically implicitly pass in without the need
And the parameter format must be normal, for example, status = 1 & type = 1.
Resolve to group
If the project group is enabled, you need to resolve the route to a group module, such
Admin group:
'Product' => array ('admin. products', 'show ',
'Category, product_id ',
'Status = 1 '),
Because the default group can be omitted, if it is resolved to the default group, you do not need to add the default group name.
URL compatibility mode
If
If the URL uses the compatibility mode
The address should be: index. php /? R = product & category = 5 & products_id = 123.
The above is
ThinkPHP URL routing configuration instructions and common routing usage examples, through URL routing can produce more friendly and SEO-compliant URLs
To learn more about powerful Generic Routing, see the ThinkPHP Generic Routing section.
Read more
ThinkPHP
URL access mode
ThinkPHP project configuration