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 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 (standardization is getting closer and closer) as an example, we can say that xml (standardization is getting closer and closer) is being parsed, DOM APIs have such cognition that a node in a DOM tree knows where it is and what it contains. (When a node in an xml (standardization is getting closer and closer) file 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:
{Some_text}
{Some_more_text}
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:
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.
/**
* A simple class for querying MySQL (the best combination with PHP)
*/
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 (the best combination with PHP) _ pconnect ($ host, $ user, $ pass );
MySQL (best combination with PHP) _ select_db ($ db, $ this-> db );
3 pages, 1st pages, 2nd pages, 3rd pages