模板的调度过程

emlog使用的是类似MVC的设计模式,将功能和模板分开了

自动加载

在文件/include/lib/function.base.php

/**
 * 基础函数库
 * @copyright (c) Emlog All Rights Reserved
 */
function __autoload($class) {
    $class = strtolower($class);
    if (file_exists(EMLOG_ROOT . '/include/model/' . $class . '.php')) {
        require_once(EMLOG_ROOT . '/include/model/' . $class . '.php');
    } elseif (file_exists(EMLOG_ROOT . '/include/lib/' . $class . '.php')) {
        require_once(EMLOG_ROOT . '/include/lib/' . $class . '.php');
    } elseif (file_exists(EMLOG_ROOT . '/include/controller/' . $class . '.php')) {
        require_once(EMLOG_ROOT . '/include/controller/' . $class . '.php');
    } else {
        emMsg($class . '加载失败。');
    }
}

Emlog加载时调用了_autoload 这个函数的功能是自动寻找调用的Class

模板显示过程

首页index.php中的代码

define('TEMPLATE_PATH', TPLS_PATH.Option::get('nonce_templet').'/');//前台模板路径
$emDispatcher = Dispatcher::getInstance();
$emDispatcher->dispatch();
View::output();

是模板显示的关键,,转到Dispatcher这个类就可以看到实现的方法

public static function getInstance() {//大概功能就是创建这个类
        if(self::$_instance == null) {
            self::$_instance = new Dispatcher();
            return self::$_instance;
        } else {
            return self::$_instance;
        }
    }

    private function __construct() {//初始化就调用的类
        $this->_path = $this->setPath();
        $this->_routingTable = Option::getRoutingTable();

        $urlMode = Option::get('isurlrewrite');
        foreach ($this->_routingTable as $route) {
            if (!isset($route['reg_' . $urlMode])) {
                $reg = isset($route['reg']) ? $route['reg'] : $route['reg_0'];
            } else {
                $reg = $route['reg_' . $urlMode];
            }
            if (preg_match($reg, $this->_path, $matches)) {
                $this->_model = $route['model'];
                $this->_method = $route['method'];
                $this->_params = $matches;
                break;
            } elseif (preg_match($route['reg_0'], $this->_path, $matches)) {
                $this->_model = $route['model'];
                $this->_method = $route['method'];
                $this->_params = $matches;
                break;
            }
        }

        if (empty($this->_model)) {
            show_404_page();
        }
    }

    public function dispatch(){
        $module = new $this->_model();
        $method = $this->_method;
        $module->$method($this->_params);
}

这个函数实现了不同的路由可以调用不同的类来显示界面
$this->_routingTable = Option::getRoutingTable(); 存储了这个对应的列表

 static function getRoutingTable(){
$routingtable = array(
array(
'model' => 'calendar',
'method' => 'generate',
'reg_0' => '|^.*/\?action=cal|',
),
array(
'model' => 'Log_Controller',
'method' => 'displayContent',
'reg_0' => '|^.*/\?(post)=(\d+)(&(comment-page)=(\d+))?([\?&].*)?$|',
'reg_1' => '|^.*/(post)-(\d+)\.html(/(comment-page)-(\d+))?/?([\?&].*)?$|',
'reg_2' => '|^.*/(post)/(\d+)(/(comment-page)-(\d+))?/?$|',
'reg_3' => '|^/([^\./\?=]+)(\.html)?(/(comment-page)-(\d+))?/?([\?&].*)?$|',
),
array(
'model' => 'Record_Controller',
'method' => 'display',
'reg_0' => '|^.*/\?(record)=(\d{6,8})(&(page)=(\d+))?([\?&].*)?$|',
'reg' => '|^.*/(record)/(\d{6,8})/?((page)/(\d+))?/?([\?&].*)?$|',
),
array(
'model' => 'Sort_Controller',
'method' => 'display',
'reg_0' => '|^.*/\?(sort)=(\d+)(&(page)=(\d+))?([\?&].*)?$|',
'reg' => '|^.*/(sort)/([^\./\?=]+)/?((page)/(\d+))?/?([\?&].*)?$|',
),
array(
'model' => 'Tag_Controller',
'method' => 'display',
'reg_0' => '|^.*/\?(tag)=([^&]+)(&(page)=(\d+))?([\?&].*)?$|',
'reg' => '|^.*/(tag)/([^/?]+)/?((page)/(\d+))?/?([\?&].*)?$|',
),
array(
'model' => 'Author_Controller',
'method' => 'display',
'reg_0' => '|^.*/\?(author)=(\d+)(&(page)=(\d+))?([\?&].*)?$|',
'reg' => '|^.*/(author)/(\d+)/?((page)/(\d+))?/?([\?&].*)?$|',
),
array(
'model' => 'Log_Controller',
'method' => 'display',
'reg_0' => '|^.*/\?(page)=(\d+)([\?&].*)?$|',
'reg' => '|^.*/(page)/(\d+)/?([\?&].*)?$|',
),
array(
'model' => 'Search_Controller',
'method' =>'display',
'reg_0' => '|^.*/\?(keyword)=([^/&]+)(&(page)=(\d+))?([\?&].*)?$|',
),
array(
'model' => 'Comment_Controller',
'method' => 'addComment',
'reg_0' => '|^.*/\?(action)=(addcom)([\?&].*)?$|',
),
array(
'model' => 'Plugin_Controller',
'method' => 'loadPluginShow',
'reg_0' => '|^.*/\?(plugin)=([\w\-]+).*([\?&].*)?$|',
),
array(
'model' => 'Log_Controller',
'method' => 'displayContent',
'reg_0' => '|^.*?/([^/\.=\?]+)(\.html)?(/(comment-page)-(\d+))?/?([\?&].*)?$|',
),
array(
'model' => 'Log_Controller',
'method' => 'display',
'reg_0' => '|^/?([\?&].*)?$|',
),
);
return $routingtable;
}

上面函数就是具体的调用函数了,'model' 是class类,'method' 是显示方式,'reg_'是通过地址的正则,想修改自定义url的修改这里就可以了。通过这种方法就直接调用了控制器,通过控制器看到首页的控制方法
前面的内置变量可以认为是emlog提供给我们的“API”

include View::getView('header');

include View::getView('log_list');

就是调用我们的模版了

results matching ""

    No results matching ""