Many friends have questioned the incorrect MVC pattern. This implementation is indeed incorrect if it is defined according to the classic MVC pattern.
In the classic MVC mode, the Controller only determines which view to call based on the request, and then calls the model to obtain the result and display it. In the following MVC mode, the Controller decides which model to call based on the request, passes the parameters to the model, and obtains the result. Finally, the results are passed to the view, which only displays the results.
The two implementations are compared as follows:
| |
Classic MVC Mode |
Alienated MVC model |
| Controller |
Only determines which view to call |
Call the model and pass the results obtained from the model to the view. |
| View |
You need to decide which model to call and display it after obtaining the result. |
Only display incoming data |
| Model |
Based on the returned results of the call, it does not have a relationship with the Controller and view. |
Based on the returned results of the call, it does not have a relationship with the Controller and view. |
| Advantages |
Separating the presentation layer from the business layer makes the Controller very simple. |
Separates the presentation layer from the business layer. The view is very simple and easier to combine with the template engine. |
| Disadvantages |
The view depends on the model and is easy to mix the business logic into the view. |
The Controller is more complex and easy to mix the business logic into the controller. |
The above table compares the differences and advantages between the classic MVC mode and the alienated MVC mode.
The fact is that the popular php development frameworks all adopt the alienated MVC model. This is because the alienated MVC mode is easier to combine with the template engine, and can be further improved with the template view, Front Controller, dispatcher, and other modes.ProgramIs structured.
For simple applications, the model provides database crud operations, while the Controller completes some of the business logic operations. If the template engine is not used, you can use include () in the Controller to load the template file (View) to display the results.
For complex applications, the model encapsulates the business logic, while the database operations are performed by the data source layer. In this case, the Controller only calls the model to obtain the result and then passes it to the view. Even if the template engine is used, the controller can use the controller base class method. This is not only simple,CodeSmall quantity, and different template engines can be easily used.
==============================================
Although this example is simple, it fully demonstrates the help of the MVC mode for separating the "presentation layer" and "business logic layer.
First, a scheduler is responsible for determining the Controller to be called Based on the HTTP request:
PHP code
- <? PHP
- Require('Controller /'. Preg_replace ('/[^ A-z0-9 _] +/I','',$ _ Get['Controller']);
- ?>
One controller:
PHP code
-
- <? PHP
-
- // Obtain data from Model
-
- Require('Model/m1.php');
-
- $ M=NewM1 ();
- $ Data=$ M-> Getdata ();
-
-
- // Construct the view and display the output
-
- Require('View/v1.php');
-
- $ V=NewV1 ();
-
- $ V-> Assign ($ Data);
-
- $ V-> Display ();
-
- ?>
A model:
PHP code
- <? PHP
- ClassM1
- {
- FunctionGetdata (){
- Return 'Hello';
- }
- }
- ?>
One view:
PHP code
-
- <? PHP
-
- ClassV1
-
- {
- VaR $ Data;
-
-
- FunctionAssign ($ Data){
-
- $ This-> DATA =$ Data;
-
- }
-
-
- FunctionDisplay (){
-
- Echo $ This-> Data;
- }
-
- }
-
- ?>