Translated by Harry Fuecks
The MVC mode is very common in website architecture. It allows us to build a three-tier application that separates useful layers from code, helping designers and developers work together and improve our ability to maintain and expand existing programs.
View)
"View" mainly refers to the final result we send to the Web browser-for example, the HTML generated by our script. When it comes to views, many people think of templates, but the correctness of the template scheme called views is questionable.
The most important thing for a view is that it should be self-aware. When a view is rendered, view elements can be aware of their roles in a larger framework.
Taking XML as an example, we can say that when XML is parsed, the dom api has such cognition that the nodes in a DOM tree know where it is and what it contains. (When a node in an XML document is parsed using SAX, it makes sense only when it is parsed to this node .)
Most template schemes use simple process languages and such template labels:
<P> {some_text} </p>
<P> {some_more_text} </p>
They do not make sense in the document. They represent that PHP will replace it with something else.
If you agree to this loose description of the view, you will agree that the vast majority of template schemes do not effectively separate views and models. The template tag will be replaced with what is stored in the model.
When you implement a view, ask yourself a few questions: "Is it easy to replace all views ?" "How long does it take to implement a new view ?" "Is it easy to replace the description language of the view? (For example, replacing HTML documents with SOAP documents in the same view )"
Model)
The model represents the program logic. (Business layer is often called in enterprise applications ))
In general, the model task is to convert the original data into data that contains some meaning, and the data will be displayed by the view. Generally, the model encapsulates data queries and may use abstract data classes (data access layer) to perform queries. For example, if you want to calculate the annual rainfall in the UK (just to find a good holiday place for yourself), the model will receive the daily rainfall for ten years, calculate the average value, and pass it to the view.
Controller)
Simply put, the controller is the first part of the HTTP request to be called in a Web application. It checks received requests, such as GET variables, and makes appropriate feedback. Before writing your first controller, it is difficult for you to write other PHP code. The most common usage is the structure of a switch statement in index. php:
<? Php
Switch ($ _ GET ['viewpage']) {
Case "news ":
$ Page = new NewsRenderer;
Break;
Case "links ":
$ Page = new LinksRenderer;
Break;
Default:
$ Page = new HomePageRenderer;
Break;
}
$ Page-> display ();
?>
This code mix process-oriented and object-oriented code, but this is usually the best choice for small websites. The code above can be optimized.
The Controller is actually a control used to trigger the binding between the data of the model and the view elements.
Example
Here is a simple example of using the MVC mode.
First, we need a database category class, which is a common class.
<? Php
/**
* A simple class for querying MySQL
*/
Class DataAccess {
/**
* Private
* $ Db stores a database resource
*/
Var $ db;
/**
* Private
* $ Query stores a query resource
*/
Var $ query; // Query resource
//! A constructor.
/**
* Constucts a new DataAccess object
* @ Param $ host string hostname for dbserver
* @ Param $ user string dbserver user
* @ Param $ pass string dbserver user password
* @ Param $ db string database name
*/
Function DataAccess ($ host, $ user, $ pass, $ db ){
$ This-> db = mysql_pconnect ($ host, $ user, $ pass );
Mysql_select_db ($ db, $ this-> db );
}
//! An accessor
/**
* Fetches a query resources and stores it in a local member
* @ Param $ SQL string the database query to run
* @ Return void
*/
Function fetch ($ SQL ){
$ This-> query = mysql_unbuffered_query ($ SQL, $ this-> db); // Perform query here
}
//! An accessor
/**
* Returns an associative array of a query row
* @ Return mixed
*/
Function getRow (){
If ($ row = mysql_fetch_array ($ this-> query, MYSQL_ASSOC ))
Return $ row;
Else
Return false;
}
}
?>
Put the model above it.
<? Php
/**
* Fetches "products" from the database
*/
Class ProductModel {
/**
* Private
* $ Dao an instance of the DataAccess class
*/
Var $ dao;
//! A constructor.
/**
* Constucts a new ProductModel object
* @ Param $ dbobject an instance of the DataAccess class
*/
Function ProductModel (& $ dao ){
$ This-> dao = & $ dao;
}
//! A manipulator
/**
* Tells the $ dboject to store this query as a resource
* @ Param $ start the row to start from
* @ Param $ rows the number of rows to fetch
* @ Return void
*/
Function listProducts ($ start = 1, $ rows = 50 ){
$ This-> dao-> fetch ("SELECT * FROM products LIMIT". $ start. ",". $ rows );
}
//! A manipulator
/**
* Tells the $ dboject to store this query as a resource
* @ Param $ id a primary key for a row
* @ Return void
*/
Function listProduct ($ id ){
$ This-> dao-> fetch ("SELECT * FROM products where productid = '". $ id ."'");
}
//! A manipulator
/**
* Fetches a product as an associative array from the $ dbobject
* @ Return mixed
*/
Function getProduct (){
If ($ product = $ this-> dao-> getRow ())
Return $ product;
Else
Return false;
}
}
?>
One thing to note is that between the model and the Data Pipeline class, their interaction will never be more than one row-no multiple rows are transmitted, which will quickly slow the program down. For classes in the usage mode, the same program only needs to keep one Row in the memory -- others are handed over to saved query resources -- in other words, we asked MYSQL to keep the results for us.
Next is the view-I removed HTML to save space. You can view the complete code of this article.
<? Php
/**
* Binds product data to HTML rendering
*/
Class ProductView {
/**
* Private
* $ Model an instance of the ProductModel class
*/
Var $ model;
/**
* Private
* $ Output rendered HTML is stored here for display
*/
Var $ output;
//! A constructor.
/**
* Constucts a new ProductView object
* @ Param $ model an instance of the ProductModel class
*/
Function ProductView (& $ model ){
$ This-> model = & $ model;
}
//! A manipulator
/**
* Builds the top of an HTML page
* @ Return void
*/
Function header (){
}
//! A manipulator
/**
* Builds the bottom of an HTML page
* @ Return void
*/
Function footer (){
}
//! A manipulator
/**
* Displays a single product
* @ Return void
*/
Function productItem ($ id = 1 ){
$ This-> model-> listProduct ($ id );
While ($ product = $ this-> model-> getProduct ()){
// Bind data to HTML
}
}
//! A manipulator
/**
* Builds a product table
* @ Return void
*/
Function productTable ($ rownum = 1 ){
$ Rowsperpage = '20 ';
$ This-> model-> listProducts ($ rownum, $ rowsperpage );
While ($ product = $ this-> model-> getProduct ()){
// Bind data to HTML
}
}
//! An accessor
/**
* Returns the rendered HTML
* @ Return string
*/
Function display (){
Return $ this-> output;
}
}
?>
Finally, the Controller implements the view as a subclass.
<? Php
/**
* Controls the application
*/
Class ProductController extends ProductView {
//! A constructor.
/**
* Constucts a new ProductController object
* @ Param $ model an instance of the ProductModel class
* @ Param $ getvars the incoming http get method variables
*/
Function ProductController (& $ model, $ getvars = null ){
ProductView: ProductView ($ model );
$ This-> header ();
Switch ($ getvars ['view']) {
Case "product ":
$ This-> productItem ($ getvars ['id']);
Break;
Default:
If (empty ($ getvars ['rownum']) {
$ This-> productTable ();
} Else {
$ This-> productTable ($ getvars ['rownum']);
}
Break;
}
$ This-> footer ();
}
}
?>
Note that this is not the only way to implement MVC-for example, you can use controllers to implement models and integrate views at the same time. This is just a way to demonstrate the mode.
Our index. php file looks like this:
<? Php
Require_once ('lib/DataAccess. php ');
Require_once ('lib/ProductModel. php ');
Require_once ('lib/ProductView. php ');
Require_once ('lib/ProductController. php ');
$ Dao = & new DataAccess ('localhost', 'user', 'pass', 'dbname ');
$ ProductModel = & new ProductModel ($ dao );
$ ProductController = & new ProductController ($ productModel, $ _ GET );
Echo $ productController-> display ();
?>
Beautiful and simple.
We have some techniques for using controllers. You can do this in PHP:
$ This-> {$ _ GET ['method']} ($ _ GET ['param']);
One suggestion is that you 'd better define the namespace format (namespace) of the program URL, so that it will be more standard:
"Index. php? Class = ProductView & method = productItem & id = 4"
With this, we can process our controller as follows:
$ View = new $ _ GET ['class'];
$ View-> {$ _ GET ['method'] ($ _ GET ['id']);
Sometimes it is very difficult to establish a controller, for example, when you weigh between development speed and adaptability. A good place to be inspired is Apache group's Java Struts. Its controller is completely defined by the XML document.