Tutorial on controller instances in php mvc

Source: Internet
Author: User
The code is as follows: Copy code

$ 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: Copy code

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: Copy code

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

<? Php

/**

* 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: 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 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: Copy code

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: Copy code

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: Copy code

Public $ html _;

Public $ tpl _;

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.