MVC implementation of PHP (1) 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 ?? A node in a DOM tree knows 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:
The following is a code snippet:
{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:
The following is a code snippet: 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.