完成封裝(四)後,新建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(六)