Home  >  Article  >  Backend Development  >  Controller class example tutorial in php mvc_PHP tutorial

Controller class example tutorial in php mvc_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:13:131042browse

This article will tell you about the tutorial on the controller class in MVC. Through the previous two sections, we know that the program determines the only Moudle, Conttoller, Action through the route class of a single entry file, and finally executes

The code is as follows Copy code
 代码如下 复制代码

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

           }

       }

 

$route->run(); /** * Execute the corresponding 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->_moudle != $this->_default['module']) && include "$filePath";                     $controller = new indexController;                      $controller->indexAction();           } }


Executed when the relevant 'Controller' file exists

The code is as follows Copy code
 代码如下 复制代码

include "$filePath";

$controller_tp = $this->_conttoller.'Controller';

$controller = new $controller_tp;

include "$filePath";

$controller_tp = $this->_conttoller.'Controller';
 代码如下 复制代码

$acion_tmp = $this->_action.'Action';

    $controller->$acion_tmp();

$controller = new $controller_tp;
The above three lines of code mean to include the corresponding file according to the determined conttoller and instantiate the corresponding conttoller.
The code is as follows Copy code
$acion_tmp = $this->_action.'Action'; $controller->$acion_tmp();

Execute the corresponding action according to the corresponding Action

All Controller classes integrate a common Controller class. In this lesson, we will analyze the public Controller class

/**

* Frontend public class interface

* Implement public part of the code

*/

/**

* This file can only be indexed. php contains

*/

defined("WEB_AUTH") || die("NO_AUTH");

/**

* Contains menu configuration file

*/

The code is as follows Copy code

class Controller

{

public $tpl;

public $controller;

public $body;//right menu

public $_route ;

public $html_;

public $tpl_;

/*

* Constructor

*/

public function __construct()

{

$this->init();

}

/*

* Initialize variables, top menu and templates

*/

protected function init()

{

global $TPL,$route;

$this->tpl = $TPL;

$this->_route = $route;

}

/**

* Template variable pass

*/

protected function diplayTpl()

{

$this->body || $this->body = $this->_route->getActionName();

$this->tpl->assign("body",$this->body);

/*Set the template directory of this controller*/

$this->controller ||$this->controller =$this->_route->getControllerName();

$this->tpl->assign("controller",$this->controller);

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

}

/**

* smarty encapsulation class

* @param string $name

* @param string $value

*/

public function assign($name,$value)

{

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

}

/**

* Show additional templates

* @param string $name

* @param string $value

*/

protected function displayOther($file)

{

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

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

}

/**

* Display the body template of a certain MCA

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

*/

protected function getMcaBody($array)

{

return 'http://www.cnblogs.com/../'.$array[0].'/body/'.$array[1].'/'.$array[2];

}

/*

* Destructor, display page

*/

protected function __destruct()

{

$this->tpl->_tpl_vars['otherTpl'] || $this->diplayTpl();

    }

    /**

* Exit midway

*/

    protected function _exit($msg = "")

    {

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

       die($msg);

    }

  

    /**

* Use $this->html_var=value to assign a value to the variable

* Use $this->tpl_var=value to assign a value to the variable

*/

    protected function __set($name,$value)

    {

       if(strtolower(substr($name,0,5)) == "html_" || strtolower(substr($name,0,4)) == "tpl_")

       {

           $this->assign(substr($name,5),$value);

       }

    }

}

?>

首先看

 

这是所有Controller 类 生命周期结束时候要执行的函数(搜索一下php魔术方法 查看详情)
 代码如下
 代码如下 复制代码

protected function __destruct()

    { 

       $this->tpl->_tpl_vars['otherTpl'] || $this->diplayTpl();

    }

复制代码

protected function __destruct()

    { 

 代码如下 复制代码

protected function __set($name,$value)

    {

       if(strtolower(substr($name,0,5)) == "html_" || strtolower(substr($name,0,4)) == "tpl_")

       {

           $this->assign(substr($name,5),$value);

       }

    }

       $this->tpl->_tpl_vars['otherTpl'] || $this->diplayTpl();

    }

 代码如下 复制代码

public $html_;

    public $tpl_;

本框架利用这时候解析模板,这样的好处是,当Controller中相关执行完相关数据处理,后自动执行相关的模板(View);而不用每次在程序最后调用模板

 
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn