您的位置:首页 > 运维架构 > 网站架构

discuz X论坛技术架构 MVC结构浅析

2016-06-15 18:28 453 查看
摘自:http://yeyuan.iteye.com/blog/930727

PS:本人刚接触discuz论坛,php水平有限,当中的理解,如有不正确之处,欢迎指出 

------------------------------------

第一个文件相当于控制器(C),比如forum.php(根目录下,相当于大模块,应该再加上小模块控制 module),功能是将相应的请求发送到相应的逻辑处理模块

第二个文件就是业务逻辑处理 如forum_post.php(source\module\forum\forum_post.php,Module相当于小模块、功能点) ,功能是帖子的发布处理        

  在第二个文件当中再调用相应的函数库,处理模块业务 如function_forum.php(source\function\function_forum.php)        

  函数库则调用数据库持久层的表对象(M)  在class/table 下的数据表对象与数据库发生交互,比如source\class\table\table_forum_post.php

第三个文件就是模板文件了(V),将获取到得数据填充到页面各模块分工明细,维护简单

如果需要添加新的模块,可以仿造这种模式,而不会太复杂大致是这个样子的

------------------------------------

依我的理解,discuz的MVC结构是这样的:

Model即逻辑处理,应该是source/function,这里面的一些函数是对数据库(表对象class/table),缓存,内存,配置等一些的相关操作。 
Control即控制器,应该是source/module对应相关的模块,比如门户的相关操作,就在portal文件夹下,论坛的相关操作是在forum文件夹下。 
View模板即最终呈现给用户看的则是template()这个函数,稍后可以简单的说下这个函数的相关过程。 

接下来说下执行的相关流程,先看下一些代码

define('APPTYPEID', 4);   

define('CURSCRIPT', 'portal');   

  

require './source/class/class_core.php';//这个文件是核心文件,初始化工作是在这里进行的。   

$discuz = & discuz_core::instance//实例化对象,这里是一个单件模式   

$cachelist = array('userapp', 'portalcategory');
  

$discuz->cachelist = $cachelist;//声明缓存列表   

$discuz->init();   

//进行初始化,环境检查,读取配置,设置内存等   

  

  

require DISCUZ_ROOT.'./source/function/function_home.php';
  

require DISCUZ_ROOT.'./source/function/function_portal.php';
  

//包含protal.php对应的核心函数文件   

if(emptyempty($_GET['mod']) || !in_array($_GET['mod'], array('list', 'view', 'comment', 'portalcp', 'topic', 'attachment'))) $_GET['mod'] = 'index';
  

检查mod,是否在mod列表里,如果不在或者不对应,则默认为index   

define('CURMODULE', $_GET['mod']);
  

runhooks();//这个是用来检查加载插件的   

  

$navtitle = str_replace('{bbname}', $_G['setting']['bbname'], $_G['setting']['seotitle']['portal']);
  

  

require_once libfile('portal/'.$_GET['mod'], 'module');
  

//这个是用来加截source/module下的对应文件的。  

define('APPTYPEID', 4);
define('CURSCRIPT', 'portal');

require './source/class/class_core.php';//这个文件是核心文件,初始化工作是在这里进行的。
$discuz = & discuz_core::instance//实例化对象,这里是一个单件模式
$cachelist = array('userapp', 'portalcategory');
$discuz->cachelist = $cachelist;//声明缓存列表
$discuz->init();
//进行初始化,环境检查,读取配置,设置内存等

require DISCUZ_ROOT.'./source/function/function_home.php';
require DISCUZ_ROOT.'./source/function/function_portal.php';
//包含protal.php对应的核心函数文件
if(empty($_GET['mod']) || !in_array($_GET['mod'], array('list', 'view', 'comment', 'portalcp', 'topic', 'attachment'))) $_GET['mod'] = 'index';
检查mod,是否在mod列表里,如果不在或者不对应,则默认为index
define('CURMODULE', $_GET['mod']);
runhooks();//这个是用来检查加载插件的

$navtitle = str_replace('{bbname}', $_G['setting']['bbname'], $_G['setting']['seotitle']['portal']);

require_once libfile('portal/'.$_GET['mod'], 'module');
//这个是用来加截source/module下的对应文件的。


接下来我们可以看下libfile()这个函数

//该文件在source/function/function.core.php   

//按上面的传入两个参数libfile("portal/index","module")   

function libfile($libname, $folder = '') {
  

    $libpath = DISCUZ_ROOT.'/source/'.$folder;
  

//$libpath = "disucz/source/module"   

    if(strstr($libname, '/')) {
  

        list($pre, $name) = explode('/', $libname);
  

        return realpath("{$libpath}/{$pre}/{$pre}_{$name}.php");
  

    } else {   

        return realpath("{$libpath}/{$libname}.php");
  

    }   

//$libname=protal/protal_index.php   

//那么返回的文件就应该是disucz/source/module/protal/protal_index.php   

}  

//该文件在source/function/function.core.php
//按上面的传入两个参数libfile("portal/index","module")
function libfile($libname, $folder = '') {
$libpath = DISCUZ_ROOT.'/source/'.$folder;
//$libpath = "disucz/source/module"
if(strstr($libname, '/')) {
list($pre, $name) = explode('/', $libname);
return realpath("{$libpath}/{$pre}/{$pre}_{$name}.php");
} else {
return realpath("{$libpath}/{$libname}.php");
}
//$libname=protal/protal_index.php
//那么返回的文件就应该是disucz/source/module/protal/protal_index.php
}


那么我们就来看下protal_index.php这个文件

if(!defined('IN_DISCUZ')) {
  

    exit('Access Denied');   

}   

//上面是用来检查discuz核心文件是否加载,   

$navtitle = str_replace('{bbname}', $_G['setting']['bbname'], $_G['setting']['seotitle']['portal']);
  

if(!$navtitle) {
  

    $navtitle = $_G['setting']['navs'][1]['navname'];
  

} else {   

    $nobbname = true;   

}   

  

$metakeywords = $_G['setting']['seokeywords']['portal'];
  

if(!$metakeywords) {
  

    $metakeywords = $_G['setting']['navs'][1]['navname'];
  

}   

  

$metadescription = $_G['setting']['seodescription']['portal'];
  

if(!$metadescription) {
  

    $metadescription = $_G['setting']['navs'][1]['navname'];
  

}   

上面是一些文件头信息,   

include_once template('diy:portal/index');  

if(!defined('IN_DISCUZ')) {
exit('Access Denied');
}
//上面是用来检查discuz核心文件是否加载,
$navtitle = str_replace('{bbname}', $_G['setting']['bbname'], $_G['setting']['seotitle']['portal']);
if(!$navtitle) {
$navtitle = $_G['setting']['navs'][1]['navname'];
} else {
$nobbname = true;
}

$metakeywords = $_G['setting']['seokeywords']['portal'];
if(!$metakeywords) {
$metakeywords = $_G['setting']['navs'][1]['navname'];
}

$metadescription = $_G['setting']['seodescription']['portal'];
if(!$metadescription) {
$metadescription = $_G['setting']['navs'][1]['navname'];
}
上面是一些文件头信息,
include_once template('diy:portal/index');


这个template函数,代码比较长,我就不贴了,大致说下功能。 

template主要的功能是用来生成缓存文件的名字,只是用来生成这个名字,实际并未生成,真正生成的是template函数最后的那个checktplrefresh(),看名字,应该猜得出,是检查模板是否更新 

看下checktplrefresh()这个函数

function checktplrefresh($maintpl, $subtpl, $timecompare, $templateid, $cachefile, $tpldir, $file) {
  

    static $tplrefresh, $timestamp;
  

    if($tplrefresh === null) {
  

        $tplrefresh = getglobal('config/output/tplrefresh');
  

        $timestamp = getglobal('timestamp');
  

    }   

//上面的那段我还不知道是干啥来着,   

    if(emptyempty($timecompare) || $tplrefresh == 1 || ($tplrefresh > 1 && !($timestamp % $tplrefresh))) {
  

        if(emptyempty($timecompare) || @filemtime(DISCUZ_ROOT.$subtpl) > $timecompare) {
  

            require_once DISCUZ_ROOT.'/source/class/class_template.php';
  

            $template = new template();
  

            $template->parse_template($maintpl, $templateid, $tpldir, $file, $cachefile);
  

            return TRUE;   

        }   

    }   

    return FALSE;   

}   

下面的这个判断主要是看是否在缓存时间内,如果在缓存时间内,则返回false,直接包含之前生成的缓存文件,如果不在缓存时间之后,则进行重新解析。完了之后,就会执行解析好的php缓存文件。显示到前台,大家可以看下parse_template()这个函数用了很正则去解析模板。这个就不多介绍了,大家可以去看下。   

  

由此以来,先是调用source/module/下的相关文件进行读取数据库或者是读取缓存数据的相关功能把,相关变量赋值然后用template和template类进行对模板解析,变量替换,然后显示到前台,大致的过程就是这样的。   

  

当然中间还有一些缓存的相关判断,这部分还在研究之中,稍候会贴出来。   

  

以上可能会有理解错误的地方,欢迎指出或补充  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  php discuz