Some shortcomings of PHP framework MVC design

Source: Internet
Author: User
Currently, the mainstream PHP framework design modes are MVC. for example, yii or codeigniter, the controller receives page requests and communicates the interaction between the model and view. If we regard the website as a matrix and regard the website to receive and process user requests as the vertical structure of the website, we regard every module of the website (such as the article module, voting module, and forum module) it is regarded as a horizontal website.

Currently, the mainstream PHP framework design modes are MVC. for example, yii or codeigniter, the controller receives page requests and communicates the interaction between the model and view. If we regard the website as a matrix and regard the website to receive and process user requests as the vertical structure of the website, we regard every module of the website (such as the article module, voting module, and forum module) it is regarded as a horizontal website.

The MVC mode focuses on coordination between requests, processing, and presentation, while ignoring coordination between modules.

When a user initiates a request, the module used to process the user's request has been determined, which makes the module highly independent. how can this be understood, for example. Assume that you have two modules: the news module and the picture module. The news module displays some news articles and the picture module is used to display pictures. If MVC is used, we will implement the following: define two controllers: news and pic, two models: news_model and pic_model, two views: news_view and pic_view. when a user requests a Message Controller, call news_model to obtain the required data, use the data to render news_view, and finally present it to the user.

One day, your leadership suddenly came up with a new idea. HEY, dude, the news module only displays the news too monotonous, so that the news module can display the most popular pictures in the picture module. This is not easy to do. there is no image-related model in new_model, and there is no view showing the image list in news_view. Do you have a solution? Copy the models and views related to the displayed image list to the news model and View respectively? No. what if I want to display a list of popular images on the entire site? Are you still copying? No. Later maintenance will be exhausting.

In the codeigniter framework, a better solution is to make the function of displaying the image list into a library. you can call the library whenever needed, because the library has the same communication model View function as the controller. However, this brings about a new problem: data sharing. if pages are divided into different libraries, it is difficult to share data in different libraries. libraries are completely independent.

Now, all of this is because the mvc model focuses on vertical communication, while ignoring horizontal communication, completely splitting the relationship between modules.

Module communication

TextPattern (TXP for short) is a small BLOG system. its biggest feature is to focus on communication between modules, completely blurring the boundaries between different modules, and a request to users, TXP does not need to know what modules the user requests, news or images, but what functions the requested page displays. Each small function is completely independent.

This is a view-driven mode. after receiving a user request, the view to be requested is obtained first, and the view file calls different function points. This weakens the independence between modules. To put it simply, you can give anything you want to see, regardless of whether it is the same module or not. Wordpress is also based on the View-driven mode.

HMVC

Stacked MVC, an upgraded version of the MVC model. First, the system is divided by function points, and the implementation of each function point is then implemented in the MVC mode. HMVC combines both horizontal and vertical communication.

In my opinion, the ideal mode is: View-driven HMVC, which is based on the user. it is right to present what the user wants to see. HMVC ensures code reuse and is easy to maintain.

View-driven, you can learn from the implementation of WORDPRESS, because it is easy to develop and replace wordpress templates; the implementation of horizontal communication can refer to TXP; vertical communication is a typical MVC.

Address of this article: http://www.nowamagic.net/librarys/veda/detail/1693,welcome.

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.