MVC pattern is very common in website architecture. It allows us to build a three-tier application, separating useful layers from the code, helping designers and developers work together and improving our ability to maintain and extend existing applications.
View (View)
"View" mainly refers to the final result we send to the web browser - such as the HTML generated by our script. When talking about views, many people think of templates, but the correctness of calling template solutions views is questionable.
Perhaps the most important thing about a view is that it should be "self aware". When the view is rendered, the elements of the view are aware of themselves within the larger frame. role.
Taking XML as an example, it can be said that when XML is parsed, the DOM API has such knowledge?? A node in the 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 that node is parsed.)
The vast majority of template schemes use simple procedural languages and template tags like this:
<p>{some_text}</p> <p>{some_more_text}</p> Copy after login |
They have no meaning in the documentation, all they mean is that PHP will replace it with something else.
If you agree with this loose description of views, you will also agree that most template solutions do not effectively separate views and models. The template tag will be replaced with whatever is stored in the model.
Ask yourself a few questions when you implement a view: "Is it easy to replace the entire view?" "How long does it take to implement a new view?" "Is it easy to replace the view's description language? (For example, in Replace HTML documents with SOAP documents in the same view)"
Model (Model)
The model represents the program logic. (Often called the business layer in enterprise-level programs)
In general, the task of the model is to convert the original data into data that contains certain meanings, which will be viewed by the view. show. Typically, the model will encapsulate data queries, perhaps through some abstract data class (data access layer) to implement the query. For example, if you wish to calculate the annual rainfall in the UK (just to find yourself a nice holiday spot), the model will receive the daily rainfall for ten years, calculate the average, and pass it to the view.
Controller
Simply put, the controller is the first part called by the incoming HTTP request in the web application. It checks the received request, such as some GET variables, and makes appropriate feedback. It's difficult to start writing other PHP code until you write your first controller. The most common usage is a structure like 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(); ?> Copy after login |
This code mixes procedural and object-oriented code, but for small sites, this is usually best choose. Although the above code can still be optimized.
Controllers are actually controls used to trigger bindings between model data and view elements.
Example
Here is a simple example using the MVC pattern.
First we need a database access 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) Copy after login ; // 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; } } ?> Copy after login |
1