当前位置:网站首页>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
边栏推荐
- 谈谈 SAP iRPA Studio 创建的本地项目的云端部署问题
- C Alibaba cloud OSS file upload, download and other operations (unity is available)
- A JS script can be directly put into the browser to perform operations
- 2022 the 4th China (Jinan) International Smart elderly care industry exhibition, Shandong old age Expo
- Unity3d click events added to 3D objects in the scene
- 2022第四届中国(济南)国际智慧养老产业展览会,山东老博会
- Rongyun won the 2022 China Xinchuang digital office portal excellence product award!
- Detailed explanation of unity hot update knowledge points and introduction to common solution principles
- torch. Numel action
- Three. JS introductory learning notes 03: perspective projection camera
猜你喜欢

A wave of open source notebooks is coming
![Unity drawing plug-in = = [support the update of the original atlas]](/img/b0/92114ffb1f168a1f27125db46c6797.jpg)
Unity drawing plug-in = = [support the update of the original atlas]

Lecturer solicitation order | Apache seatunnel (cultivating) meetup sharing guests are in hot Recruitment!

深度之眼(六)——矩阵的逆(附:logistic模型一些想法)

Rongyun won the 2022 China Xinchuang digital office portal excellence product award!

Three. JS introductory learning notes 05: external model import -c4d into JSON file for web pages

星瑞格数据库入围“2021年度福建省信息技术应用创新典型解决方案”

企业级日志分析系统ELK

Wireless sensor networks -- ZigBee and 6LoWPAN

Xingruige database was shortlisted as the "typical solution for information technology application and innovation in Fujian Province in 2021"
随机推荐
Shader basic UV operations, translation, rotation, scaling
SPI master RX time out interrupt
招标公告:2022年云南联通gbase数据库维保公开比选项目(第二次)比选公告
保证接口数据安全的10种方案
Three. JS introductory learning notes 04: external model import - no material obj model
TiDB For PostgreSQL和YugabyteDB在Sysbench上的性能对比
神经网络c语言中的指针是怎么回事
121. The best time to buy and sell stocks
Mesh merging under ue4/ue5 runtime
What else can an ordinary person do besides working in a factory to make money?
Xcode Revoke certificate
Three singleton modes of unity (hungry man, lazy man, monobehavior)
A wave of open source notebooks is coming
Three. JS introductory learning notes 13: animation learning
laravel 是怎么做到运行 composer dump-autoload 不清空 classmap 映射关系的呢?
Syntaxhighlight highlights the right scroll bar
Limit of total fields [1000] in index has been exceeded
Numpy -- epidemic data analysis case
Eye of depth (VII) -- Elementary Transformation of matrix (attachment: explanation of some mathematical models)
TS as a general cache method