当前位置:网站首页>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
边栏推荐
- Three. JS introduction learning notes 12: the model moves along any trajectory line
- Unity3d click events added to 3D objects in the scene
- 讲师征集令 | Apache SeaTunnel(Incubating) Meetup 分享嘉宾火热招募中!
- Excessive dependence on subsidies, difficult collection of key customers, and how strong is the potential to reach the dream of "the first share of domestic databases"?
- 修改配置文件后tidb无法启动
- A wave of open source notebooks is coming
- 应用程序和matlab的通信方式
- Enterprise log analysis system elk
- 统计学习方法——感知机
- 航天宏图信息中标乌鲁木齐某单位数据库系统研发项目
猜你喜欢

航运船公司人工智能AI产品成熟化标准化规模应用,全球港航人工智能/集装箱人工智能领军者CIMC中集飞瞳,打造国际航运智能化标杆

神经网络c语言中的指针是怎么回事

谈谈 SAP iRPA Studio 创建的本地项目的云端部署问题

Strengthen real-time data management, and the British software helps the security construction of the medical insurance platform

尤雨溪,来了!

Logback日志框架第三方jar包 免费获取

模仿企业微信会议室选择

Three. JS introductory learning notes 07: external model import -c4d to JSON file for web pages -fbx import

Numpy -- epidemic data analysis case

Excessive dependence on subsidies, difficult collection of key customers, and how strong is the potential to reach the dream of "the first share of domestic databases"?
随机推荐
Three. JS introductory learning notes 00: coordinate system, camera (temporarily understood)
hellogolang
Unity的三种单例模式(饿汉,懒汉,MonoBehaviour)
You Yuxi, coming!
Excessive dependence on subsidies, difficult collection of key customers, and how strong is the potential to reach the dream of "the first share of domestic databases"?
Use moviepy Editor clips videos and intercepts video clips in batches
Good news! Kelan sundb database and Hongshu technology privacy data protection management software complete compatibility adaptation
prometheus api删除某个指定job的所有数据
Eye of depth (VI) -- inverse of matrix (attachment: some ideas of logistic model)
Step by step monitoring platform ZABBIX
SPI master rx time out中断
Wireless sensor networks -- ZigBee and 6LoWPAN
Shader basic UV operations, translation, rotation, scaling
A link opens the applet code. After compilation, it is easy to understand
How to implement backspace in shell
Shipping companies' AI products are mature, standardized and applied on a large scale. CIMC, the global leader in port and shipping AI / container AI, has built a benchmark for international shipping
Function: JS Click to copy content function
Bidding announcement: 2022 Yunnan Unicom gbase database maintenance public comparison and selection project (second) comparison and selection announcement
Mysql database backup script
MySQL中, 如何查询某一天, 某一月, 某一年的数据