php mvc开发系列教程第三节 Controller 类实现
通過上兩節(jié)我們知道 程序通過單一入口文件的route類決定了 唯一的moudle, conttoller, action,并在最后執(zhí)行了
$route->run();? ??
/*** 執(zhí)行相應(yīng)的 MCA
*
*/
private function run ()
{
$filePath = APPLICATION_PATH.'/controller/'.$this->_moudle.'/'.$this->_conttoller.'.inc.php';
$isNo = 0;
if(file_exists($filePath))
{
include "$filePath";
$controller_tp = $this->_conttoller.'Controller';
$controller = new $controller_tp;
if (method_exists($controller,$this->_action.'Action'))
{
$acion_tmp = $this->_action.'Action';
$controller->$acion_tmp();
}else
{
$isNo = 1;
}
}else
{
$isNo = 1;
}
if ($isNo)
{
$filePath = APPLICATION_PATH.'/controller/default/index.inc.php';
$this->_moudle = $this->_default['module'];
$this->_conttoller = $this->_default['conttoller'];
$this->_action = $this->_default['action'];
($this->_moudle != $this->_default['module']) && include "$filePath";
$controller = new indexController;
$controller->indexAction();
}
}
?
?
當(dāng)相關(guān)'Controller'文件存在時(shí)執(zhí)行
?
include "$filePath";$controller_tp = $this->_conttoller.'Controller';
$controller = new $controller_tp;
上述三行代碼的意思是,根據(jù)確定好的 conttoller 包含相應(yīng)文件,并實(shí)例化相應(yīng)的conttoller。
??????
? ??
$acion_tmp = $this->_action.'Action';$controller->$acion_tmp();
?
根據(jù)相應(yīng)的Action 執(zhí)行相應(yīng)的action
?
?
所有的? Controller 類都集成一個(gè)公用的Controller 類,本節(jié)課我們就來分析一下公共的Controller 類
<?php/**
* 前臺公共類 接口
* 實(shí)現(xiàn)公共部分代碼
*/
/**
* 本文件只能被index。php包含
*/
defined("WEB_AUTH") || die("NO_AUTH");
/**
* 包含菜單配置文件
*/
class Controller
{
public $tpl;
public $controller;
public $body;//右邊菜單
public $_route ;
public $html_;
public $tpl_;
/*
* 構(gòu)造函數(shù)
*/
public function __construct()
{
$this->init();
}
/*
* 初始化變量,頂部菜單和模板
*/
protected function init()
{
global $TPL,$route;
$this->tpl = $TPL;
$this->_route = $route;
}
/**
* 模板變量傳第
*/
protected function diplayTpl()
{
$this->body || $this->body = $this->_route->getActionName();
$this->tpl->assign("body",$this->body);
/*設(shè)置本控制器的模板目錄*/
$this->controller ||$this->controller =$this->_route->getControllerName();
$this->tpl->assign("controller",$this->controller);
$this->tpl->display($this->layout);
}
/**
* smarty封裝類
* @param string $name
* @param string $value
*/
public function assign($name,$value)
{
$this->tpl->assign($name,$value);
}
/**
* 顯示另外的模板
* @param string $name
* @param string $value
*/
protected function displayOther($file)
{
$this->assign("otherTpl",TRUE);
$this->tpl->display($file);
}
/**
* 顯示某個(gè)MCA的body模板
* 0=>m 1=>c =>a
*/
protected function getMcaBody($array)
{
return 'http://www.cnblogs.com/../'.$array[0].'/body/'.$array[1].'/'.$array[2];
}
/*
* 析構(gòu)函數(shù),顯示頁面
*/
protected function __destruct()
{
$this->tpl->_tpl_vars['otherTpl'] || $this->diplayTpl();
}
/**
* 中途退出
*/
protected function _exit($msg = "")
{
$this->assign("otherTpl",TRUE);
die($msg);
}
/**
* 用 $this->html_var=value放法給變量賦值
* 用 $this->tpl_var=value放法給變量賦值
*/
protected function __set($name,$value)
{
if(strtolower(substr($name,0,5)) == "html_" || strtolower(substr($name,0,4)) == "tpl_")
{
$this->assign(substr($name,5),$value);
}
}
}
?>
?
首先看
? ?
protected function __destruct(){
$this->tpl->_tpl_vars['otherTpl'] || $this->diplayTpl();
}
?
這是所有Controller 類 生命周期結(jié)束時(shí)候要執(zhí)行的函數(shù)(搜索一下php魔術(shù)方法 查看詳情)
本框架利用這時(shí)候解析模板,這樣的好處是,當(dāng)Controller中相關(guān)執(zhí)行完相關(guān)數(shù)據(jù)處理,后自動執(zhí)行相關(guān)的模板(View);而不用每次在程序最后調(diào)用模板
?
protected function __set($name,$value){
if(strtolower(substr($name,0,5)) == "html_" || strtolower(substr($name,0,4)) == "tpl_")
{
$this->assign(substr($name,5),$value);
}
}
這個(gè)函數(shù)簡化了程序向模板傳遞變量的方法,以smarty為例,在程序中需要執(zhí)行 $tpl->assign(‘key’,$value);
來向模板中注冊變量,而此函數(shù)中簡化了此方法 ,只需 $this->html_key=$value;來實(shí)現(xiàn)相同的作用.(利用開發(fā)環(huán)境的提示功能,在前面聲明
public $html_;public $tpl_;
更加簡化了向模板注冊變量
轉(zhuǎn)載于:https://www.cnblogs.com/tianliangle/archive/2012/01/07/2315585.html
總結(jié)
以上是生活随笔為你收集整理的php mvc开发系列教程第三节 Controller 类实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Run tomcat 5.5 in wi
- 下一篇: ASP.NET模板引擎技术