文章来自:http://blog.csdn.net/jh_zzz/archive/2010/01/11/5173806.aspx,由于我也正在看typecho这个玩意所以,就记录下来。虽然我一天下来,也看了不少代码,但毕竟没有深读过。对于流程啥的,还没有开始关心,只是为了写而写。

第一篇:页面渲染及路由机制

从 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

if (! $validated && ! emptyempty ($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();