MVC mode PHP implementation
Original: Harry Fuecks Translation: Easy CHEN MVC mode is very common in the website architecture. It allows us to build a three-story application, separating a useful layer from your code, helping designers and developers working together and improve our maintenance and expanding existing program.
View (View)
"View" mainly refers to our final result of the web browser - such as the HTML generated by our script. When it comes to the view, many people think of the template, but the correctness of the template scheme is worthy of being questionable.
For view, the most important thing may be that it should be "self aware", and the view is rendered (render), the elements of the view can realize that they are in a larger framework.
Take XML as an example, you can say that XML is in parsing, the DOM API has such a cognition - a node in a DOM tree know where it is and what it contains. (When the node in an XML document is parsed with SAX, it means that it is meaningless when resolving the node.)
Most template schemes use simple process language and such template tags:
{some_text} p>
{some_more_text} p>
They have no meaning in the document, and their representative is just that PHP will replace it with other things.
If you agree to this loose description of the view, you will agree to the vast majority of template schemes and there is no effective separation view and model. Template tags will be replaced in what stored in the model.
When you implement the view, ask yourself a few questions: "Is there a replacement of all views?" "How long does it take to implement a new view?" "Can I easily replace the description language of the view? (For example, use SOAP in the same view Document replacement HTML document) "
Model (Model)
The model represents the program logic. (The business layer is often referred to as a business layer in an enterprise program, the task of the model is to convert the original data into data containing certain significance, which will be displayed by the view. Typically, the model will pick up the data query, which may be inquired by some abstract data classes (data access layers). For example, you want to calculate the British annual rainfall (just to find a better holiday), the model will receive a daily rainfall in ten years, calculate the average, and then pass to the view.
Controller
A simple saying that the controller is part of the HTTP request that is entered in a web application. It checks the request received, such as some Get variables, making appropriate feedback. It is difficult to start writing other PHP code before writing your first controller. The most common usage is the structure of INDEX.PHP in the Switch statement:
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 mixes the code-oriented process and object, but for a small site, this is usually the best choice. Although the code on the upper side can be optimized.