ThinkPHPURL routing functions and examples

Source: Internet
Author: User
This section describes the ThinkPHP routing function and the use of the U method. it is divided into the following parts. This section describes the use of the ThinkPHP routing function and the U method. it is divided into the following parts:

ThinkPHP URL
For details about the routing function, see the text below this page.
ThinkPHP regular routing and instances
ThinkPHP
U method: Use the U method to automatically generate URL hyperlinks

The routing function of ThinkPHP 3.0 is significantly different from that of 2.x. if your version is
2. x. See the following documents:

ThinkPHP 2.0 URL
Route (applicable to version 2.0)
Use of ThinkPHP Generic Routing (applicable to version 2.0)
Comparison of ThinkPHP 2.1 Routing Rules 2.0
Changes
Routing Problems (bugs) and solutions in the ThinkPHP U method (applicable to version 2.0)

ThinkPHP URL
Route overview

Simply put, URL routing allows you to customize the URL you need under certain rules to beautify the URL.
To improve the user experience, but also for the purpose of search engine indexing.

Example

Original URL
Is:

Http://www.phplo.com/index.php/Products/Show/category/5/id/123

The
The purpose of a URL is to display the product whose id is 5th in the 123 category. URL modified by URL routing
It can be:

Http://www.phplo.com/index.php/product/5/123

If you use. htaccess
The Rewrite rule of the file hides the entry file, then the above URL
It can be further simplified:

Http://www.phplo.com/product/5/123

This URL
The address is relatively simple and easy to accommodate.

Tip: The URL Rewrite rules of Apache can also be used to customize the URL. this feature is not available here. For more information, see
Apache Rewrite related articles.
ThinkPHP URL routing configuration

Use URL in ThinkPHP
The following configurations are required for the routing function:

In the project configuration file Conf/config. php, enable the routing function (set
True ):

'URL _ ROUTER_ON '=> true,

Routing rule definition

Different from 2.x, 3.0
The routing rule is defined in the project configuration file config. php.
The specific rules are classified into rule routing and regular routing. The rule routing syntax is as follows:

Format 1: 'routing rules' => '[group/module/operation]? Extra parameter 1 = value 1 & additional parameter 2 = value 2 ...'
Format 2: 'routing rules' => array ('[group/module/operation]', 'additional parameter 1 = value 1 & additional parameter 2 = value 2 ...')
Format 3: 'routing rules' => 'External address'
Format 4: 'routing rules' => array ('external address', 'redirection code ')

Syntax Description

The routing rules are
The rule is displayed in the URL. the following element values are the actual URL address and parameters.
If:
It indicates a dynamic variable. Otherwise, it is a static address.
Additional parameters in format 2 can be input into arrays or strings
Routing rules support variable numerical constraints, such as: 'Product/: id \ d' => 'products/Show'
Non-numeric variables of routing rules can be excluded, such
'News/: cate ^ add | edit | delete' => 'news/category'
The routing rules can be defined completely, for example, 'product/: id \ d $ '=> 'products/show'
The static address in the routing rule is case insensitive.
To reference dynamic variables in an external address,
Adopt: 1,: 2
Rule routing supports dynamic and dynamic definition, for example
': User/blog/: ID' => 'Home/Blog/user'

These rules and syntax descriptions are obscure. the following examples can help you understand the preceding routing rules and syntax descriptions.

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 at the beginning of this article as an example to see how the route is defined. In the project configuration file Conf/config. php
To define the following rules:

// Route definition
'URL _ ROUTE_RULES '=>
Array (
'Product/: category \ d/: id \ d' => 'products/Show ',
// Rule routing
),

When we access the following address:

Http://www.phplo.com/index.php/product/5/123

Will resolve this address
The Show operation of the Products module, and input the get parameter category = 5 & id = 123.

If there are additional fixed parameters, such
Status = 1. you can define a route:

'Product/: category \ d/: id \ d' => 'products/Show? Status = 1 ',
// Rule routing

That is, match the following URL
Address:

Http://www.phplo.com/index.php/product/5/123/1

The preceding routes are defined by format 1. when there are additional parameters, they can be converted to 2nd definition formats:

'Product/: category \ d/: id \ d' => array ('products/Show', 'status = 1 ')

In the preceding routing rules
\ D
It indicates that only numbers are matched. if this constraint is not added, all characters can be matched, which is the default condition. To strictly specify the format of input parameters, use regular routing to define rules.
Route format: external address

For the routing format
3 and format 4 are matched routing formats, then jump to the external address, the difference is that format 4 has redirection code, such as 301
Indicates permanent redirection.

Read more

ThinkPHP project configuration

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.