Typecho 代码阅读笔记(一) - 页面渲染及路由机制

Typecho 代码阅读笔记(三) - 插件机制
Typecho 代码阅读笔记(二) - 数据库访问
Typecho 代码阅读笔记(一) - 页面渲染及路由机制

转载请注明出处:http://blog.csdn.net/jh_zzz

 

index.php 开始看,

 

/** 初始化组件 */

Typecho_Widget:: widget('Widget_Init' );

 

Typecho_Widget:: widget 函数,查找 Widget/Init.php Widget 下的文件都是从 Typecho_Widget 派生的,这里创建该对象实例,并将相关的 Request Response 对象作为参数传递过去,然后调用该对象的 execute 方法。

 

看一下 Init 中的 execute ,首先会初始化一些参数,重点看看 MVC 架构的路由:

Typecho_Router:: setPathInfo($this -> request-> getPathInfo());

Typecho_Router:: setRoutes($options -> routingTable);

首先设置路径,然后初始化路由, $options -> routingTable 默认值是在安装时写在数据库中的,运行时再读出来, Typecho_Router setRoutes 函数调用了 Typecho_Router_Parser parse 函数, parse 函数遍历整个 routingTable 数组,将处理后的路由数组返回给 Typecho_Router ,保存在 $_routingTable 中。一个典型的路由条目如下:

  (

  [url] => /

  [widget] => Widget_Archive

  [action] => render

  [regx] => |^[/]?$|

  [format] => /

  [params] => Array

  (

)

)

 

/** 注册一个初始化插件 */

Typecho_Plugin:: factory('index.php' )-> begin();

 

/** 开始路由分发 */

Typecho_Router:: dispatch();

dispatch 首先取到当前的 Url 路径信息,遍历 $_routingTable 找到匹配的路由条目,根据路由条目中的 widget action 设置加载相应 Widget 并执行其相应的方法,以 / 目录为例,最终执行的就是 Archive render 方法。

 

看一下 Widget_Archived render 方法,这块比较复杂:

首先得到设置的主题的目录:

$themeDir = __TYPECHO_ROOT_DIR__ . '/' . __TYPECHO_THEME_DIR__ . '/' . $this -> options-> theme . '/' ;

再往下看:

if (! $validated && ! empty ($this -> _archiveSlug)) {

    $themeFile = $this -> _archiveType . '/' . $this -> _archiveSlug . '.php' ;

    if (file_exists($themeDir . $themeFile )) {

        $this -> _themeFile = $themeFile ;

        $validated = true ;

    }

}

这里需要看一下 _archiveType _archiveSlug 是怎么来的:

Widget_Archived execute 函数中会根据 $this -> parameter-> type 执行相应的 handler

if (isset ($handles [$this -> parameter-> type])) {

    $handle = $handles [$this -> parameter-> type];

    $this -> {$handle }($select , $hasPushed );

} else {

    $hasPushed = $this -> pluginHandle()-> handle($this -> parameter-> type, $this , $select );

}

 

$this -> parameter-> type 这个变量是在构造函数中赋值的 :

$this -> parameter-> type = Typecho_Router:: $current ;

Typecho_Router:: $current 根据路由表可以查到对应于 index.php 就是 index

 

所以对于上面标黄的代码对应于 index.php 最终执行的是的是 $handles [‘index’] 对应的 handle ,就是 indexHandle 。可以看到对于其他的 Handle 一般都会设置 _archiveType _archiveSlug 变量, indexHandle 没有,因为 _archiveType 默认就是 index ,所以在 index.php _archiveType 等于 index _archiveSlug 为空。

 

所以根据前面 render 函数中的代码,最终是找到对应于 $themeDir 下的 $this -> _archiveType . '/' . $this -> _archiveSlug . '.php' 文件,然后直接包含进来,我们看到的就是这个文件的输出了。

 

/** 注册一个结束插件 */

Typecho_Plugin:: factory('index.php' )-> end();

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值