这篇文章主要介绍了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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Chinese version
Chinese version, very easy to use

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.