Andy Hawthorne
I write and I code.
3 Ways to Use HMVC in Your CodeIgniter Projects
The most common design pattern for developing web applications is the Model-View-Controller (MVC) pattern. It was Ruby on Rails that made the pattern popular for web development back in 2005. Since then, there has been an increasing number of PHP frameworks appearing based on the MVC pattern. CodeIgniter was one of them.
Here, I will offer a brief explanation how 3 ways you might choose to go a step further, and use the Hierarchical Model Viewer (HMVC) Controller pattern.
HMVC – What’s That?
HMVC is a direct extension of the MVC pattern and attempts to solve some of the scalability problems that can occur with MVC. For a small web application MVC works fine, especially with CodeIgniter because it offers a lot of flexibility.
The problems begin when you have a need to modularise things a little bit. For example, you might have an admin section that needs to be separate from the rest of the app. In an e-commerce web site, you typically have production selection, a shopping basket, and a checkout.
HMVC introduces the idea of ‘triads’. You can think of a triad as a single MVC collection working in conjunction with other MVC collections. A collection is made up of models, views, and controllers. What that means is, in the e-commerce scenario described above for example, you can create ‘modules’ of code to keep things organised.
It also means you can create reusable components that are available to other triads within the application. Say you need to keep track of a users login for example, while there are ways to do it through a regular MVC setup, with HMVC you can create a module specifically designed to handle logging in and user tracking. You will call the same set of code every time you need to work with a user instance.
The 3 Ideas
- Develop in Modules
The CodeIgniter project is working towards having support for HMVC built in. While you wait though, you can use the excellent HMVC extensions for CodeIgniter by Wiredesignz. In their simplest form, the extensions allow you to create modules that could easily be dropped into another CodeIgniter application.They provide a convenient way to create modular separation in your app, so that you can code say, and admin area , and a main application section in easy to maintain models. Each module can have its own controllers, views, and models, as well as helpers and libraries. - Build an API
The HMVC extensions allow you to create an Application Programming Interface (API) for your app. When I say API, I mean in the context of having core code components that can be called from anywhere within your application. The extensions allow you to load the methods of one controller, inside another. That is not something you can do normally. They also allow you to make calls to modules::run from a view. That means you can execute components without the main controllers needing to know about them. A bit like making an API call. - Create a multi-tiered site
Say you wanted one site for your customers, one site for your employees, and one site for the public at large. With HMVC you could achieve that with one CodeIgniter installation. That can easily be achieved with HMVC and the modular extensions. You could even employ it as way to create landing pages. a module for them, and other modules for the rest of the site.What about your own site? Your blog, your CV, and your portfolio could all be managed and maintained easily with HMVC.
The Bottom Line
You might be thinking that many of the things above could be achieved with regular MVC. To some extent that is true. However, when the application/site is going to get much bigger over time, HMVC can save you from some nasty structural problems.
Scalability is a word that gets thrown around a lot. But it is an important consideration for almost all web applications. HMVC might just hep you to scale your application for the future.
Tags: Codeigniter, HMVC, Modular Extensions, MVC

I don’t understand, “The problems begin when you have a need to modularise things a little bit.” – isn’t object orientation modular by design? It’s key principles being shared components, i.e. modularisation and re-use of said objects?
Furthermore, how is the ‘H’ component of ‘HMVC’ in anyway more appropriate than packages, modules or namespacing currently supported in most languages? You mention CodeIgniter, would it not, therefore, make more sense to abstract certain aspects of functionality (i.e. your example of login information) by delegating to a specific namespace within your application hierarchy, utilising the core language’s features and hooks than abstracting again through the CodeIgniter abstraction?
“isn’t object orientation modular by design?” I suppose it depends how you define modular. But no, I don’t think so. You could have a bunch of classes for creating various objects but it wouldn’t necessarily make them a module. To me, shared components are not the same thing as modules in the context of MVC.
Namespaces are only supported in PHP 5.3 and above. A lot of people are not using 5.3 yet for production. Where other languages are concerned is not relevant, to be fair, this article is concerned with PHP and the CodeIgniter framework.
I think you might have missed the concept of triads. That is collections of models, views, and controllers that can be re-used across the application, or other applications. There is nothing being abstracted there really. It’s hierarchical, not abstract, that’s the point.
In this article, I am specifically suggesting ways that HMVC could be used within a CodeIgniter based web app. The suggestions are things I have used myself, and I thought other CodeIgniter users might find them useful. Like most things, there is always more than one way to achieve something.