Tutorial on controller instances in phpmvc

Source: Internet
Author: User
This article describes the controller class tutorial in mvc. through the last two sections, we know that the program uses the route class of a single entry file to determine the unique moudle, conttoller, action, and finally executed the real... this article describes the controller class tutorial in mvc. through the last two sections, we know that the program determines the unique moudle, conttoller, and action through the route class of a single entry file, and finally executes

The instance code is as follows:

 Run ();/*** run 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 () ;}}?>

Run this command when the related 'controller' file exists.

The instance code is as follows:

Include "$ filePath ";

$ Controller_tp = $ this-> _ conttoller. 'controller ';

$ Controller = new $ controller_tp;

The above three lines of code mean that, according to the determined conttoller, the corresponding file is contained and the corresponding conttoller is instantiated.

The instance code is as follows:

$ Acion_tmp = $ this-> _ action. 'action ';

$ Controller-> $ acion_tmp ();

Execute the corresponding Action according to the corresponding action

All Controller classes are integrated with a public Controller class. In this lesson, we will analyze the public Controller class.

 Init ();}/** initialization variable, top menu and template */protected function init () {global $ TPL, $ route; $ this-> tpl = $ TPL; $ this-> _ route = $ route;}/*** template variable to pass the */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 );} /*** display another template * @ param string $ name * @ param string $ value */protected function displayOther ($ file) {$ this-> assign ("otherTpl ", TRUE); $ this-> tpl-> display ($ file );} /*** display the body Template * 0 => m 1 => c => a */protected function getMcaBody ($ array) {return' http://www.cnblogs.com/../ '. $ Array [0]. '/body /'. $ array [1]. '/'. $ array [2];}/** destructor. the */protected function _ destruct () page is displayed () {$ this-> tpl-> _ tpl_vars ['othertpl '] | $ this-> diplayTpl ();} /*** quit midway */protected function _ exit ($ msg = "") {$ this-> assign ("otherTpl", TRUE); die ($ msg );} /*** assign a value to the variable using the $ this-> html_var = value placement method * assign a value to the variable using the $ this-> tpl_var = value placement method */protected function _ set ($ name, $ value) {if (strtolower (subst R ($ name, 0, 5) = "html _" | strtolower (substr ($ name, 0, 4) = "tpl _") {$ this-> assign (substr ($ name, 5), $ value) ;}}}?>

The instance code is as follows:

Protected function _ destruct ()

{

$ This-> tpl-> _ tpl_vars ['othertpl '] | $ this-> diplayTpl ();

}

This is the function to be executed at the end of the lifecycle of all Controller classes (search for the php magic method to view details)

This framework uses the parsing template at this time. the advantage is that when the relevant data processing in the Controller is completed, the relevant template (View) is automatically executed, instead of calling the template at the end of each program

The instance code is as follows:

    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 procedure of passing variables to the template. taking smarty as an example, you need to execute $ tpl-> assign ('key', $ value) in the program );

To register variables in the template, and this function simplifies this method, you only need $ this-> html_key = $ value; to achieve the same effect. (use the prompt function of the development environment to declare

The instance code is as follows:

Public $ html _;

Public $ tpl _;


Permanent address:

Reprint at will ~ Please bring the tutorial URL ^

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.