本文章來講述一下關于mvc中controller類教程,通過上兩節我們知道 程序通過單一入口文件的route類決定了 唯一的moudle, conttoller, action,并在最后執行了
實例代碼如下:
- $route->run();
- /**
- * 執行相應的 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();
- }
- }
當相關'Controller'文件存在時執行
實例代碼如下:
- include "$filePath";
- $controller_tp = $this->_conttoller.'Controller';
- $controller = new $controller_tp;
上述三行代碼的意思是,根據確定好的 conttoller 包含相應文件,并實例化相應的conttoller.
實例代碼如下:
- $acion_tmp = $this->_action.'Action';
- $controller->$acion_tmp();
根據相應的Action 執行相應的action
所有的 Controller 類都集成一個公用的Controller 類,本節課我們就來分析一下公共的Controller 類
- <?php
- /**
- * 前臺公共類 接口
- * 實現公共部分代碼
- */
- /**
- * 本文件只能被index.php包含
- */
- defined("WEB_AUTH") || die("NO_AUTH");
- /**
- * 包含菜單配置文件
- */
- ?>
實例代碼如下:
- class Controller
- {
- public $tpl;
- public $controller;
- public $body;//右邊菜單
- public $_route ;
- public $html_;
- public $tpl_;
- /*
- * 構造函數
- */
- 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);
- /*設置本控制器的模板目錄*/
- $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);
- }
- /**
- * 顯示某個MCA的body模板
- * 0=>m 1=>c =>a
- */
- protected function getMcaBody($array)
- {
- return 'http://www.cnblogs.com/../'.$array[0].'/body/'.$array[1].'/'.$array[2];
- }
- /*
- * 析構函數,顯示頁面
- */
- 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 類 生命周期結束時候要執行的函數(搜索一下php魔術方法 查看詳情)
本框架利用這時候解析模板,這樣的好處是,當Controller中相關執行完相關數據處理,后自動執行相關的模板(View);而不用每次在程序最后調用模板
實例代碼如下:
- protected function __set($name,$value)
- {
- if(strtolower(substr($name,0,5)) == "html_" || strtolower(substr($name,0,4)) == "tpl_")
- {
- $this->assign(substr($name,5),$value);
- }
- }
這個函數簡化了程序向模板傳遞變量的方法,以smarty為例,在程序中需要執行 $tpl->assign(‘key’,$value);
來向模板中注冊變量,而此函數中簡化了此方法 ,只需 $this->html_key=$value;來實現相同的作用.(利用開發環境的提示功能,在前面聲明
實例代碼如下:
- public $html_;
- public $tpl_;
新聞熱點
疑難解答