Home > Article > Backend Development > Example of php implementing mvc mode
They have no meaning in the documentation, all they mean is 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 the 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, using SOAP document replaces HTML document)" 2. Model Model represents 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 containing certain meanings, which will be displayed by the view. 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. 3. 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:
This code mixes process-oriented and Object's code, but for small sites this is usually the best option. Although the above code can still be optimized. Controllers are actually controls used to trigger bindings between the model's data and view elements. Here is a simple example using MVC pattern. First we need a database access class, which is an ordinary class.
Put the model on top of it.
Note: Between the model and the data access class, their interaction is never more than one line - no multiple lines are sent, which can quickly slow down the program. For the same program that uses schema classes, it only needs to keep one row (Row) in memory - the rest is given to the saved query resource (query resource) - in other words, we let MYSQL keep the results for us. Next is the view (the following code removes the html content).
Finally the controller, we will implement the view as a subclass. A constructor. /*** Controls the application $productModel =& new ProductModel($dao); */ require_once('lib/ProductView.php') ; require_once('lib/ProductController.php');
$dao=& new DataAccess ('localhost','user','pass','dbname'); $productController=& new ProductController($productModel,$_GET); echo $productController->display();?>
Sometimes building a controller is not an easy task, such as when there is a trade-off between development speed and adaptability. A good place to get inspiration is the Apache group's Java Struts, whose controllers are entirely defined by XML documents. |