Example of the simplest MVC Mode

Source: Internet
Author: User

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
    1. <? PHP
    2. Require('Controller /'. Preg_replace ('/[^ A-z0-9 _] +/I','',$ _ Get['Controller']);
    3. ?>

One controller:

PHP code
  1. <? PHP
  2. // Obtain data from Model
  3. Require('Model/m1.php');
  4. $ M=NewM1 ();
  5. $ Data=$ M-> Getdata ();
  6. // Construct the view and display the output
  7. Require('View/v1.php');
  8. $ V=NewV1 ();
  9. $ V-> Assign ($ Data);
  10. $ V-> Display ();
  11. ?>

A model:

PHP code
    1. <? PHP
    2. ClassM1
    3. {
    4. FunctionGetdata (){
    5. Return 'Hello';
    6. }
    7. }
    8. ?>

One view:

PHP code
  1. <? PHP
  2. ClassV1
  3. {
  4. VaR $ Data;
  5. FunctionAssign ($ Data){
  6. $ This-> DATA =$ Data;
  7. }
  8. FunctionDisplay (){
  9. Echo $ This-> Data;
  10. }
  11. }
  12. ?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.