What is the HMVC pattern?
Asked Answered
F

5

133

Reading Kohana's documentation, I found out that the main difference in 3.0 version is that it follows the HMVC pattern instead of MVC as version 2.x does. The page about this in Kohana's docs and the one on wikipedia didn't really give me a clear idea.

So question: what is the HMVC pattern and how does it differ from MVC?

Flabellate answered 15/2, 2010 at 0:15 Comment(2)
A discussion on this very topic took place within the Kohana forums. You may find it to be of help: forum.kohanaframework.org/discussion/1681Pedaias
#18161641Discernible
D
86

Sam de Freyssinet (one of the Kohana developers) wrote a rather in-depth article about HMVC, what it is, and how it can be used.

Link is dead: New Link - https://web.archive.org/web/20160214073806/http://techportal.inviqa.com/2010/02/22/scaling-web-applications-with-hmvc/

Doit answered 1/3, 2010 at 13:14 Comment(3)
thanks for a good link , also checkout this javaworld.com/jw-07-2000/jw-0721-hmvc.htmlGaston
@Sithu web.archive.org/web/20160220044633/http://techportal.inviqa.com/…Charlot
Links always will die! post content instead of link.Peracid
F
59

I am currently in the process of developing my own PHP 5.3 HMVC framework called Alloy. Since I am heavily invested in and sold on HMVC, I thought I could offer a different view point, and perhaps a better explanation of why HMVC should be used and the benefits it brings.

The largest practical benefit of using an HMVC architecture is the "widgetization" of content structures. An example might be comments, ratings, Twitter or blog RSS feed displays, or the display of shopping cart contents for an e-commerce website. It is essentially a piece of content that needs to be displayed across multiple pages, and possibly even in different places, depending on the context of the main HTTP request.

Traditional MVC frameworks generally don't provide a direct answer for these types of content structures, so people generally end up duplicating and switching layouts, using custom helpers, creating their own widget structures or library files, or pulling in unrelated data from the main requested Controller to push through to the View and render in a partial. None of these are particularly good options, because the responsibility of rendering a particular piece of content or loading required data ends up leaking into multiple areas and getting duplicated in the places it is used.

HMVC, or specifically the ability to dispatch sub-requests to a Controller to handle these responsibilities is the obvious solution. If you think about what you're doing, it fits the Controller structure exactly. You need to load some data about comments, and display them in HTML format. So you send a request to the comments Controller with some params, it interacts with the Model, picks a View, and the View displays the content. The only difference is you want the comments displayed inline, below the blog article the user is viewing instead of a completely separate full comments page (though with an HMVC approach, you can actually serve both internal and external requests with the same controller and "kill two birds with one stone", as the saying goes). In this regard, HMVC is really just a natural byproduct of striving for increased code modularity, re-usability, and maintaining a better separation of concerns. THIS is the selling point of HMVC.

So while Sam de Freyssinet's TechPortal article on scaling out with HMVC is interesting to think about, it's not where 90%+ of the people who use HMVC frameworks are going to get real, practical, day-to-day benefits from it.

Forwent answered 20/4, 2011 at 20:21 Comment(5)
Yes this is how I imagined it in real world use, but from this point of view the name isn't quite fitting as the H in HMVC is misleading (there is no real hierarchy).Flabellate
Yes, you make a good point. I actually share this point of view and gave it another name - "Nested MVC" - in a presentation I did on Alloy at Confoo 2011. It's up on Slideshare, slide #20: slideshare.net/vlucas/alloy-hmvc-php-frameworkForwent
How would HMVC handle the need for multiple returns from the tree of modules? Eg collating head/body/footer content, JS/Css dependencies and interrelations between modules. Events? Hooks? A singleton page framework? Structured return objects?Violante
This answer is a copy of wikipedia :/ en.wikipedia.org/wiki/…Detestation
@Detestation looks like someone copied the answer I gave here, and then added it to Wikipedia (it wasn't me). Check the "References" section at the bottom of the Wikipedia article - it links back to this comment.Forwent
E
7

HMVC is closely related to the "component based" approach to dispatching. Basically, instead of having a single dispatcher, which delegates to a controller, each controller can act as a dispatcher it self. This gives you a hierarchy of controllers. The design is more flexible and causes better encapsulation of code, but at a price of higher abstraction. Konstrukt is designed around this pattern.

See also this answer: https://stackoverflow.com/questions/115629/simplest-php-routing-framework/120411#120411

Exurb answered 15/2, 2010 at 10:43 Comment(0)
P
7

In Kohana, at least, an HMVC request is a HTTP request that is serviced "internally": instead of being issued over the network, it's routed, dispatched and handled by the framework itself. The similarity of the names "HMVC" and "MVC" is confusing in that it suggests an underlying connection between the terms that does not in fact exist: one is not a minor variant or modification of the other, they are completely different things. (HMVC is also described as Ajax without the client-side HTTP request.) Kohana's emphasis on, and support for "HMVC" means that the framework has strong support for a HTTP-based service oriented architecture.

The advantage of this architectural pattern is that since the same "calling convention" is used for internal and external requests, it's trivial to convert "internal" service requests to "external" requests or vice versa as the need arises.

Whilst this is a sensible architectural pattern, giving it its own name seems unnecessary (Symfony2 describes the same concept "sub-requests"), and in fact the name seems to be a misnomer: there's no particular requirement or need that the requests form a hierarchy (other than the standard call graph of every imperative program); the requests could easily be recursive, for example.

[Update Apr 2011, Mar 2012: Expanded on answer in response to comments.]

Pinko answered 5/7, 2010 at 22:33 Comment(4)
Being able to convert 'internal' service requests to 'external' requests menas you can scale-out more easily if required, i.e. move some application modules to their own server.Hebrews
yeah, try implementing an internal web service with it and without it, just to see if it actually "doesn't really matter that much".Malvia
@Malvia I think it's a fine architecture, I just think the name is confusing, and it implies that Kohana is doing something particularly unusual.Pinko
I'm not sure how your answer is helpful. You're not answering the question just complaining about the name and that it's unnecessary (which is fine).Denverdeny
R
5

HMVC is Hierarchical Model View Controller.In in normal MVC every GUI object has its MVC.But there is no any relation between the parent GUI object and Child GUI object unlike HMVC. In HMVC each GUI object has access to its child objects and each of child object can access to its parent object.

So in every view there is a parent view.Through which it can access it parent view. For in every controller there is a parent controller through which It can pass the event to parent controller (If event is not in its scope.)

For details description please click here

New link is this address

Rolan answered 7/6, 2011 at 9:39 Comment(3)
the mark of a good answer is not just a link with no other information or context. Please can you expend your answer and summarize the relevant portion of the linked post?Winthorpe
@Sanjay, any reason you changed the destination of the link from the HMVC article to one on the state of gwt for mobile?Hartsock
@Koch..I havn't change link...even i don't know who changed it....btw I linked it to original link.Rolan

© 2022 - 2024 — McMap. All rights reserved.