浅解用PHP实现MVC( 四 )


* @param mixed $cache_id 缓存的ID
*/
function display($resource_name = null, $cache_id = null, $compile_id = null)
{
?
//将部分全局变量直接分配到模板中使用
$this->assign("root", rtrim($GLOBALS["root"], "/"));
$this->assign("app", rtrim($GLOBALS["app"], "/"));
$this->assign("url", rtrim($GLOBALS["url"], "/"));
$this->assign("public", rtrim($GLOBALS["public"], "/"));
$this->assign("res", rtrim($GLOBALS["res"], "/"));
?
if (is_null($resource_name)) {
$resource_name = "{$_GET["m"]}/{$_GET["a"]}." . TPLPREFIX;
} else if (strstr($resource_name, "/")) {
$resource_name = $resource_name . "." . TPLPREFIX;
} else {
$resource_name = $_GET["m"] . "/" . $resource_name . "." . TPLPREFIX;
}
Debug::addmsg("使用模板 <b> $resource_name </b>");
parent::display($resource_name, $cache_id, $compile_id);
}
?
/*
* 重载父类的Smarty类中的方法
* @param string $tpl_file 模板文件
* @param mixed $cache_id 缓存的ID
*/
function is_cached($tpl_file = null, $cache_id = null, $compile_id = null)
{
if (is_null($tpl_file)) {
$tpl_file = "{$_GET["m"]}/{$_GET["a"]}." . TPLPREFIX;
} else if (strstr($tpl_file, "/")) {
$tpl_file = $tpl_file . "." . TPLPREFIX;
} else {
$tpl_file = $_GET["m"] . "/" . $tpl_file . "." . TPLPREFIX;
}
return parent::is_cached($tpl_file, $cache_id, $compile_id);
}
?
/*
* 重载父类的Smarty类中的方法
* @param string $tpl_file 模板文件
* @param mixed $cache_id 缓存的ID
*/
?
function clear_cache($tpl_file = null, $cache_id = null, $compile_id = null, $exp_time = null)
{
if (is_null($tpl_file)) {
$tpl_file = "{$_GET["m"]}/{$_GET["a"]}." . TPLPREFIX;
} else if (strstr($tpl_file, "/")) {
$tpl_file = $tpl_file . "." . TPLPREFIX;
} else {
$tpl_file = $_GET["m"] . "/" . $tpl_file . "." . TPLPREFIX;
}
return parent::clear_cache($tpl_file = null, $cache_id = null, $compile_id = null, $exp_time = null);
}
}
比如访问登录页面
function index() {//登陆页面 $GLOBALS['debug'] = 0; $this->display(); }类Mytpl的构造方法会自动初始化Smarty的模板目录、编译目录、缓存目录等Smarty模板引擎需要的内容
$this->template_dir = APP_PATH . "views/" . TPLSTYLE; //模板目录 $this->compile_dir = PROJECT_PATH . "runtime/comps/" . TPLSTYLE . "/" . TMPPATH; //里的文件是自动生成的,合成的文件 $this->caching = CSTART; //设置缓存开启 $this->cache_dir = PROJECT_PATH . "runtime/cache/" . TPLSTYLE; //设置缓存的目录主要内容如下:
template_dir = "./admin/views/default" compile_dir = "./runtime/comps/default/admin_php/" cache_dir = "./runtime/cache/default" cache_lifetime = 604800在Login控制器中调用无参的$this->display();方法,会自动从$this->template_dir文件夹下面查找模板文件,模板文件的是保存在_GET["m"]子文件夹下的名称为_GET["a"]的文件,比如,Login控制器对应的index模板位于如下位置:
最后使用Smarty模板引擎完成页面内容的渲染工作,最终把编译后的模板文件保存在$this->compile_dir目录下面,如下所示:
3.4、模型(Model)
模型层分为业务模型和数据模型,业务模型用于处理业务流程中的数据验证、数据处理、结果输出等等步骤;数据模型处理数据的持久化(增删改查等操作),数据模型承担了重要的责任,所以会围绕数据模型的底层处理展开来说 。

  • insert
  • delete
  • update
模型层的基类是抽象的DB类,有以下几个重要的公有属性
protected $tabName = ""; //表名,自动获取 protected $fieldList = array(); //表字段结构,自动获取 protected $auto; //SQL的初使化 protected $sql = array("field" => "", "where" => "", "order" => "", "limit" => "", "group" => "", "having" => "");$sql变量保存了以下信息