当前位置:网站首页>ThinkPHP URL 路由简介
ThinkPHP URL 路由简介
2022-07-07 14:06:00 【全栈程序员站长】
简单的说,URL 路由就是允许你在一定规则下定制你需要的 URL 样子,以达到美化 URL ,提高用户体验,也有益于搜索引擎收录的目的。
例子
原本的 URL 为:
http://www.5idev.com/index.php/Products/Show/category/5/id/123该 URL 的本意是显示第 5 分类中 id 为 123 的产品。经过 URL 路由改写后 URL 可以为:
http://www.5idev.com/index.php/product/5/123如果使用 .htaccess 文件的 Rewrite 规则再把入口文件隐藏,则上面的 URL 可以进一步简化为:
http://www.5idev.com/product/5/123这个 URL 地址就相对比较简单易容。
提示:使用 Apache 的 URL Rewrite 规则也能达到 URL 定制的功能,在此就不展开了,感兴趣的请参看 Apache Rewrite 相关的文章。
ThinkPHP URL 路由配置
在 ThinkPHP 中要使用 URL 路由功能,需要做如下配置:
在项目配置文件 Conf/config.php 里面开启路由功能(设置为 true):
'URL_ROUTER_ON' => true,路由规则定义
与 2.x 版本不同,3.0 路由规则定义于项目配置文件 config.php 内,格式为数组格式,具体定义规则又分为规则路由和正则路由。规则路由语法如下:
格式1:'路由规则'=>'[分组/模块/操作]?额外参数1=值1&额外参数2=值2...'
格式2:'路由规则'=>array('[分组/模块/操作]','额外参数1=值1&额外参数2=值2...')
格式3:'路由规则'=>'外部地址'
格式4:'路由规则'=>array('外部地址','重定向代码') 语法说明
- 路由规则即是我们要在 URL 中显示出来规则,后面元素值部分是实际的 URL 地址及参数
- 路由规则中如果以 : 开头,表示动态变量,否则为静态地址
- 格式2的额外参数可以传入数组或者字符串
- 路由规则支持变量的数字约束定义,例如:’product/:id\d’=>’Products/Show’
- 路由规则非数字变量支持排除,例如 ‘news/:cate^add|edit|delete’=>’News/category’
- 路由规则支持完整匹配定义,例如:’product/:id\d$’=>’Products/Show’
- 路由规则中的静态地址部分不区分大小写
- 外部地址中如果要引用动态变量, 采用 :1、:2 的方式
- 规则路由可以支持 全动态和动静结合定义,例如 ‘:user/blog/:id’=>’Home/Blog/user’
这些规则及语法说明比较晦涩难懂,下面会有实例来对照以便理解上述路由规则及语法说明。
如果在配置文件里定义了路由开启功能,系统在执行 Dispatch 解析的时候,会判断当前 URL 是否存在定义的路由名称,如果有就会按照定义的路由规则来进行 URL 解析。
ThinkPHP URL 路由实例
以本文开始的例子为例,看该路由是如何定义的。在项目配置文件 Conf/config.php 里定义如下规则:
//路由定义
'URL_ROUTE_RULES'=> array(
'product/:category\d/:id\d'=>'Products/Show', //规则路由
),当我们访问如下这个地址的时候:
http://www.5idev.com/index.php/product/5/123会将该地址解析到 Products 模块的 Show 操作,并传入 get 参数 category=5&id=123。
如果有额外的固定参数,如 status=1,可以定义路由:
'product/:category\d/:id\d'=>'Products/Show?status=1', //规则路由也即匹配下面这个 URL 地址:
http://www.5idev.com/index.php/product/5/123/1上面都是按格式1来定义的路由,在有额外参数的情况下,可以转换为第2种定义格式:
'product/:category\d/:id\d'=>array('Products/Show','status=1') 上面的路由规则中 \d 表示只匹配数字,当不加此约束时,则可匹配所有字符,这也是默认情况。如果要严格约定传入的参数格式,请使用正则路由定义规则。
路由格式:外部地址
对于路由格式 3 和格式 4,则是检测到匹配的路由格式,则跳转到外部地址,区别是格式 4 有重定向代码,如 301 代表永久重定向。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/113177.html原文链接:https://javaforall.cn
边栏推荐
- Step by step monitoring platform ZABBIX
- TS typescript type declaration special declaration field number is handled when the key key
- 融云斩获 2022 中国信创数字化办公门户卓越产品奖!
- Migration and reprint
- 航天宏图信息中标乌鲁木齐某单位数据库系统研发项目
- Numpy --- basic learning notes
- Talk about the cloud deployment of local projects created by SAP IRPA studio
- 深度之眼(七)——矩阵的初等变换(附:数模一些模型的解释)
- Ue4/ue5 multi thread development attachment plug-in download address
- Continuous creation depends on it!
猜你喜欢

强化实时数据管理,英方软件助力医保平台安全建设

C4D learning notes 3- animation - animation rendering process case

Shandong old age Expo, 2022 China smart elderly care exhibition, smart elderly care and aging technology exhibition

Xingruige database was shortlisted as the "typical solution for information technology application and innovation in Fujian Province in 2021"

Three. JS introductory learning notes 04: external model import - no material obj model

TS as a general cache method

95.(cesium篇)cesium动态单体化-3D建筑物(楼栋)

numpy--疫情数据分析案例

Step by step monitoring platform ZABBIX

星瑞格数据库入围“2021年度福建省信息技术应用创新典型解决方案”
随机推荐
统计学习方法——感知机
TCP framework___ Unity
leetcode 241. Different Ways to Add Parentheses 为运算表达式设计优先级(中等)
招标公告:盘锦市人民医院盘锦医院数据库维保项目
Use moviepy Editor clips videos and intercepts video clips in batches
2022第四届中国(济南)国际智慧养老产业展览会,山东老博会
UE4 exports the picture + text combination diagram through ucanvasrendertarget2d
torch.numel作用
SPI master RX time out interrupt
Description of vs common shortcut keys
Unity3D_ Class fishing project, control the distance between collision walls to adapt to different models
尤雨溪,来了!
47_Opencv中的轮廓查找 cv::findContours()
iptables只允许指定ip地址访问指定端口
Notification uses full resolution
融云斩获 2022 中国信创数字化办公门户卓越产品奖!
markdown公式编辑教程
[flower carving experience] 15 try to build the Arduino development environment of beetle esp32 C3
LeetCode3_ Longest substring without duplicate characters
When opening the system window under UE4 shipping, the problem of crash is attached with the plug-in download address