Home >Backend Development >PHP Tutorial >About URL routing rules and static settings in thinkphp

About URL routing rules and static settings in thinkphp

不言
不言Original
2018-06-07 11:48:412247browse

This article mainly introduces thinkphp's URL routing rules and configuration methods. It analyzes ThinkPHP's routing rules and pseudo-static setting methods with examples, and analyzes related precautions. It is a very important skill in ThinkPHP development and is required. Friends can refer to

The examples in this article describe the URL routing rules and configuration methods of thinkphp. Share it with everyone for your reference. The specific analysis is as follows:

1. URL rules

1. The default is case-sensitive

2. If we don’t want to be case-sensitive, we can change it The code of the configuration file

is as follows:

'URL_CASE_INSENSITIVE'=>true,//url不区分大小写

3. If the module name is UserGroupAction, then the url to find the module must be written as

The code is as follows:

http://localhost/thinkphp4/index.php/user_group/index

4. If 'URL_CASE_INSENSITIVE'=>false, then the url can also be written as

The code is as follows:

http://localhost/thinkphp4/index.php/UserGroup/index

2. URL pseudo-static

The code is as follows:

'URL_HTML_SUFFIX'=>'html|shtml|xml',//限制伪静态的后缀

3. URL routing

1. Start routing

To be started in Enable routing support in the configuration file

The code is as follows:

'URL_ROUTER_ON'=>ture//开启路由

2. Use routing

1. Configure routing with regular expressions

The code is as follows:

'URL_ROUTE_RULES'=>array()//路由规则  
    'my'=>'Index/index',//静态地址路由  
    'my'=>'/Index/index',//静态地址路由,加/直接跳到网站根目录下。  
    ':id/:num'=>'Index/index',//动态地址路由,可以$_GET接收地址栏参数  
    'year/:year/:month/:date'=>'Index/index',//动态和静态混合地址路由  
    'year/:year\d/:month\d/:date\d'=>'Index/index',//动态和静态混合地址路由加上 \d代表类型只能是数字  
    'my/:id$'=>'Index/index',// 加上$说明地址中只能是 my/1000 后面不能有其他内容了

2. Regular expression configuration routing must start with / and end with /

The code is as follows:

'/^year\/(\d{4})\/(\d{2})\/(\d{2})/'=>'Index/index?year=:1&month=:2&date=:3'//这里\d表示必须是数字

3. Notes:

1. The more complex the route, the more forward it is.

The code is as follows:

'URL_ROUTE_RULES'=>array( 
 'my/:year/:month:/:day'=>'Index/day',  
 'my/:id\d'=>'Index/index',  
 'my/:name'=>'Index/index',  
)

2. You can use $ as a completely matching routing rule

The code is as follows:

'URL_ROUTE_RULES'=>array(  
 'my/:id\d$'=>'Index/index',  
 'my/:name$'=>'Index/index',  
 'my/:year/:month:/:day$'=>'Index/day',  
),

3. Use regular matching, starting with /^ and ending with $/

The code is as follows:

'URL_ROUTE_RULES'=>array(  
 '/^my\/(\d+)$/'=>'Index/index?id=:1',  
 '/^my\/(\w+)$/'=>'Index/index?name=:1',  
 '/^my\/(\d{4})\/(\d{2})\/(\d{2})$/'=>'Index/day?year=:1&month=:2&day=:3',  
),

Related recommendations:

Build your own PHP MVC framework

The above is the detailed content of About URL routing rules and static settings in thinkphp. 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