欢迎光临
我们一直在努力

ZenCart深入二次开发-核心文件说明

想对ZenCart深入了解的程序猿可以认真看看zencart的文件结构…

1.大家可以看到ZenCart根目录index.php文件中第一句话是包含include目录下的application_top.php文件,如:require(’includes/application_top.php’);

zencart系统中application_top.php负责的是初始化工作,比如加载配置文件include(’includes/configure.php’);,如果系统没检测到该文件的存在则会尝试调用安装文件。

然后它会自动遍历include/extra_configures下的配置文件并包含进来。

在加载了系统配置文件以后接下来是一个非常重要的文件,这也导致了zencart和oscommerce感觉上很大不同的原因,首先调用一个文件require(’includes/initsystem.php’);
在initsystem.php中最先加载include/auto_loaders/config.core.php,config.core.php 是一个二围数组$autoLoadConfig,即以数组的形式保存文件的信息供后面文件调用,然后系统会接着加载完 include/auto_loaders目录下所有文件名匹配$loaderPrefix(默认为config)的文件。

上面程序执行完以后就是加载自动执行程序了require(’includes/autoload_func.php’);在这里它会遍 历$autoLoadConfig数组,它最终执行的效果会包含所有必须用到的函数或者类的定义,还有变量的初始化,config.core.php里面 的注释比较清楚比如

$autoLoadConfig[0][] = array(’autoType’=>’class’,’loadFile’=>’class.base.php’);

在autoload_func.php里面执行完以后的效果就是require(DIR_WS_CLASSES . ‘class.base.php’),大部分的初始化化工作是通过包含init_includes目录下的文件来实现的,如:

$autoLoadConfig[110][] = array(’autoType’=>’init_script’,’loadFile’=> ‘init_templates.php’);

它在执行完autoload_func.php文件后就已经加载了init_includes目录下的init_templates.php文件。

 

2.下面来介绍下ZenCart是怎么根据摸版把内容显示出来的。
在index.php的第29行有句

$directory_array = $template->get_template_part($code_page_directory, ‘/^header_php/’);

由于所有初始化工作已经完成,所以我们就可以在上面的文件找到他们的定义,如
$autoLoadConfig[100][] = array(’autoType’=>’classInstantiate’,’className’=>’template_func’,’objectName’=>’template’);

在这里就定义了$template = new template_func(); ,然后$code_page_directory变量的定义是在init_includes/init_sanitize.php文件中定义在这里必须要 对class/template_func.php中定义的template_func类比较熟悉,在该类中主要定义了两个方法 get_template_dir()和get_template_part();
这两个方法在zencart的模板使用中起到了决定性的作用。

get_template_dir方法function get_template_dir($template_code, $current_template, $current_page, $template_dir, $debug=false),它定义了5个参数,第一个参数一般是个文件名,它是用来判断后两个参数组成的目录中有没有匹配$template_code 的这个文件,该类复写了默认的系统函数file_exists所以很多初学者可能会比较迷惑

function get_template_dir($template_code, $current_template, $current_page, $template_dir, $debug=false) {
//echo ‘template_default/’ . $template_dir . ‘=’ . $template_code;

if($this->file_exists($current_template . $current_page, $template_code)){
return $current_template . $current_page . ‘/’;
}elseif ($this->file_exists(DIR_WS_TEMPLATES . ‘template_default/’ . $current_page, ereg_replace(’/’, ”, $template_code), $debug)){
return DIR_WS_TEMPLATES . ‘template_default/’ . $current_page;
} elseif ($this->file_exists($current_template . $template_dir, ereg_replace(’/’, ”, $template_code), $debug)){
return $current_template . $template_dir;
} else {
return DIR_WS_TEMPLATES . ‘template_default/’ . $template_dir;
//return $current_template . $template_dir;
}
}

/*

includes/templates/zccn/index

includes/templates/template_default/index

includes/templates/zccn/common

includes/templates/template_default/common

*/

get_template_part()方法有两个函数,第一个参数是文件目录,第二个参数是匹配的条件,执行的结果是包含该目录下所有文件名匹配这个条件的文件

比如$directory_array = $template->get_template_part($code_page_directory, ‘/^header_php/’);

这句话执行的结果就是返回目录下$code_page_directory所有文件名以header_php开头的文件

如此时的url(http://localhost/zencart/index.php?main_page=product_info&cPath=49_27&products_id=83)

你现在应该查看init_sanitize.php中$code_page_directory的定义此时的$code_page_directory的值应该是includes/modules/product_info/

所以它就应该包含该目录下所有以header_php开头的文件,在这里好象就只有一个header_php.php

$directory_array = $template->get_template_part($code_page_directory, ‘/^header_php/’);这个包含文件其实是初始化前台不同页面显示所需要用到的变量函数,主要是初始化数据库的东西,因为每个页面需要的数据 资料都有可能不同,所以index.php?main_page=index 当main_page的值不同是在includes/modules/目录下都会有个对应的目录,这里是index目录

只要知道了这两个方法的用法,你就会知道模板文件都是怎么显示出来的了

 

再来解释一 require($template->get_template_dir(’html_header.php’,DIR_WS_TEMPLATE, $current_page_base,’common’). ‘/html_header.php’);

假设当前url:http://localhost/zencart/index.php?main_page=index&cPath=48

DIR_WS_TEMPLATE 定义是在includes/init_templates.php中定义define(’DIR_WS_TEMPLATE’, DIR_WS_TEMPLATES . $template_dir . ‘/’);,因为我现在用的是默认的zccn模板

所以现在的DIR_WS_TEMPLATE=includes/templates/zccn/

$current_page_base在这里已经就是index

上面已经解释了$template->get_template_dir()的方法了

程序会依次在
includes/templates/zccn/index
includes/templates/template_default/index
includes/templates/zccn/common
includes/templates/template_default/common

在以上四个目录中找html_header.php,最终在template_default/common目录下找到html_header.php,最先优先当前模板的。

未经允许不得转载:外贸商城系统,外贸网站模板,php建站教程,zencart模板 » ZenCart深入二次开发-核心文件说明

分享到:更多 ()

35PHP 更全 更专业 更方便