MVCwithPHP (2)

Source: Internet
Author: User
The bug in MVCwithPHP (1) exists. The biggest problem is the log system problem. after completing this introduction, I will package the source code of all the corrected programs, this will not be changed for the time being. let's take a look at the application. class. php: PHP code: --------------------------------------------------------- MVC

The bug in MVC with PHP (1) exists, and the biggest problem is the log system problem. after completing this introduction, I will package the source code of all the corrected programs.
This will not be changed for the moment.
Let's take a look at how to create a controller instance in application. class. php:

PHP code :--------------------------------------------------------------------------------
/**
* Execute a function
*
* This type of unique external interface
**/
Public function run ()
{
$ This-> parsePath ();
$ This-> checkSecurity ($ this-> module, $ this-> action );
1. $ controller = new $ this-> controllerClassName ();
2. $ controller-> {$ this-> action }();
$ This-> writeLog ($ this-> module, $ this-> action );
}

--------------------------------------------------------------------------------

The Application class is the only function that can be called after the instance. it analyzes the required Controller class name based on the user's URL request, then instantiate this class (the place marked with 1 above) and call the action name obtained from the URL (the place marked with 2 above ),

Here is a simple example:
URL: http: // localhost /? Module = news & action = showList
The Application analyzes the URL to repeat controllerClassName = news, action = showList, and then it will include the file name that processes the controller class (in Application-> getControllerFile ), then instantiate the News
Controller class (place marked with 1), and then call its action showList (place marked with 2 ).
Let's take a look at the content in newsController. php:
========================================================== ==================================

PHP code :--------------------------------------------------------------------------------

/**
* FileName: newsController. php
* Introduce: news control
*
* @ Author: Master
* @ Email: teacherli@163.com
* @ Version $ Id $
* @ Copyright 2004-10-26
**/
Include_once ("./controller/comm/controller. class. php ");
Include_once ("./model/news/newsModel. php ");

Class NewsController extends Controller
{
Private $ model;

/**
* Constructor
*
**/
Public function _ construct ()
{
Parent: :__ construct ();
$ This-> model = new NewsModel ();
$ This-> setSmartyTemplate_dir ("./view/news ");
}

/**
* Display the news list
*
**/
Public function showList ()
{
1. $ newsList = & $ this-> model-> getList ();

2. $ this-> smarty-> assign ("newsList", $ newsList );
3. unset ($ newsList );

4. $ this-> smarty-> display ("newsList.html ");
}
}
?>

--------------------------------------------------------------------------------

========================================================== ======================================
First, the NewsController class inherits from the public class Controller and generates a NewsModel class during class initialization. this class is a model class, which is responsible for all the interactions between the news module and the database. parent ::__ construct () calls the constructor of the parent class to initialize the control class Smarty of The view. $ this-> setSmartyTemplate_dir (". /view/news ") locate the Template directory in. /view/news Directory.

Then let's look at the example above. the request URL is http: // localhost /? Module = news & actio... List, indicating to call
For the showList action, see the showList () member function of the NewsController class:
1. $ newsList = & $ this-> model-> getList (): $ this-> model is created during NewsController initialization, this statement uses $ this-> model to extract a News list from the database. this list is of course a two-dimensional array required by Smarty to operate the cyclic block. in the NewsModel class, it is a two-dimensional array returned by ADODB.
2. $ this-> smarty-> assign ("newsList", $ newsList): familiar with program control of cyclic blocks in smarty
3. unset ($ newsList): for efficiency issues, these temporary variables are immediately unset after they are used.
4. $ this-> smarty-> display ("newsList.html"): use smarty to display the view.

Do you understand? In fact, the controller class has to do this: 1. call the model to retrieve records from the database. 2. operate

As a Smarty display view.
Let's take a look at the source code of the Controller class of NewsController's parent class:
========================================================== ==============================

PHP code :--------------------------------------------------------------------------------

/**
* FileName: controller. class. php
* Introduce: Base class of controller
*
* @ Author: Li Xiaojun
* @ Email: teacherli@163.com
* @ Version $ Id $
* @ Copyright 2004-10-26
**/

Include_once ("./comm/smarty/Smarty. class. php ");
Include_once ("./comm/config. inc. php ");

Abstract class Controller
{
Private $ smarty;

/**
* System build functions
* Initialize Smarty
**/
Function _ construct ()
{
$ This-> smarty = new Smarty ();

$ This-> smarty-> template_dir = "./view/templates ";
$ This-> smarty-> compile_dir = "./view/templates_c ";
$ This-> smarty-> cache_dir = "./view/cache ";
$ This-> smarty-> cache_lifetime = 60*60*24;
$ This-> smarty-> caching = false;
$ This-> smarty-> left_delimiter = "<{";
$ This-> smarty-> right_delimiter = "}> ";
}

/**
* Set the path of the smarty Template
*
* @ Param string $ template
**/
Public function setSmartyTemplate_dir ($ template)
{
$ This-> smarty-> template_dir = $ template;
}

/**
* Set whether smarty is cached.
*
* @ Param boolean $ cache
**/
Public function setSmartyCache ($ cache = false)
{
$ This-> smarty-> cache = $ cache;
}

/**
* Set the smarty cache time
*
* @ Param string $ cacheLifetime
**/
Public function setSmartyCacheTime ($ cacheLifetime)
{
$ This-> smarty-> cache_lifetime = $ cacheLifetime;
}

/**
* A short prompt after an action is executed
*
* @ Param string $ name of the module to which the module is redirected
* @ Param string $ name of the action to which the action is redirected
* @ Param string $ params parameter name
* @ Param string $ message
**/
Public function redirect ($ module, $ action, $ params = "", $ message = "action has been successfully executed

Line ")
{
$ Time = WAIT_FOR_TIME;
$ Params = ("" = $ params )? "": "& $ Params ";
$ URL = "? Module = ". $ module." & action = ". $ action. $ params;

// Reset the Smarty Template directory to the public directory
$ This-> setSmartyTemplate_dir ("./view/templates ");
$ This-> smarty-> assign ("URL", $ URL); // redirect Directory
$ This-> smarty-> assign ("message", $ message); // prompt message
$ This-> smarty-> assign ("time", $ time );
$ This-> smarty-> display ("wait.html ");
}

/**
* Processing when calling a method that does not exist in this class
*
* @ Param string $ name
* @ Param string $ parameter
**/
Public function _ call ($ name, $ parameter)
{
Throw new ActionNotAllowException ("Sorry, the action you requested $ NameNot defined

...
");
}

/**
* Destructor
*
**/
Public function _ destruct ()
{
Unset ($ this-> smarty );
}
}
?>

--------------------------------------------------------------------------------

========================================================== ======
Controller is an abstract class, that is, it cannot directly use new to generate an instance object, generate a Smarty class in the class constructor, and perform basic settings on it. Several other functions are member functions for setting the Smarty object. let's take a look at these two functions:

Public function redirect ($ module, $ action, $ params = "", $ message = "action successfully executed "):
This is a retargeting member function. it serves as a prompt page after we perform some operations on the module, and then automatically redirects to another location after the set time, for example, we want to delete the news. after the deletion is successful, we want to return such a page to the user, telling the user that the operation has been successful. please wait n seconds and return it automatically .... this is very common in forums. I have also referenced this policy here.

Public function _ call ($ name, $ parameter ):
Use this function for processing when the class call class has no declared function. this is a good thing. with it, you can use the program

The control is simpler. you can try this method ....

Okay, here's the Controller part.

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.