国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

php mvc中controller類實例教程

2024-05-04 21:57:53
字體:
來源:轉載
供稿:網友

本文章來講述一下關于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_

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 韩城市| 澄迈县| 漳浦县| 肇州县| 华蓥市| 阿拉善右旗| 凤台县| 新和县| 海门市| 宝丰县| 樟树市| 蒙城县| 合肥市| 盐城市| 元氏县| 延庆县| 温泉县| 营口市| 广安市| 阳新县| 屏东县| 任丘市| 海安县| 正镶白旗| 芷江| 贡觉县| 哈尔滨市| 株洲县| 靖边县| 买车| 五家渠市| 眉山市| 宁乡县| 焉耆| 太白县| 平潭县| 汤阴县| 双鸭山市| 江山市| 池州市| 城步|