Tutorial _php Tutorial On Example controller classes in PHP MVC

Source: Internet
Author: User
This article tells about the Controller class tutorial in MVC, through the last two sections we know that the route class through a single portal file determines the unique Moudle, Conttoller, action, and finally executes the
The code is as follows Copy Code

$route->run ();

/**

* Perform 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->_default[' action '];

($this->_moudle! = $this->_default[' module ') && include "$filePath";

$controller = new Indexcontroller;

$controller->indexaction ();

}

}


Executes when the associated ' Controller ' file exists

The code is as follows Copy Code

Include "$filePath";

$controller _TP = $this->_conttoller. ' Controller ';

$controller = new $controller _tp;

The above three lines of code mean that the corresponding file is included according to the Conttoller, and the corresponding Conttoller is instantiated.

The code is as follows Copy Code

$acion _tmp = $this->_action. ' Action ';

$controller, $acion _tmp ();

Execute the corresponding action according to the action

All controller classes are integrated with a common controller class, this lesson we will analyze the public controller class

/**

* Front public class interface

* Implement common part code

*/

/**

* This document can only be index. PHP contains

*/

Defined ("Web_auth") | | Die ("No_auth");

/**

* Contains menu profiles

*/

The code is as follows Copy Code

Class Controller

{

Public $tpl;

Public $controller;

public $body;//Right Menu

Public $_route;

Public $html _;

Public $tpl _;

/*

* Constructor function

*/

Public Function __construct ()

{

$this->init ();

}

/*

* Initialize variables, top menu and template

*/

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 for this controller */

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

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

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

}

/**

* Smarty Package 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);

}

/**

* Displays the body template for a 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 ();

}

/**

* Halfway out

*/

protected function _exit ($msg = "")

{

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

Die ($msg);

}

/**

* Assign values to variables with $this->html_var=value

* Assign values to variables with $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);

}

}

}

?>

First Look

The code is as follows Copy Code

protected function __destruct ()

{

$this->tpl->_tpl_vars[' Othertpl ' | | $this->diplaytpl ();

}

This is the function to be executed at the end of all controller class lifecycles (Search the PHP Magic method for details)

This framework uses this time to parse the template, the advantage is that when the controller is related to the execution of related data processing, and then automatically execute the relevant template (View), instead of having to call the template at the end of the program

The code is as follows Copy Code

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 function simplifies the way the program passes variables to the template, taking Smarty as an example, $tpl->assign (' key ', $value) need to be executed in the program;

To register variables in the template, and this function simplifies this method by simply $this->html_key= $value to achieve the same effect. (Take advantage of the development environment's hints feature, which is stated earlier

The code is as follows Copy Code

Public $html _;

Public $tpl _;

http://www.bkjia.com/PHPjc/629229.html www.bkjia.com true http://www.bkjia.com/PHPjc/629229.html techarticle This article tells about the Controller class tutorial in MVC, through the last two sections we know that the program passes through a single portal file of the route class that determines the only Moudle, Conttoller, action, and in the most ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.