完成封装(四)后,新建model.class.php,代码如下:
<?php
class Model extends SQLQuery{
protected $_model;
function __construct() {
$this->connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);
$this->_model = get_class($this);
$this->_table = strtolower($this->_model)."s";
}
function __destruct() {
}
}
如图所示:
新建视图基类为template.class.php,具体代码如下:
<?php
class Template {
protected $variables = array();
protected $_controller;
protected $_action;
function __construct($controller,$action) {
$this->_controller = $controller;
$this->_action =$action;
}
/* 设置变量 */
{
if(!empty($name)){
$this->_action = $name;
}
$this->variables = $array;
}
/* 显示模板 */
function render() {
if(!empty($this->variables)){
foreach ($this->variables as $key => $value) {
extract(array($key=>$value)); //将变量导入当前符号表
}
}
if (file_exists(ROOT.DS. 'application' .DS. 'views' .DS. $this->_controller .DS. 'header.php')) {
include(ROOT.DS. 'application' .DS. 'views' .DS. $this->_controller .DS. 'header.php');
} else {
include(ROOT.DS. 'application' .DS. 'views' .DS. 'header.php');
}
include (ROOT.DS. 'application' .DS. 'views' .DS. $this->_controller .DS. $this->_action . '.php');
if (file_exists(ROOT.DS. 'application' .DS. 'views' .DS. $this->_controller .DS. 'footer.php')) {
include (ROOT.DS. 'application' .DS. 'views' .DS. $this->_controller .DS. 'footer.php');
} else {
include (ROOT.DS. 'application' .DS. 'views' .DS. 'footer.php');
}
}
}
如图所示:
做完了以上这么多操作,基本上整个MVC框架已经出来了,下面就该制作我们的站点了。我们要做的站点其实很简单,一个ToDo程序。
下面操作请点击:封装MVC(六)