路由器(Route)
路由器(Route)是Typecho系统中的一个重要组件,类似mod_rewrite的机制,来实现独立的URL和指定的controller/action/params的映射规则.它通过识别诸如http://localhost/2008/10/1/hello-world
此类的伪地址,它在文件系统中并不存在但通过结合rewrite我们可以模拟出这样一个静态地址,路由器在路由表中找到匹配记录,并将用户请求导向最终应答页面.
通过这个机制,Typecho可以拥有简洁且可配置的URL并且可以摆脱对mod_rewrite的依赖.在一般的系统中,都会有一个朴素的路由系统,它们通过识别GET中的默认参数值来达到导向的目的.而在Typecho中我们通过匹配系统的伪路径来导向目的,路由表是可以配置的,并且可以动态地增加.路由基类在includes/Route.php
里面.
相关变量:
$current
:当前路由名称. $_parameters
:数组,路径解析值列表.
相关函数:
target($path)
:路由指向函数,返回根据pathinfo
和路由表配置的目的文件名.$path
为目的文件所在目录. getParameter($key)
:获取路径解析值. parse($name, array $value = NULL, $prefix = NULL)
:路由反解析.$name
为路由配置表名称,$value
为路由填充值,默认为空,$prefix
为最终合成路径的前缀,默认为空.
[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 ( )
)
[do] => Array (
[url] => /action/[action:alpha]
[widget] => Widget_Do
[action] => action [regx] => |^/action/([_0-9a-zA-Z-]+)[/]?$|
[format] => /action/%s
[params] => Array ( [0] => action )
)
[post] => Array (
[url] => /[slug].html
[widget] => Widget_Archive
[action] => render
[regx] => |^/([^/]+)\.html[/]?$|
[format] => /%s.html
[params] => Array ( [0] => slug )
)
[attachment] => Array (
[url] => /attachment/[cid:digital]/
[widget] => Widget_Archive
[action] => render
[regx] => |^/attachment/([0-9]+)[/]?$|
[format] => /attachment/%s/
[params] => Array ( [0] => cid )
)
[category] => Array (
[url] => /[slug]/
[widget] => Widget_Archive
[action] => render
[regx] => |^/([^/]+)[/]?$| [format] => /%s/
[params] => Array ( [0] => slug )
)
[tag] => Array (
[url] => /tag/[slug]/
[widget] => Widget_Archive
[action] => render
[regx] => |^/tag/([^/]+)[/]?$|
[format] => /tag/%s/ [params] => Array ( [0] => slug )
)
[author] => Array (
[url] => /author/[uid:digital]/
[widget] => Widget_Archive
[action] => render
[regx] => |^/author/([0-9]+)[/]?$|
[format] => /author/%s/
[params] => Array ( [0] => uid )
)
[search] => Array (
[url] => /search/[keywords]/
[widget] => Widget_Archive
[action] => render [regx] => |^/search/([^/]+)[/]?$|
[format] => /search/%s/
[params] => Array ( [0] => keywords )
)
[index_page] => Array (
[url] => /page/[page:digital]/
[widget] => Widget_Archive
[action] => render
[regx] => |^/page/([0-9]+)[/]?$|
[format] => /page/%s/
[params] => Array ( [0] => page )
)
[archive_page] => Array (
[url] => /blog/page/[page:digital]/
[widget] => Widget_Archive
[action] => render
[regx] => |^/blog/page/([0-9]+)[/]?$|
[format] => /blog/page/%s/
[params] => Array ( [0] => page )
)
[category_page] => Array (
[url] => /[slug]/[page:digital]/
[widget] => Widget_Archive
[action] => render
[regx] => |^/([^/]+)/([0-9]+)[/]?$|
[format] => /%s/%s/
[params] => Array ( [0] => slug [1] => page )
)
[tag_page] => Array (
[url] => /tag/[slug]/[page:digital]/
[widget] => Widget_Archive
[action] => render
[regx] => |^/tag/([^/]+)/([0-9]+)[/]?$|
[format] => /tag/%s/%s/
[params] => Array ( [0] => slug [1] => page )
)
[author_page] => Array (
[url] => /author/[uid:digital]/[page:digital]/
[widget] => Widget_Archive
[action] => render
[regx] => |^/author/([0-9]+)/([0-9]+)[/]?$|
[format] => /author/%s/%s/
[params] => Array ( [0] => uid [1] => page )
)
[search_page] => Array (
[url] => /search/[keywords]/[page:digital]/
[widget] => Widget_Archive
[action] => render
[regx] => |^/search/([^/]+)/([0-9]+)[/]?$|
[format] => /search/%s/%s/
[params] => Array ( [0] => keywords [1] => page )
)
[archive_year] => Array (
[url] => /[year:digital:4]/
[widget] => Widget_Archive [action] => render
[regx] => |^/([0-9]{4})[/]?$|
[format] => /%s/
[params] => Array ( [0] => year )
)
[archive_month] => Array (
[url] => /[year:digital:4]/[month:digital:2]/
[widget] => Widget_Archive
[action] => render
[regx] => |^/([0-9]{4})/([0-9]{2})[/]?$|
[format] => /%s/%s/
[params] => Array ( [0] => year [1] => month )
)
[archive_day] => Array (
[url] => /[year:digital:4]/[month:digital:2]/[day:digital:2]/
[widget] => Widget_Archive
[action] => render
[regx] => |^/([0-9]{4})/([0-9]{2})/([0-9]{2})[/]?$|
[format] => /%s/%s/%s/
[params] => Array ( [0] => year [1] => month [2] => day )
)
[archive_year_page] => Array (
[url] => /[year:digital:4]/page/[page:digital]/
[widget] => Widget_Archive
[action] => render
[regx] => |^/([0-9]{4})/page/([0-9]+)[/]?$|
[format] => /%s/page/%s/
[params] => Array ( [0] => year [1] => page )
)
[archive_month_page] => Array (
[url] => /[year:digital:4]/[month:digital:2]/page/[page:digital]/
[widget] => Widget_Archive
[action] => render
[regx] => |^/([0-9]{4})/([0-9]{2})/page/([0-9]+)[/]?$|
[format] => /%s/%s/page/%s/
[params] => Array ( [0] => year [1] => month [2] => page )
)
[archive_day_page] => Array (
[url] => /[year:digital:4]/[month:digital:2]/[day:digital:2]/page/[page:digital]/
[widget] => Widget_Archive
[action] => render
[regx] => |^/([0-9]{4})/([0-9]{2})/([0-9]{2})/page/([0-9]+)[/]?$|
[format] => /%s/%s/%s/page/%s/
[params] => Array ( [0] => year [1] => month [2] => day [3] => page )
)
[comment_page] => Array (
[url] => [permalink:string]/comment-page-[commentPage:digital]
[widget] => Widget_Archive
[action] => render
[regx] => |^(.+)/comment\-page\-([0-9]+)[/]?$|
[format] => %s/comment-page-%s
[params] => Array ( [0] => permalink [1] => commentPage )
)
[feed] => Array (
[url] => /feed[feed:string:0]
[widget] => Widget_Archive
[action] => feed
[regx] => |^/feed(.*)[/]?$|
[format] => /feed%s
[params] => Array ( [0] => feed )
)
[feedback] => Array (
[url] => [permalink:string]/[type:alpha]
[widget] => Widget_Feedback
[action] => action
[regx] => |^(.+)/([_0-9a-zA-Z-]+)[/]?$|
[format] => %s/%s
[params] => Array ( [0] => permalink [1] => type )
)
[page] => Array (
[url] => /[slug].html
[widget] => Widget_Archive
[action] => render
[regx] => |^/([^/]+)\.html[/]?$|
[format] => /%s.html
[params] => Array ( [0] => slug )
)
数据库中的位置
默认的路由信息存储在数据表“typecho_options”中,字段名是“routingTable”;
select * from typecho_options where name='routingTable';
在typecho中的调用
如下语句可以将路由表打印在前端页面上,当然仅仅只是排查之用,谁也不愿意在前端看到这么一堆乱糟糟的数据。
<?php print_r(Helper::options()->routingTable);?>