On PHP framework design pattern and the defects of MVC

Source: Internet
Author: User
Tags php framework codeigniter

The current mainstream PHP framework design patterns are MVC patterns, such as Yii or CodeIgniter, are received by the Controller page request, and communicate the model and the interaction of the view. If we think of the whole site as a matrix, the site to receive user requests and processing as a vertical site, and the site of each module (such as the article module, voting module, forum module, etc.) as the site's landscape. So we can draw a diagram like this:

Module 1 Module 2 Module 3
User Request----------------------
|      | |
|      | |
Data processing----------------------
|      | |
|      | |
Page Rendering----------------------

The flaws of MVC
The MVC pattern, which focuses on the coordination between the vertical request, the processing, the rendering, and ignores the coordination between the modules, we can see from the diagram that when the user initiates the request, which module handles the user's request has been determined, which makes the module very strong independence, how to understand, for example.
Suppose now, you have two modules, one is the news module, one is the picture module, the news module shows some news articles, the picture module is used to display the picture.
In the case of MVC, we implement this: Define two controller news,pic, two model News_model,pic_model, two view News_view,pic_view, and call News_ when a user requests the news controller The model obtains the required data and then renders the news_view with the data, which is finally presented to the user.
One day, your leader suddenly has a new idea, HEY, buddy, news module only shows the news is too monotonous, let the news module, can display the most popular pictures in the picture module. This is not easy to do, New_model there is no picture-related model, News_view also does not show the view of the picture list. You got a solution? Copy the models and views associated with the display picture list to the news model and view separately? No, this is not good, if I now all the station to display a list of popular pictures What do you do? Do you copy it? No, you can't. Post-Maintenance will be exhausting people.
In the CodeIgniter framework, there is a better solution, is to display the Picture list this function as a library, every need, call the library can be, because the library has the same function as the controller of the communication Model view. But this will bring a new problem: data sharing, if the page is divided into different libraries to achieve, different library data is difficult to share, the library is completely independent.
Now, all of this is because the MVC pattern focuses on vertical communication and ignores horizontal communication, which completely separates the relationship between modules.

Module communication
TextPattern (abbreviation TXP) is a small blog system, its biggest feature is to pay attention to the communication between the modules, completely blurred the boundaries between the different modules, a request to the user, Txp absolutely no need to know what the user is requesting the module, news or pictures, Just know what feature points the requested page will render. Each small function point is completely independent.
This is a view-driven pattern that, after receiving a user request, first takes the view to be requested, and the view file invokes a different function point, thus weakening the independence between the modules. Popular Little said: users want to see what, you give what, do not care it is not the same module, is not related. WordPress is also based on a view-driven pattern.


Hmvc
cascading MVC, which is an upgraded version of the MVC pattern. First of all, the system according to the function points, the implementation of each function point and then adopt the MVC mode. HMVC also takes into account both horizontal and vertical communication.

What I think is the ideal pattern
View-driven HMVC, based on view-driven, is user-based, presenting users want to see, demand-driven is right. HMVC guarantees the reuse of code and is easy to maintain.
View-driven, can learn from the implementation of WordPress, because WordPress template development, replacement is easy, the implementation of horizontal communication can learn from TXP, vertical communication is a typical MVC.

Source: http://baiyuxiong.iteye.com/blog/833996

On PHP framework design pattern and the defects of MVC

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.