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

最模板

当前位置: 首页 > 建站教程 > 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.     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_
(责任编辑:admin)
------分隔线----------------------------
栏目列表
推荐内容