Controller instance in phpmvc tutorial _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Tutorial on controller instances in phpmvc. 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, in the latest article, I will introduce the controller class tutorial in mvc. through the last two sections, we know that the program determines the unique moudle, conttoller, action through the route class of a single entry file, and finally executed
The code is as follows:

$ 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-> _ default ['action'];

($ This-> _ moudle! = $ This-> _ default ['module']) & include "$ filePath ";

$ Controller = new indexController;

$ Controller-> indexAction ();

}

}


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

The code is as follows:

Include "$ filePath ";

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

$ Controller = new $ controller_tp;

The above three lines of code mean that, based on the determined conttoroller, it contains the corresponding file and instantiate the corresponding conttoroller.

The 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.

/**

* Foreground public interfaces

* Implement partial public code

*/

/**

* This file can only be indexed. Php inclusion

*/

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

/**

* Contains the menu configuration file.

*/

The code is as follows:

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 menus, and templates

*/

Protected function init ()

{

Global $ TPL, $ route;

$ This-> tpl = $ TPL;

$ This-> _ route = $ route;

}

/**

* Pass the template variable to

*/

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 of a MCA

* 0 => m 1 => c =>

*/

Protected function getMcaBody ($ array)

{

Return 'http: // response

}

/*

* Destructor. the page is displayed.

*/

Protected function _ destruct ()

{

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

}

/**

* Exit midway through

*/

Protected function _ exit ($ msg = "")

{

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

Die ($ msg );

}

/**

* Use the $ this-> html_var = value placement method to assign values to variables.

* Use the $ this-> tpl_var = value placement method to assign values to variables.

*/

Protected function _ set ($ name, $ value)

{

If (strtolower (substr ($ name,) = "html _" | strtolower (substr ($ name,) = "tpl _")

{

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

}

}

}

?>

First, see

The 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 code is as follows:

Protected function _ set ($ name, $ value)

{

If (strtolower (substr ($ name,) = "html _" | strtolower (substr ($ name,) = "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 code is as follows:

Public $ html _;

Public $ tpl _;

The handler program determines the unique moudle, conttoller, and action through the route class of a single entry file, and...

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.