路由概述
路由的功能简单的说就是根据请求(即URL)找到对应业务代码(或者说控制器,Controller),请求来自用户端,业务由系统提供,路由器的主要任务就是为请求和业务提供一对一的绑定。
目前大部分PHP框架都有路由的概念,譬如ThinkPHP或者CodeIgniter,只不多彼此的实现不太一样。
CodeIgniter中MVC的概念非常强,而Typecho则感觉有点弱化此概念,而采用“Widget”取而代之,即CodeIgniter中Router是将请求路由到Controller,而Typecho中,是将请求路由到Widget。
原理分析
Typecho的路由实现是保存在数据库中的路由表,使用正则匹配路径,这一点和Django框架有点像。Typecho的路由表如下数组组成,路由器类会使用子数组中的regx正则式逐个匹配pathinfo中的路径,如果匹配成功,立即初始化并执行该类对应的action。
以index为例,如果使用正则表达式匹配成功,系统就会新建Widget_Archive类并执行其render方法,看他的名字就知道执行的是渲染页面的方法,也就是之前说的所谓业务。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
Array ( [index] => Array ( [url] => / [widget] => Widget_Archive [action] => render [regx] => |^[/]?$| [format] => / [params] => Array ( ) ) [archive] => Array ( [url] => /blog/ [widget] => Widget_Archive [action] => render [regx] => |^/blog[/]?$| [format] => /blog/ [params] => Array ( ) ) # .... |
更详细的路由表,可以参考这里
Typecho路由速查将Typecho的默认系统路由打出来,方便调试代码时速查~~ [index] => Array ( [url] => / [widget] => Widget_Archive [ac...
时间:2021/4/7 分类:建站教程 人气:802 评论:0
自定义路由
Typecho的路由是保存在MySQL中的,而并非类似CodeIgniter,有特定的匹配模式,此时可能有筒子会质疑,这样的实现会不会对自定义路由非常不友好,不够灵活。
实际上,Typecho是支持自定义路由的,大家可以自由扩展路由表。有兴趣的同学可以去看看Helper类中addRoute()
和removeRoute()
的用法。举个例子,如果你需要自定义一个URL,比如www.typechodev.com/my_page.html,就可以在路由表加入如下路由:
1 |
Helper::addRoute('my_page','my_page.html','My_Plugin_Action','render') //不用担心自定义Widget的路径问题,typecho系统会自动帮你找到。 |
实际添加的路由为:
1 2 3 4 5 6 |
[my_page] => Array ( [url] => /my_page.html [widget] => My_Plugin_Action [action] => render ) |
Helper中addRoute定义:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
/** * 增加路由 * * @access public * @param string $name 路由名称 * @param string $url 路由路径 * @param string $widget 组件名称 * @param string $action 组件动作 * @param string $after 在某个路由后面 * @return integer */ public static function addRoute($name, $url, $widget, $action = NULL, $after = NULL) { // some code here... |
Helper中removeRoute:
1 2 3 4 5 6 7 8 9 10 |
/** * 移除路由 * * @access public * @param string $name 路由名称 * @return integer */ public static function removeRoute($name) { //..... |
具体加入自定义路由的时机,可以参考《Typecho插件开发入门》