MVC with PHP (ii)

Source: Internet
Author: User
MVC with PHP (i) bugs in the problem is there, the biggest problem is the problem of the log system, such as the completion of this introduction after I have all corrected the program source code package
Come out, there will be no change here for the time being.
Let's take a look at how to create a controller instance in application.class.php:

PHP Code:--------------------------------------------------------------------------------
/**
* Executive function
*
* This kind of only 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);
}

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

Application This class is the only function that can be invoked after an instance, which analyzes the desired controller class name based on the user's URL request, then instantiates the class (where the superscript 1 above) is invoked, and then invokes the name of the action obtained from the URL (the place labeled 2 above),

Here's a simple example:
Url:http://localhost/?module=news&action=showlist
Application by parsing this URL to Controllerclassname=news, action=showlist, and then it will be in the file name containing the controller class (in application-> Getcontrollerfile (), and then instantiate the news of this
The Controller class (the place marked 1), then calls its action showlist (the place labeled 2).
Look at the contents of the newscontroller.php:
=============================================================

PHP Code:--------------------------------------------------------------------------------

<?php
/**
* FileName:newsController.php
* Introduce: News control class
*
* @author: Big bro
* @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");
}

/**
* Show 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, generating a Newsmodel class when the class is initialized, which is a model class that is responsible for all of the news module's interactions with the database. Parent::__construct () invokes the constructor of the parent class to complete the initialization of the control class Smarty for view. $this->setsmartytemplate_dir ("./view/news") locates the template directory in the./ View/news directory.

Then look at our example above, request URL for http://localhost/?module=news&actio ... List that represents the call to
Showlist This action, look at the showlist () member function of the Newscontroller class:
1. $newsList = & $this->model->getlist (): $this->model is established at Newscontroller initialization, this sentence is to be used $this-> Model from the database to extract a news list, this list of course is smarty in the operation of the loop block required two-dimensional array, in the Newsmodel class, it is a two-dimensional array of ADODB.
2. $this->smarty->assign ("Newslist", $newsList): Familiar with it, control of circulation block in Smarty
3. unset ($newsList): In view of the efficiency problem, these temporary variables are unset immediately after the use is complete.
4. $this->smarty->display ("newslist.html"): Use Smarty to display view.

Do you see what you've got? In fact the Controller class is doing this: 1. Call model to fetch records from the database 2.

Show view as Smarty.
Then look at the Newscontroller of the parent class controller class Source:
===========================================================

PHP Code:--------------------------------------------------------------------------------

<?php
/**
* 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 function
* Initialization of 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 Smarty template path
*
* @param string $template
**/
Public Function Setsmartytemplate_dir ($template)
{
$this->smarty->template_dir = $template;
}

/**
* Set whether the Smarty is cached
*
* @param boolean $cache
**/
Public Function Setsmartycache ($cache = False)
{
$this->smarty->cache = $cache;
}

/**
* Set Smarty Cache time
*
* @param string $cacheLifetime
**/
Public Function Setsmartycachetime ($cacheLifetime)
{
$this->smarty->cache_lifetime = $cacheLifetime;
}

/**
* The action is executed after a short hint
*
* @param string $module redirected to the name of the module
* @param string $action a redirected action name
* @param string $params parameter name
* @param string $message hint information
**/
Public function Redirect ($module, $action, $params = "", $message = "Action has been successfully

Line ")
{
$time = Wait_for_time;
$params = ("" = = $params)? ":" & $params ";
$URL = "module=". $module. "&action=". $action. $params;

Re-smarty template directory to public directory
$this->setsmartytemplate_dir ("./view/templates");
$this->smarty->assign ("URL", $URL); Redirected directories
$this->smarty->assign ("message", $message); Hint Information
$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, you requested action <b> $name </b> no definition

...<br> ");
}

/**
* destructor
*
**/
Public Function __destruct ()
{
unset ($this->smarty);
}
}
?>

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

==============================================
Controller is an abstract class, which means that it cannot directly use new to produce an instance object, produce a Smarty class in the constructor of a class, and make basic settings for it. The other functions are the member functions that set the Smarty object, and here's a look at the two functions:

Public function Redirect ($module, $action, $params = "", $message = "Action has been successfully executed"):
This is a redirect member function, it is the role of the module when we do some operation after the prompt page, and then set a good time to automatically redirect to another location, for example, we want to delete some of the news, after the deletion of success we want to return to the user such a page, told the user operation has been successful, Please wait for n seconds to return automatically .... This is very common in the forum, and I quote this strategy here.

Public Function __call ($name, $parameter):
This is a good thing to do when a class invokes a class that does not have a declared function, and with it, you can use the program

The control is more simple, we can try this method ....

Well, here's the Controller section.



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.