emlog结构解析 MVC的显示方式及模板的调度过程

emlog博客简单的风格,轻松的体验大获好评,作为一个php学习的新手,emlog源码中有很多值得学习的地方

首先要学习的是emlog MVC设计模式,(听说emlog没有用框架是自己写的),MVC即使model view Controller的缩写,是模型(model)-视图(view)-控制器(controller)的缩写

 

但严格来说,emlog只有MC没有Vemlogview类似于WordPress的,模板都是由.php组成,里面调用内置的函数。

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

 

所以所有的类都在include这个目录下

 

 

首页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内置的变量

后面的

include View::getView('header');

include View::getView('log_list');

就是调用的我们博客模板了。

其他都是类似

 

另外说下emlog源码有空行的解决办法

Emlog即使在默认的default模板上也有一行空白

 

 

虽然没什么大碍,但强迫症很可怕。。

解决办法:

mothod.php 搜索 widget_link

然后到结尾 或者直接调到默认模板第219

 

会发现后面就个空格,删除掉即可

 

版权属于emlog大博客 www.adfun.cn 未经允许不得转载emlog大博客 » emlog结构解析 MVC的显示方式及模板的调度过程


分享到:更多 ()

暂无评论

  • QQ (选填)
  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址