MVC definition
Model-View-Controller: It is a software architecture pattern in software engineering, which divides the software system into three basic parts: model (Model) and view (View) and Controller (Controller).
M(Model) Model: The core function of the application, managing the data and values used in this module;
V(View) View: The view provides the display of the model and manages how the model is displayed to the user. It is the appearance of the application;
C (Controller) controller: reacts to user input, manages the interaction between the user and the view, and is the hub connecting the model and the view.
How MVC works
MVC is a design pattern that enforces separation of input, processing, and output of an application. Applications using MVC are divided into three core components: model, view, and controller. They each handle their own tasks.
1. View V
A view is the interface that users see and interact with. For old-fashioned Web applications, the view is an interface composed of HTML elements. In new-style Web applications, HTML still plays an important role in the view, but some new technologies have emerged in endlessly, including Macromedia Flash and Some markup languages and Web services like XHTML, XML/XSL, WML, etc. One of the big benefits of MVC is that it can handle many different views for your application. No real processing occurs in the view, whether the data is stored online or a list of employees. As a view, it just serves as a way to output the data and allow the user to manipulate it.
2. Model M
The model represents enterprise data and business rules. Among the three components of MVC, the model has the most processing tasks. The data returned by the model is neutral, which means that the model has nothing to do with the data format, so that a model can provide data for multiple views. Code duplication is reduced because the code applied to the model only needs to be written once and can be reused by multiple views.
3. Controller C
The controller accepts user input and calls models and views to complete the user's needs. So when a hyperlink in a Web page is clicked and an HTML form is sent, the controller itself does not output anything or do any processing. It just receives the request and decides which model component to call to handle the request, and then determines which view to use to display the returned data.
Advantages of the MVC framework pattern
1. Developers can only focus on one layer of the entire structure;
2. It is easy to replace the original level of implementation with a new implementation;
3. It can reduce the dependence between layers;
4. Conducive to standardization;
5. It is conducive to the reuse of logic at each layer.
Disadvantages of the MVC framework pattern
1. Increases the complexity of the system structure and implementation. For simple interfaces, strictly following MVC and separating the model, view and controller will increase the complexity of the structure, may generate too many update operations, and reduce operating efficiency.
2. Too tight connection between view and controller. The view and the controller are components that are separated from each other, but are indeed closely related. The view does not have the existence of the controller, and its application is very limited, and vice versa, which hinders their independent reuse.
3. View’s inefficient access to model data. Depending on the model operation interface, the view may need multiple calls to obtain enough display data. Unnecessarily frequent access to unchanged data will also harm operational performance.
The above is the detailed content of What is MVC framework. For more information, please follow other related articles on the PHP Chinese website!