通过上两节我们知道 程序通过单一入口文件的route类决定了 唯一的moudle, conttoller, action,并在最后执行了

$route->run();

   /**

        * 执行相应的 MCA

        *

*/

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

/**

 * 前台公共类 接口

 * 实现公共部分代码

*/

/**

 * 本文件只能被index。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);   

    }

/**

     * smarty封装类

     * @param string $name

     * @param string $value

*/

public  function assign($name,$value)

    {

$this->tpl->assign($name,$value);

    }

/**

     * 显示另外的模板

     * @param string $name

     * @param string $value

*/

protected function displayOther($file)

    {

$this->assign("otherTpl",TRUE);

$this->tpl->display($file);

    }  

/**

     * 显示某个MCA的body模板

     * 0=>m 1=>c =>a

*/

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);

    }

/**

     * 用 $this->html_var=value放法给变量赋值

     * 用 $this->tpl_var=value放法给变量赋值

*/

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_;

更加简化了向模板注册变量

转载于:https://www.cnblogs.com/tianliangle/archive/2012/01/07/2315585.html

php mvc开发系列教程第三节 Controller 类实现相关推荐

  1. 树莓派开发系列教程10——树莓派spi液晶屏支持(fbtft)

        树莓派官方支持av及HDMI输出,板子上预留了一个csi接口的液晶显示屏,但是一直没有相应的模组出现.在很多应用场合我们需要一些小型的液晶屏显示一些基本的信息,所以小屏驱动很是必要. 在git ...

  2. unity2d游戏开发系列教程:三、场景布置,增加怪物和机关

    目录 unity2d游戏开发系列教程:一.环境安装 unity2d游戏开发系列教程:二.新建工程并熟悉Unity编辑器常用功能 第一节.场景草地布置 先查看一下资源文件里都有什么,一会就要用到的 打开 ...

  3. unity2d游戏开发系列教程:二、新建工程并熟悉Unity编辑器常用功能

    目录 unity2d游戏开发系列教程:一.环境安装 第一步.打开项目 耐心等待一小会 工程界面 第二步.创建第一个场景(第一关)进行试玩 点击图中标号1的运行按钮,即可简单试玩感受,操作如下 移动A, ...

  4. ASP.NET MVC 5 入门教程 (2) 控制器Controller

    原文:ASP.NET MVC 5 入门教程 (2) 控制器Controller 文章来源: Slark.NET-博客园 http://www.cnblogs.com/slark/p/mvc-5-get ...

  5. C#微信公众号开发系列教程三(消息体签名及加解密)

    http://www.cnblogs.com/zskbll/p/4139039.html C#微信公众号开发系列教程一(调试环境部署) C#微信公众号开发系列教程一(调试环境部署续:vs远程调试) C ...

  6. 微信小程序开发系列教程三:微信小程序的调试方法

    微信小程序开发系列教程 微信小程序开发系列一:微信小程序的申请和开发环境的搭建 微信小程序开发系列二:微信小程序的视图设计 这个教程的前两篇文章,介绍了如何用下图所示的微信开发者工具自动生成一个Hel ...

  7. C#微信公众号开发系列教程五(接收事件推送与消息排重)

    C#微信公众号开发系列教程五(接收事件推送与消息排重) 原文:C#微信公众号开发系列教程五(接收事件推送与消息排重) 微信公众号开发系列教程一(调试环境部署) 微信公众号开发系列教程一(调试环境部署续 ...

  8. C#微信公众号开发系列教程二(新手接入指南)

    此系列前面已经更新了两篇博文了,都是微信开发的前期准备工作,现在切入正题,本篇讲解新手接入的步骤与方法,大神可直接跳过,也欢迎大神吐槽. 微信公众号开发系列教程一(调试环境部署) 微信公众号开发系列教 ...

  9. HTML5游戏开发系列教程5(译)

    原文地址:http://www.script-tutorials.com/html5-game-development-lesson-5/ 最终我决定准备下一篇游戏开发系列的文章,我们将继续使用can ...

最新文章

  1. NanoPi NEO Air使用四:操作GPIO
  2. LayUi 树形组件tree 实现懒加载模式,展开父节点时异步加载子节点数据
  3. Python中小整数对象池和大整数对象池
  4. 洛谷P1144-最短路计算【日常最短路,日常图论,SPFA】
  5. spring-boot注解详解(三)
  6. 使用Apache CXF进行Web服务学习
  7. Pytest fixture之request传参
  8. 代码质量度量标准_Google研发度量改进实践
  9. Java 1.2.3 文件输入与输出
  10. python画饼图存在的问题_python_使用matplotlib画饼状图(pie)
  11. 插值和空间分析(二)_变异函数分析(R语言)
  12. 江苏图采上传自定义证件照
  13. matlab——diff函数
  14. 图像处理——SIFT算法
  15. 基于STM32F103RCT6的AD9833驱动开发(代码可以免费发邮箱)
  16. [转]技术以外的功夫
  17. 腾讯地图点聚合开发-实现地图找房功能
  18. 联想电脑如何打开BIOS并开启虚拟化——以G50为例
  19. 自媒体平台数据统计分析爬虫之【趣头条】模拟登陆分析详解及数据统计接口详解
  20. 软件测试之与大厂测试经理的问答

热门文章

  1. weblogic12c 设置jvm大小_weblogic 12c 配置jvm的内存大小
  2. 蛋白质合成有关的分子生物学知识问答
  3. <文本,场景图>解析实践
  4. 黑科技(next_permutation和prev_permutation)
  5. JZOJ__Day 5:【普及模拟】权势二进制
  6. Codeforces 1188 题解
  7. python开发是不是苦累_Python 2.7 辛苦了,你好Python 3.7
  8. chrome js 读取文件夹_使用JavaScript遍历本地文件夹的文件
  9. 20210620 Successive projection algorithm(连续投影法)
  10. first day in microsoft