Ready to use Codeigniter Modular Extensions HMVC

Posted by Darwin Biler on December 16, 2012

Hierarchical Model-View-Controller (HMVC) breaks down an application into manageable smaller components. The best analogy for this is your PC computer. A PC has smaller parts like power supply, microprocessor, hard disk, mouse, keyboard etc. Each part is doing specific function to make the whole PC run.

The advantage of this is when your hard disk malfunctioned, you just need to troubleshoot or replace the hard disk. Likewise, when other component seems malfunctioning, all you need to do is identify the part that is responsible for that functionality and just replace/fix it. You could even bring your hard disk with you and plug it in other computer and you can use your data right away without any hassle.

Going back to CodeIgniter, when you are developing complex CI application, let's say you have 3 different controllers for each different component of the application. The problem here is, those 3 different controllers shares the same set of models/views/helpers.

So let's say you just want to transfer 1 controller to another CI application, you need to trace all of the models/views/helpers it uses and transfer those as well. Sometimes, when you transfer those files to another complex CI application, it is easy to get conflicts on class/function names to the target application.


With all the controllers sharing the same set of models/libraries/models, it is also very hard to fix errors (specially logic errors) since a fix on one controller could conflict with another controller.

With HMVC, it will break down your CI application into independent, portable, easy to debug parts which enables developers to isolate different parts of the applications.

hmvc1

On the image above, you will see that the application is composed of 6 modules. These modules are like mini-applications tied together in a single application since each has its own set of Controllers/Models/View/Helpers.

Module 4 has no models, helpers or even view files of its own and just calls the Module 1 and 2 for its needs.
Module 5 and 6 has some of the files, but some functionality is also present in Module 2 and 3 (accessing the database for example). Instead of re-implementing the functionality, he just can call the other modules for it's needs w/o needing to know the internal details of the other modules.

This strategy makes the application easy to maintain since the dependency of each part is reduced and you can simply fix the defective modules w/o worrying that the change could possibly conflict with another.

Now, on the implementation part. Everything is the same like an ordinary CI application, except that you will just put the mini-applications in the applications/modules subfolder. Each folder in modules folder is like an isolated application of its own in a sense that each can have their own set of libraries/models/views/helpers completely isolated to each other.

If you had just happened to read the Modular Extensions HMVC stuffs on either via wiredesigns or nettuts+ you might find that it is quite tedious to do all those code changes/moving of files that they are asking for you to do just to get started in using the Hierarchical Model View Controller in CodeIgniter.

This post will not repeat all the tutorial and concepts which is already mentioned in those tutorials. But rather, it focuses on giving you the actual files that is needed to get started with CI-HMVC, so that you can skip ahead the gory details of setting it up by yourself and either start using it right away or have a glimpse on how do a working copy should look like when finished.

Note that this is just an ordinary CodeIgniter 2.1.0 installation with HMVC already setup and ready to be dropped in any folder in your web-server. The only thing you need to do is of course adjust the base_url in the config.php file and maybe some database settings (if you need to).

Click Here to download the zip file ( direct download)


Did you find this useful?

I'm always happy to help! You can show your support and appreciation by Buying me a coffee (I love coffee!).