首页 后端开发 php教程 php mvc中controller类实例教程_PHP教程

php mvc中controller类实例教程_PHP教程

Jul 13, 2016 pm 05:13 PM
controller mvc php 一下 关于 实例 教程 文章

本文章来讲述一下关于mvc中controller类教程,通过上两节我们知道 程序通过单一入口文件的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 类

/**

 * 前台公共类 接口

 * 实现公共部分代码

 */

 

/**

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

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/629229.htmlTechArticle本文章来讲述一下关于mvc中controller类教程,通过上两节我们知道 程序通过单一入口文件的route类决定了 唯一的moudle, conttoller, action,并在最...
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

适用于 Ubuntu 和 Debian 的 PHP 8.4 安装和升级指南 适用于 Ubuntu 和 Debian 的 PHP 8.4 安装和升级指南 Dec 24, 2024 pm 04:42 PM

PHP 8.4 带来了多项新功能、安全性改进和性能改进,同时弃用和删除了大量功能。 本指南介绍了如何在 Ubuntu、Debian 或其衍生版本上安装 PHP 8.4 或升级到 PHP 8.4

我后悔之前不知道的 7 个 PHP 函数 我后悔之前不知道的 7 个 PHP 函数 Nov 13, 2024 am 09:42 AM

如果您是一位经验丰富的 PHP 开发人员,您可能会感觉您已经在那里并且已经完成了。您已经开发了大量的应用程序,调试了数百万行代码,并调整了一堆脚本来实现操作

如何设置 Visual Studio Code (VS Code) 进行 PHP 开发 如何设置 Visual Studio Code (VS Code) 进行 PHP 开发 Dec 20, 2024 am 11:31 AM

Visual Studio Code,也称为 VS Code,是一个免费的源代码编辑器 - 或集成开发环境 (IDE) - 可用于所有主要操作系统。 VS Code 拥有针对多种编程语言的大量扩展,可以轻松编写

在PHP API中说明JSON Web令牌(JWT)及其用例。 在PHP API中说明JSON Web令牌(JWT)及其用例。 Apr 05, 2025 am 12:04 AM

JWT是一种基于JSON的开放标准,用于在各方之间安全地传输信息,主要用于身份验证和信息交换。1.JWT由Header、Payload和Signature三部分组成。2.JWT的工作原理包括生成JWT、验证JWT和解析Payload三个步骤。3.在PHP中使用JWT进行身份验证时,可以生成和验证JWT,并在高级用法中包含用户角色和权限信息。4.常见错误包括签名验证失败、令牌过期和Payload过大,调试技巧包括使用调试工具和日志记录。5.性能优化和最佳实践包括使用合适的签名算法、合理设置有效期、

您如何在PHP中解析和处理HTML/XML? 您如何在PHP中解析和处理HTML/XML? Feb 07, 2025 am 11:57 AM

本教程演示了如何使用PHP有效地处理XML文档。 XML(可扩展的标记语言)是一种用于人类可读性和机器解析的多功能文本标记语言。它通常用于数据存储

php程序在字符串中计数元音 php程序在字符串中计数元音 Feb 07, 2025 pm 12:12 PM

字符串是由字符组成的序列,包括字母、数字和符号。本教程将学习如何使用不同的方法在PHP中计算给定字符串中元音的数量。英语中的元音是a、e、i、o、u,它们可以是大写或小写。 什么是元音? 元音是代表特定语音的字母字符。英语中共有五个元音,包括大写和小写: a, e, i, o, u 示例 1 输入:字符串 = "Tutorialspoint" 输出:6 解释 字符串 "Tutorialspoint" 中的元音是 u、o、i、a、o、i。总共有 6 个元

解释PHP中的晚期静态绑定(静态::)。 解释PHP中的晚期静态绑定(静态::)。 Apr 03, 2025 am 12:04 AM

静态绑定(static::)在PHP中实现晚期静态绑定(LSB),允许在静态上下文中引用调用类而非定义类。1)解析过程在运行时进行,2)在继承关系中向上查找调用类,3)可能带来性能开销。

什么是PHP魔术方法(__ -construct,__destruct,__call,__get,__ set等)并提供用例? 什么是PHP魔术方法(__ -construct,__destruct,__call,__get,__ set等)并提供用例? Apr 03, 2025 am 12:03 AM

PHP的魔法方法有哪些?PHP的魔法方法包括:1.\_\_construct,用于初始化对象;2.\_\_destruct,用于清理资源;3.\_\_call,处理不存在的方法调用;4.\_\_get,实现动态属性访问;5.\_\_set,实现动态属性设置。这些方法在特定情况下自动调用,提升代码的灵活性和效率。

See all articles