php mvc中controller類實例教程
來源:程序員人生 發布時間:2014-04-10 03:16:43 閱讀次數:3318次
本文章來講述一下關于mvc中controller類教程,通過上兩節我們知道 程序通過單一入口文件的route類決定了 唯一的moudle, conttoller, action,并在最后執行了
實例代碼如下:
- $route->run();
-
-
-
-
- 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
-
-
-
-
-
-
-
-
- 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);
- }
-
-
-
-
-
- public function assign($name,$value)
- {
- $this->tpl->assign($name,$value);
- }
-
-
-
-
-
-
- protected function displayOther($file)
- {
- $this->assign("otherTpl",TRUE);
- $this->tpl->display($file);
- }
-
-
-
-
- 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);
- }
-
-
-
-
-
- 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_;
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈