Implementation analysis of PHP MVC pattern in website architecture

  • 2020-03-31 20:23:03
  • OfStack

View.

"View" mainly refers to the final result that we send to the Web browser. Like our script generated HTML . When it comes to views, many people think of templates, but put The template The correctness of a scheme called a view is questionable.

Perhaps the most important thing for a view is that it should be "self aware." when a view is rendered, its elements are aware that they are larger The framework The role of.

In order to XML For example, you can say that when XML is parsed, the DOM API has this knowledge?? A node in a DOM tree knows where it is and what it contains. (when a node in an XML document is parsed with SAX, it only makes sense when the node is parsed.)

Most template schemes use simple process languages and template tags like this:

< p > {some_text} < /p >
< p > {some_more_text} < /p >

They have no meaning in the document, they represent only meaning PHP I'm going to replace it with something else.

If you agree with this loose description of the view, you will also agree that most template scenarios do not effectively separate the view from the model. Template tags will be replaced with what is stored in the model.

Ask yourself a few questions as you implement the view: "is it easy to replace the entire view?" "How long does it take to implement a new view?" "Can you easily replace the description language of a view? (such as replacing an HTML document with a SOAP document in the same view)"

Model

The model represents the program logic. (often referred to as the business layer in enterprise-level applications)

controller

Simply put, the controller is the first part of an incoming HTTP request invoked in a Web application. It checks for incoming requests, such as some GET variables, and gives appropriate feedback. It's hard to start writing other PHP code until you've written your first controller. The most common usage is the index.php structure like the switch statement:

Switch ($_GET [' viewpage ']) {
A case of "news" :
$page = new NewsRenderer;
Break;
Case "links" :
$page = new LinksRenderer;
Break;
Default:
$page = new HomePageRenderer;
Break;
}
$page - > display ();
? >

This code USES a mixture of procedural and procedural object But for small sites, this is usually the best option. Although the above code can be optimized.

A controller is actually a control that triggers the binding between the model's data and view elements.

example

Here is a simple example of using the MVC pattern.

First we need one The database Access class, which is a normal class.

/ * *
* A simple class for querying MySQL
* /
The class DataAccess {

Var $db;

Var $query; / / Query the resource

/ /! A constructor.

The function DataAccess ($host, $user and $pass, $db) {
$this - > db = mysql_pconnect ($host, $user and $pass);
The mysql_select_db ($db, $this - > db);
}

/ /! An accessor

The function the fetch ($SQL) {
$this-> query=mysql_unbuffered_query($SQL,$this-> db)

; / / Perform query here
}

/ /! An accessor

The function getRow () {
If ($row=mysql_fetch_array($this-> query,MYSQL_ASSOC))
Return $row;
The else
Return false;
}
}
? >

Put a model on top of it.


The class ProductModel {

Var $dao;

/ /! A constructor.

The function ProductModel (& $dao) {
$this - > dao = & $dao;
}

/ /! A manipulator

The function listProducts ($start = 1, $rows = 50) {
$this-> dao-> fetch("SELECT * FROM products LIMIT ".$start.", ".$rows ");
}

/ /! A manipulator

The function listProduct ($id) {
$this-> dao-> fetch("SELECT * FROM products WHERE PRODUCTID='".$id."'");
}

/ /! A manipulator

The function getProduct () {
If ($product=$this-> dao-> getRow())
Return $product;
The else
Return false;
}
}
? >

One thing to note is that between the model and the data access class, they never interact more than one line. No multiple lines are sent, which slows the program down quickly. The same program only needs to keep a Row in memory for classes that use the schema. The rest is given to the saved query resource?? In other words, we let MYSQL hold the results for us.

Next is the view?? I removed the HTML to save space, so you can see the full code for this article.


The class ProductView {

Var $model;


Var $output;

/ /! A constructor.

The function ProductView (& $model) {
$this - > model = & $model;
}

/ /! A manipulator

The function header () {

}

/ /! A manipulator

The function footer () {

}

/ /! A manipulator

The function productItem ($id = 1) {
$this- > model- > listProduct($id);
While ($product=$this- > model- > getProduct()) {
// Bind data to HTML
}
}

/ /! A manipulator

The function productTable ($rownum = 1) {
$rowsperpage = '20';
$this-> model-> listProducts($rownum,$rowsperpage);
While ($product=$this- > model- > getProduct()) {
// Bind data to HTML
}
}

/ /! An accessor

The function display () {
Return $this - > output;
}
}
? >

And finally, controller, we're going to implement the view as a subclass.


Class ProductController extends ProductView {

/ /! A constructor.

The 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 ();
}
}
? >

 

< img border = 0 onmousewheel = "javascript: the return of the big (this)" height = 238 Alt = "" SRC =" http://files.jb51.net/upload/2010-3/20100304185203723.gif "width = 400 Onload = "javascript: the if (this. Width > 498). This style. The width = 498;" Border = 0 >

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 one mode of demonstration methods .

Our index.php file looks like this:

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' and 'dbname');
$productModel = & new productModel ($dao);
$productController = & new productController ($productModel, $_GET);
Echo $productController - > display ();
? >

Beautiful and simple.

We have some tricks for using controllers, and in PHP you can do this:

$this - > {$_GET [' method ']} ($_GET [' param ']);

One suggestion is that you'd better define the namespace form of the program URL, so that it will be more formal, such as:

"Index.php? Class = = ProductView&method productItem&id = 4"

Through it we can handle our controller like this:

$view = new $_GET [' class '];
$view - > {$_GET [' method '] ($_GET [' id ']);


Related articles: