最模板 - 外贸网站建设,外贸网站模板

最模板

当前位置: 首页 > 建站教程 > php教程 >

php mvc中controller类实例教程

时间:2014-06-09 16:40来源: 作者: 点击: 次
本文章来讲述一下关于mvc中controller类教程,通过上两节我们知道 程序通过单一入口文件的route类决定了 唯一的moudle, conttoller, action,并在最后执行了 实例代码如下: $route -run(); /** *执行相应的MC

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

实例代码如下:

  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_; 
(责任编辑:admin)
------分隔线----------------------------
栏目列表
推荐内容