当前位置:网站首页>Introduction to ThinkPHP URL routing
Introduction to ThinkPHP URL routing
2022-07-07 16:21:00 【Full stack programmer webmaster】
To put it simply ,URL Routing allows you to customize what you need under certain rules URL Looks like , To beautify URL , Improve user experience , It is also beneficial to the purpose of search engine collection .
Example
The original URL by :
http://www.5idev.com/index.php/Products/Show/category/5/id/123The URL The original intention of is to show 5 In the classification id by 123 Products . after URL After route rewriting URL It can be for :
http://www.5idev.com/index.php/product/5/123If Use .htaccess Of documents Rewrite The rules hide the entry file , The top URL It can be further simplified to :
http://www.5idev.com/product/5/123This URL The address is relatively simple and easy to look .
Tips : Use Apache Of URL Rewrite Rules can also be achieved URL Customized functions , It's not going to unfold here , For those interested, please refer to Apache Rewrite Related articles .
ThinkPHP URL Routing configuration
stay ThinkPHP To be used in URL Routing functions , The following configuration is required :
stay Project configuration file Conf/config.php Inside, the routing function is enabled ( Set to true):
'URL_ROUTER_ON' => true,Routing rule definition
And 2.x Different versions ,3.0 Routing rules are defined in the project configuration file config.php Inside , The format is array , The specific definition rules are divided into rule route and regular route . The syntax of rule routing is as follows :
Format 1:' Routing rules '=>'[ grouping / modular / operation ]? Extra parameters 1= value 1& Extra parameters 2= value 2...'
Format 2:' Routing rules '=>array('[ grouping / modular / operation ]',' Extra parameters 1= value 1& Extra parameters 2= value 2...')
Format 3:' Routing rules '=>' External address '
Format 4:' Routing rules '=>array(' External address ',' Redirection code ') Syntax description
- The routing rule is that we should URL Rules shown in , The following element values are actual URL Address and parameters
- In the routing rule, if : start , Represents a dynamic variable , Otherwise, it is a static address
- Format 2 The additional parameters of can be passed into array or string
- Routing rules support numerical constraint definitions of variables , for example :’product/:id\d’=>’Products/Show’
- Non numeric variables of routing rules support exclusion , for example ‘news/:cate^add|edit|delete’=>’News/category’
- Routing rules support complete matching definitions , for example :’product/:id\d$’=>’Products/Show’
- The static address part of the routing rule is not case sensitive
- If you want to reference dynamic variables in the external address , use :1、:2 The way
- Regular routing can support Full dynamic and dynamic static combination definition , for example ‘:user/blog/:id’=>’Home/Blog/user’
These rules and grammatical descriptions are rather obscure , The following will be examples to compare in order to understand the above routing rules and syntax instructions .
If the routing enable function is defined in the configuration file , The system is executing Dispatch When parsing , Will judge the present URL Whether there is a defined route name , If there is, it will follow the defined routing rules URL analysis .
ThinkPHP URL Routing instance
Take the example at the beginning of this article , See how the route is defined . In the project profile Conf/config.php The following rules are defined in :
// Route definition
'URL_ROUTE_RULES'=> array(
'product/:category\d/:id\d'=>'Products/Show', // Rule routing
),When we visit the following address :
http://www.5idev.com/index.php/product/5/123The address will be resolved to Products Modular Show operation , And pass in get Parameters category=5&id=123.
If there are additional fixed parameters , Such as status=1, You can define routes :
'product/:category\d/:id\d'=>'Products/Show?status=1', // Rule routing That is, match the following URL Address :
http://www.5idev.com/index.php/product/5/123/1The above is in the format 1 To define the route , With additional parameters , It can be converted to 2 Define... Formats :
'product/:category\d/:id\d'=>array('Products/Show','status=1') In the above routing rules \d Indicates that only numbers are matched , When this constraint is not applied , Then all characters can be matched , This is also the default . If you want to strictly agree on the format of the incoming parameters , Please use Regular route definition rules .
Routing format : External address
For routing format 3 And format 4, The matching route format is detected , Then jump to the external address , The difference is format 4 There is redirection code , Such as 301 Represents a permanent redirect .
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/113177.html Link to the original text :https://javaforall.cn
边栏推荐
- 喜讯!科蓝SUNDB数据库与鸿数科技隐私数据保护管理软件完成兼容性适配
- Detailed explanation of several ideas for implementing timed tasks in PHP
- You Yuxi, coming!
- Shandong old age Expo, 2022 China smart elderly care exhibition, smart elderly care and aging technology exhibition
- Shader_ Animation sequence frame
- 47_ Contour lookup in opencv cv:: findcontours()
- Bidding announcement: Fujian Rural Credit Union database audit system procurement project (re bidding)
- 企业级日志分析系统ELK
- Balanced binary tree (AVL)
- 95. (cesium chapter) cesium dynamic monomer-3d building (building)
猜你喜欢

PyTorch 中的乘法:mul()、multiply()、matmul()、mm()、mv()、dot()

喜讯!科蓝SUNDB数据库与鸿数科技隐私数据保护管理软件完成兼容性适配

过度依赖补助,大客户收款难,冲刺“国产数据库第一股”的达梦后劲有多足?

2022 the 4th China (Jinan) International Smart elderly care industry exhibition, Shandong old age Expo

You Yuxi, coming!
通知Notification使用全解析

Apache Doris刚“毕业”:为什么应关注这种SQL数据仓库?

Dotween -- ease function

山东老博会,2022中国智慧养老展会,智能化养老、适老科技展

AE learning 02: timeline
随机推荐
Apache Doris just "graduated": why should we pay attention to this kind of SQL data warehouse?
Common training data set formats for target tracking
PHP实现微信小程序人脸识别刷脸登录功能
Logback日志框架第三方jar包 免费获取
There are many ways to realize the pause function in JS
A JS script can be directly put into the browser to perform operations
laravel中将session由文件保存改为数据库保存
华东师大团队提出,具有DNA调控电路的卷积神经网络的系统分子实现
JS 模块化
平衡二叉树(AVL)
Xingruige database was shortlisted as the "typical solution for information technology application and innovation in Fujian Province in 2021"
Step by step monitoring platform ZABBIX
Rongyun won the 2022 China Xinchuang digital office portal excellence product award!
Numpy --- basic learning notes
招标公告:2022年云南联通gbase数据库维保公开比选项目(第二次)比选公告
How to query the data of a certain day, a certain month, and a certain year in MySQL
TiDB For PostgreSQL和YugabyteDB在Sysbench上的性能对比
Continuous creation depends on it!
[excelexport], Excel to Lua, JSON, XML development tool
招标公告:盘锦市人民医院盘锦医院数据库维保项目