Implementing the Hierarchical Model-View-Controller Design Pattern in PHP 5
(Page 1 of 4 )
Without a doubt, the Model-View-Controller design pattern (also known by its acronym as MVC) is a very popular architectural paradigm these days. This article, the first in a series, will show you how to implement a more advanced version of this design pattern, known as the Hierarchical Model-View-Controller pattern.
The Model-View-Controller pattern is popular for a number of reasons. First, its proper implementation allows developers to build web applications that separate the business and application logic from the visual presentation, and second, it can be applied either by using procedural programming or an object-oriented approach.
Due to its inherent flexibility and the number of benefits that it produces when it comes to developing more organized web-based programs, this pattern has been widely adopted by several frameworks in the last few years, including those built with PHP.
Whatís more, well-trusted PHP frameworks like Zend Framework, Kohana and CodeIgniter have adopted the MVC pattern from the very beginning as the default standard for leveraging the functionality of their building components. It's worth mentioning, though, that with the Zend Framework itís also possible to use its libraries without having to implement a full-featured MVC layer.
With all of the bells and whistles that the MVC paradigm offers, it seems that thereís nothing beyond it that can beat its unquestionable power, right? Well, fortunately thatís not entirely true. It's feasible to take the pattern one step further through the implementation of a more advanced concept called the Hierarchical Model-View-Controller pattern (or simply HMVC). Definitely, thatís a long and intimidating name, but understanding the underlying logic that stands behind HMVC is a pretty straightforward process, trust me.
Without digging too deeply into complex theoretical concepts, the typical cycle of operation of a web application that uses the MVC pattern establishes a one-to-one relationship between an HTTP request and a controller. In other words, thereís always only one controller responsible for handling a single request, which may require using one or more models to interact with the data layer, and one or more views that will display some output on the browser, generally in the form of (X)HTML pages. However, as I said before, the one-to-one mapping between a request and a controller is always maintained.
Things become more interesting when the operation cycle described above can be extended to the point where a single HTTP request is handled by one of more triads composed individually of a Model, a Controller and a View. In a scenario like this, the initial relationship that maps a request to a single controller is extended to the scope of multiple controllers, in this way allowing developers to create a hierarchy of MVC triads completely isolated from each other that perform specific tasks.
This is, in raw terms, the rationale behind the HMVC design pattern. Far from being an academic definition of the pattern, this brief description should give you an exact idea of how it works and how to implement it specifically in PHP 5.
Not a clue yet on this latter? Well, fear not, since in the lines to come Iím going to demonstrate by way of a hands-on approach how to use the HMVC design pattern within an object-oriented PHP 5 framework, so you can grasp its key concepts and apply them to take your own web applications to the next level of modularity. Now, let's begin.