Home  >  Article  >  PHP Framework  >  How does thinkphp load routes?

How does thinkphp load routes?

步履不停
步履不停Original
2019-07-01 10:48:063179browse

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
),

When we access the following address:

http://www.5idev .com/index.php/product/5/123


will resolve the address to the Show operation of the Products module, and pass in the get parameter category=5&id=123.

If there are additional fixed parameters, such as status=1, you can define the route:

'product/:category\d/:id\d'=>'Products/Show?status =1', //Rule routing


also matches the following URL address:

http://www.5idev.com/index.php/product/5/123 /1


The above are routes defined according to format 1. If there are additional parameters, they can be converted to the second definition format:

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



In the above routing rules, \d means only matching numbers. When Without this constraint, all characters can be matched, which is also the default situation. If you want to strictly agree on the format of the incoming parameters, use regular routing to define rules.

Routing format: external address

For routing format 3 and format 4, if a matching routing format is detected, it will jump to the external address. The difference is that format 4 has a redirection code. For example, 301 represents permanent redirection.

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn