这篇文章主要介绍了thinkphp的URL路由规则与配置方法,实例分析了ThinkPHP的路由规则及伪静态设置方法,并分析了相关的注意事项,是ThinkPHP开发中非常重要的技巧,
本文实例讲述了thinkphp的URL路由规则与配置方法。分享给大家供大家参考。具体分析如下:
一、URL规则
1、默认是区分大小写的
2、如果我们不想区分大小写可以改配置文件
复制代码 代码如下:
'URL_CASE_INSENSITIVE'=>true,//url不区分大小写
3、如果模块名为 UserGroupAction,那么url找模块就必要要写成
复制代码 代码如下:
4、如果'URL_CASE_INSENSITIVE'=>false,那么url也可以写为
复制代码 代码如下:
二、URL伪静态
复制代码 代码如下:
'URL_HTML_SUFFIX'=>'html|shtml|xml',//限制伪静态的后缀
三、URL路由
1、启动路由
要在配置文件中开启路由支持
复制代码 代码如下:
'URL_ROUTER_ON'=>ture//开启路由
2、使用路由
1.规则表达式配置路由
复制代码 代码如下:
'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.正则表达式配置路由,,必须以/开始 /结束
复制代码 代码如下:
'/^year\/(\d{4})\/(\d{2})\/(\d{2})/'=>'Index/index?year=:1&month=:2&date=:3'//这里\d表示必须是数字
3、注意事项:
1.越复杂的路由越往前面放
复制代码 代码如下:
'URL_ROUTE_RULES'=>array(
'my/:year/:month:/:day'=>'Index/day',
'my/:id\d'=>'Index/index',
'my/:name'=>'Index/index',
)
2.可以使用$作为完全匹配的路由规则
复制代码 代码如下:
'URL_ROUTE_RULES'=>array(
'my/:id\d$'=>'Index/index',
'my/:name$'=>'Index/index',
'my/:year/:month:/:day$'=>'Index/day',
),
3.用正则匹配的方式,以/^开始,以$/结束
复制代码 代码如下:
'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',
),
希望本文所述对大家的ThinkPHP建站有所帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
