首 页  >>  E商学院  >>  zencart百科  >>  zen cart前端控制分析

zen cart前端控制分析

字号: 小 中 大
zen cart 并没有按照 MVC
模式进行设计,
但的确有点这个影子, 前台系统就只有一个入口,
就是根目录下面的index.php文件,
可以把这个文件美其名为前端 控制器, 前端调度等, 因为它的确是总控制台.
在访问zen cart的资源时,
一般都是这种连接
http://zcmb.net/index.php?main_page=xxx,
由此可知, 页面的构建,
是由index.php完成,
至于弄成啥样, 有main_page决定.


看一下这个文件源码:


//环境构建, 由application_top.php初始化一起, 会加载大量内容 require('includes/application_top.php'); //记录语言包目录 如:includes/languages/english/ $language_page_directory = DIR_WS_LANGUAGES . $_SESSION['language'] . '/'; //$code_page_directory 在系统初始化时由main_page参数决定, 如果是首页那么它的值就是index, get_template_part()函数会进入includes/modules/pages/$code_page_directory/目录里面查找以header_php的文件返回数组, 然后全部加载进来, 针对每个页面导入PHP脚本.  $directory_array = $template->get_template_part($code_page_directory, '/^header_php/'); foreach ($directory_array as $value) { require($code_page_directory . '/' . $value); } //到includes/templates/当前模板/common/目录里面加载html_header.php, 这个文件负责标签的内容, 比如链接CSS Javascript等信息, 全部交给这个文件处理, 它是先进入$current_page_base目录, 所以这个逻辑可以针对页面定制, 只要在 当前模板目录/当前页面目录/里面放入自己的html_header.php文件即可 require($template->get_template_dir('html_header.php',DIR_WS_TEMPLATE, $current_page_base,'common'). '/html_header.php'); //到includes/templates/当前模板/common/目录里面加载main_template_vars.php, 这个文件决定加载那个主内容模板, 这个文件特殊的地方在于它可能又跑回到includes/modules/pages/当前页目录/ 里面重复查找自身, 如果找到就用找到的这个文件的逻辑加载主内容模板, 否则就到模板下面的templates目录查找tpl_当前页面_default.php这个模板, 可以知道, includes/modules/pages/当前页目录/里面如果有main_template_vars.php, 只是更加有效地控制主内容模板的加载, 比如根据第二个参数加载另一个模板 require($template->get_template_dir('main_template_vars.php',DIR_WS_TEMPLATE, $current_page_base,'common'). '/main_template_vars.php'); //以下代码都是在获取on_load_开头的js文件的内容, 读入一个$za_onload_array的数组中, 最终变成字符串放入到$zv_onload中, 这个字符串会作为body的onload属性的值, 这里还在使用onload视乎也是历史原因 $directory_array = $template->get_template_part(DIR_WS_MODULES . 'pages/' . $current_page_base, '/^on_load_/', '.js'); foreach ($directory_array as $value) { $onload_file = DIR_WS_MODULES . 'pages/' . $current_page_base . '/' . $value; $read_contents=''; $lines = @file($onload_file); foreach($lines as $line) { $read_contents .= $line; } $za_onload_array[] = $read_contents; } $directory_array=array(); $tpl_dir=$template->get_template_dir('.js', DIR_WS_TEMPLATE, 'jscript/on_load', 'jscript/on_load_'); $directory_array = $template->get_template_part($tpl_dir ,'/^on_load_/', '.js'); foreach ($directory_array as $value) { $onload_file = $tpl_dir . '/' . $value; $read_contents=''; $lines = @file($onload_file); foreach($lines as $line) { $read_contents .= $line; } $za_onload_array[] = $read_contents; } if (isset($zc_first_field) && $zc_first_field !='') $za_onload_array[] = $zc_first_field; $zv_onload = ""; if (isset($za_onload_array) && count($za_onload_array)>0) $zv_onload=implode(';',$za_onload_array); $zv_onload = str_replace(';;',';',$zv_onload.';'); if (trim($zv_onload) == ';') $zv_onload=''; //这是布局文件, 多少列, 页头, 页脚, 边框等, 每一个页面的布局都是可以定制的, 可以在common/tpl_main_page.php文件中统一控制, 也可以在页面目录里面单独放入tpl_main_page.php针对当前页面进行布局, 好好利用这个特征, zen cart也是无限可能的. require($template->get_template_dir('tpl_main_page.php',DIR_WS_TEMPLATE, $current_page_base,'common'). '/tpl_main_page.php');

最后在加载application_bottom.php, 来做一些清理工作.

zen cart 由osc发展而来, 在它之上有了很大的发展,
但是环境初始化开销很大是一点都没有改善,
而且把程序拆分成很多的小片段放入到独立文件中,
增加了读取搜索的文件的次数, 可想而知, 带来了灵活性的同时, 必定在降低性能,
这也是Zen
Cart一直所诟病的地方, 不过好在 zen cart原本就定位于一个小型网店系统.

最后补个基础链接 什么是 MVC
点击次数:1314  发布日期:2013-04-15  【打印此页】  【关闭