MVC vs HMVC for web application development

2019-01-22 12:21发布

问题:

I'm using MVC pattern for web development. I use codeIgniter framework. I found http://fuelphp.com/ and http://kohanaframework.org/ , they are using HMVC. It still not is clear of HMVC and why we should use HMVC instead of MVC ? I am confused after reading HMVC pattern and it's not too much different from MVC. Can you explain why we should use HMVC for web application development ?

回答1:

The Hierarchical-Model-View-Controller (HMVC) pattern is a direct extension to the MVC pattern that manages to solve many of the scalability issues already mentioned. HMVC was first described in a blog post entitled HMVC: The layered pattern for developing strong client tiers on the JavaWorld web site in July 2000. Much of the article concentrates on the benefits of using HMVC with graphical user interfaces. There has been some suggestion that the authors where actually re-interpreting another pattern called Presentation-Abstraction-Control (PAC) described in 1987. The article in JavaWorld provides a detailed explanation of how HMVC can aid in the design of desktop applications with GUIs. The focus of this article is to demonstrate how HMVC can be used to create scalable web applications.

HMVC is a collection of traditional MVC triads operating as one application. Each triad is completely independent and can execute without the presence of any other. All requests made to triads must use the controller interface, never loading models or libraries outside of their own domain. The triads physical location within the hosting environment is not important, as long as it is accessible from all other parts of the system. The distinct features of HMVC encourages the reuse of existing code, simplifies testing of disparate parts of the system and ensures that the application is easily enhanced or extended.

From Scaling Web Applications with HMVC by Sam de Freyssinet



回答2:

HMVC can do anything that MVC can and more

one of the things that you will or had struggled with in MVC is that you can't call a controller from another controller (unless you use a custom library or helper to do that)

in HMVC (Hierarchical Model View Controller) you can do that , i would sum all the qualities in here:

  • reuse your code in other projects
  • simplify maintaining your project
  • scale your project
  • organize code
  • modular structure

you will never go back to MVC after trying HMVC here is a picture to be more clear

for CodeIgniter use "Modular Extensions - HMVC"

Modular Extensions makes the CodeIgniter PHP framework modular. Modules are groups of independent components, typically model, controller and view, arranged in an application modules sub-directory that can be dropped into other CodeIgniter applications.

Module Controllers can be used as normal Controllers or HMVC Controllers and they can be used as widgets to help you build view partials.

if you want to use codeigniter with HMVC pattern, you can use Modular Extensions - HMVC here

https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc

and here are some nice and funny video tutorials that will get you on rails with HMCV in codeigniter

https://www.youtube.com/watch?v=8fy8E_C5_qQ&list=PLBEpR3pmwCawDZ6FgNYoyvicEz4HrJPec&index=1

hope that helps!



回答3:

Another HMVC framework that you should check out is Alloy. I have attempted to explain the concept in layman's terms on the HMVC Architecture manual page, and in this StackOverflow response to a similar question.