多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > php開源 > php教程 > php mvc中controller類實例教程

php mvc中controller類實例教程

來源:程序員人生   發布時間:2014-04-10 03:16:43 閱讀次數:3318次

本文章來講述一下關于mvc中controller類教程,通過上兩節我們知道 程序通過單一入口文件的route類決定了 唯一的moudle, conttoller, action,并在最后執行了

實例代碼如下:

  1. $route->run(); 
  2. /** 
  3.         * 執行相應的 MCA 
  4.         * 
  5.         */ 
  6.        private function run () 
  7.        { 
  8.            $filePath = APPLICATION_PATH.'/controller/'.$this->_moudle.'/'.$this->_conttoller.'.inc.php'
  9.            $isNo = 0; 
  10.            if(file_exists($filePath)) 
  11.            { 
  12.                   include "$filePath"
  13.                   $controller_tp = $this->_conttoller.'Controller'
  14.                   $controller = new $controller_tp
  15.                  
  16.               if (method_exists($controller,$this->_action.'Action')) 
  17.                   { 
  18.                      $acion_tmp = $this->_action.'Action'
  19.                      $controller->$acion_tmp(); 
  20.                   }else 
  21.                   { 
  22.                      $isNo = 1; 
  23.                   } 
  24.  
  25.            }else 
  26.            { 
  27.               $isNo = 1; 
  28.            } 
  29.           
  30.            if ($isNo
  31.            { 
  32.               $filePath = APPLICATION_PATH.'/controller/default/index.inc.php'
  33.               $this->_moudle = $this->_default['module']; 
  34.               $this->_conttoller = $this->_default['conttoller']; 
  35.               $this->_action = $this->_default['action'];            
  36.              
  37.               ($this->_moudle != $this->_default['module']) && include "$filePath"
  38.               $controller = new indexController; 
  39.               $controller->indexAction(); 
  40.            } 
  41.        } 

當相關'Controller'文件存在時執行

實例代碼如下:

  1. include "$filePath"
  2. $controller_tp = $this->_conttoller.'Controller'
  3. $controller = new $controller_tp

上述三行代碼的意思是,根據確定好的 conttoller 包含相應文件,并實例化相應的conttoller.

實例代碼如下:

  1. $acion_tmp = $this->_action.'Action'
  2.     $controller->$acion_tmp(); 

根據相應的Action 執行相應的action

所有的 Controller 類都集成一個公用的Controller 類,本節課我們就來分析一下公共的Controller 類

  1. <?php 
  2. /** 
  3.  * 前臺公共類 接口 
  4.  * 實現公共部分代碼 
  5.  */ 
  6.  
  7. /** 
  8.  * 本文件只能被index.php包含 
  9.  */ 
  10. defined("WEB_AUTH") || die("NO_AUTH"); 
  11.  
  12. /** 
  13.  * 包含菜單配置文件 
  14.  */ 
  15. ?> 

實例代碼如下:

  1. class Controller 
  2.     public $tpl
  3.     public $controller
  4.     public $body;//右邊菜單 
  5.     public $_route ; 
  6.     public $html_
  7.     public $tpl_
  8.    
  9.     /* 
  10.      * 構造函數 
  11.      */ 
  12.     public function __construct() 
  13.     { 
  14.            $this->init(); 
  15.     } 
  16.  
  17.     /* 
  18.      * 初始化變量,頂部菜單和模板 
  19.      */ 
  20.     protected function init() 
  21.     {  
  22.         global $TPL,$route
  23.         $this->tpl  = $TPL
  24.         $this->_route = $route
  25.     }  
  26.    
  27.    
  28.     /** 
  29.      * 模板變量傳第 
  30.      */ 
  31.     protected function diplayTpl() 
  32.     { 
  33.        $this->body   || $this->body = $this->_route->getActionName(); 
  34.        $this->tpl->assign("body",$this->body);      
  35.        /*設置本控制器的模板目錄*/ 
  36.        $this->controller ||$this->controller  =$this->_route->getControllerName(); 
  37.         $this->tpl->assign("controller",$this->controller); 
  38.        $this->tpl->display($this->layout);   
  39.     } 
  40.     /** 
  41.      * smarty封裝類 
  42.      * @param string $name 
  43.      * @param string $value 
  44.      */ 
  45.     public  function assign($name,$value
  46.     { 
  47.        $this->tpl->assign($name,$value); 
  48.     } 
  49.    
  50.     /** 
  51.      * 顯示另外的模板 
  52.      * @param string $name 
  53.      * @param string $value 
  54.      */ 
  55.     protected function displayOther($file
  56.     { 
  57.        $this->assign("otherTpl",TRUE); 
  58.        $this->tpl->display($file); 
  59.     }  
  60.     /** 
  61.      * 顯示某個MCA的body模板 
  62.      * 0=>m 1=>c =>a 
  63.      */ 
  64.     protected function getMcaBody($array
  65.     { 
  66.        return   'http://www.cnblogs.com/../'.$array[0].'/body/'.$array[1].'/'.$array[2]; 
  67.     } 
  68.     /* 
  69.      * 析構函數,顯示頁面 
  70.      */ 
  71.     protected function __destruct() 
  72.     {  
  73.        $this->tpl->_tpl_vars['otherTpl'] || $this->diplayTpl(); 
  74.     } 
  75.     /** 
  76.      * 中途退出 
  77.      */ 
  78.     protected function _exit($msg = ""
  79.     { 
  80.        $this->assign("otherTpl",TRUE); 
  81.        die($msg); 
  82.     } 
  83.    
  84.     /** 
  85.      * 用 $this->html_var=value放法給變量賦值 
  86.      * 用 $this->tpl_var=value放法給變量賦值 
  87.      */ 
  88.     protected function __set($name,$value
  89.     { 
  90.        if(strtolower(substr($name,0,5)) == "html_" || strtolower(substr($name,0,4)) == "tpl_"
  91.        { 
  92.            $this->assign(substr($name,5),$value); 
  93.        } 
  94.     } 
  95. ?> 

實例代碼如下:

  1. protected function __destruct() 
  2.     {  
  3.        $this->tpl->_tpl_vars['otherTpl'] || $this->diplayTpl(); 
  4.     } 

這是所有Controller 類 生命周期結束時候要執行的函數(搜索一下php魔術方法 查看詳情)

本框架利用這時候解析模板,這樣的好處是,當Controller中相關執行完相關數據處理,后自動執行相關的模板(View);而不用每次在程序最后調用模板

實例代碼如下:

  1. protected function __set($name,$value
  2.     { 
  3.        if(strtolower(substr($name,0,5)) == "html_" || strtolower(substr($name,0,4)) == "tpl_"
  4.        { 
  5.            $this->assign(substr($name,5),$value); 
  6.        } 
  7.     } 

這個函數簡化了程序向模板傳遞變量的方法,以smarty為例,在程序中需要執行 $tpl->assign(‘key’,$value);

來向模板中注冊變量,而此函數中簡化了此方法 ,只需 $this->html_key=$value;來實現相同的作用.(利用開發環境的提示功能,在前面聲明

實例代碼如下:

  1. public $html_
  2.     public $tpl_
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 亚洲午夜久久影院 | 亚洲精品亚洲九十七页 | 亚洲成人黄色片 | 日韩视频在线观看一区 | 亚洲4区| 欧美黑人巨大最猛性xxxxx | 极品福利视频 | 亚洲黄色网址 | 亚洲午夜免费视频 | 亚洲视频 中文字幕 | 天堂最新版免费观看 | 视频网站在线 | 国产一区二区三区在线免费 | 亚洲 欧美 日韩在线 | 亚洲欧洲日本精品 | 午夜理伦三级理论三级60 | 成人免费视频一区 | 麻豆精品国产自产在线 | 亚洲第一成年人网站 | 午夜久久久精品 | 欧美午夜视频在线 | 亚洲最大黄色 | xx综合网| 亚洲欧美日韩国产色另类 | 波多野结衣成人 | 亚洲春色小说 | 天堂在线天堂最新版 | 亚洲码欧美码一区二区三区 | 最近中文字幕免费完整 | 亚洲欧美日韩精品久久亚洲区色播 | 欧美精品区 | 亚洲天堂免费 | 欧美日韩国产一区 | 精品一区二区三区 不卡高清 | 欧美日韩亚洲综合在线一区二区 | 欧美一区二区三区影院 | 国产精品嫩草影院午夜 | 波多野结衣精品一区二区三区 | 欧美一级淫片aaaaaaa视频 | 中文字幕日韩一区 | 成 人国产在线观看高清不卡 |