首页 > typecho的路由规则是啥?

typecho的路由规则是啥?

Typecho/Router/Parser.php中的parse方法里面的正则表达式是啥意思?

附代码:

public function parse()
    {
        $result = array();

        foreach ($this->_routingTable as $key => $route) {
            $this->_params = array();
//在_routingTables,[regx] => |^/attachment/([0-9]+)[/]?$|
            $route['regx'] = preg_replace_callback("/%([^%]+)%/", array($this, '_match'),
// [url] => /attachment/[cid:digital]/
            preg_quote(str_replace(array('[', ']', ':'), array('%', '%', ' '), $route['url'])));

            /** 处理斜线 */
            $route['regx'] = rtrim($route['regx'], '/');
            $route['regx'] = '|^' . $route['regx'] . '[/]?$|';

            $route['format'] = preg_replace("/\[([^\]]+)\]/", "%s", $route['url']);
            $route['params'] = $this->_params;

            $result[$key] = $route;
        }

        return $result;
}

Typecho的路由实现是保存在数据库中的路由表,使用正则匹配路径,这一点和Django框架有点像。Typecho的路由表如下数组组成,路由器类会使用子数组中的regx正则式逐个匹配pathinfo中的路径,如果匹配成功,立即初始化并执行该类对应的action。
更加具体的解析,可参考这里,有一篇关于Typecho的路由原理解析,同时还可以在这里速查路由


http://docs.typecho.org/develop/widget#何时调用

http://.com/blog/doudou/1190000000449033


这个 Parser.php 是路由解析类来的,可以看下这里
https://github.com/typecho/typecho/blob/master/var/Widget/Init.php#L54

这里指的是从数据库取出路由表(路由表可以看这里),然后使用 Parser 去匹配解析它,主要是把路由表里的一些预设定字符,转换成正则表达式,为下一步匹配 $pathInfo 做准备。具体流程,可以看下兜兜写的文章:
http://.com/blog/doudou/1190000000449033


typecho的路由还是有点意思的

http://www.phpgao.com/typecho_source_code_dispatch.html

【热门文章】
【热门文章】