ThinkPHP2.0URL routing (applicable to version 2.0)

Source: Internet
Author: User
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:




  1. Return array (
  2. // Regular route
  3. 'Route name' => array ('Module name', 'Operation name', 'parameter defining', 'additional parameter '),

  4. // Generic Routing
  5. 'Route name @ '=> array (
  6. Array ('Route matching regularize', 'Module name', 'Operation name', 'parameter defining', 'additional parameter '),
  7. ),
  8. ... More route name definitions
  9. )

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
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.