Introduce HMVC into Codeigniter Application

As the market for the reusable components increases, the use of the HMVC(Hierarichal Model View Controller) has pushed up the stacks geometrically with the frameworks.

By default, the Codeigniter framework follows the Model-View-Controller approach. The ease of use and lightning speed of the framework has gained the framework a lot of importance in comparison to other frameworks such as Zend, CakePhp etc. The frameworks enable to users to use the core features of the language with the extended features that are available in the framework to ease the road to development.

One of the feature which the framework lacks is the reuse of the components to extensive level. You can create custom components in the MVC based codeigniter but the re-usability is available to a limited extent.
HMVC extensions enables the developers to extend and reuse the functionality to an extensive level with minimal modification to the underlying code. Use of the HMVC provides the following benefits to the developers:

  • Modularization: Reduction of dependencies between the disparate parts of the application.
  • Organization: Having a folder for each of the relevant triads makes for a lighter work load.
  • Reusability: By nature of the design it is easy to reuse nearly every piece of code.
  • Extendibility: Makes the application more extensible without sacrificing ease of maintenance.

This enables the developers to maintain the code to a better level with more focus on the business logic of the application.

This post will help you to successfully configure codeiginiter with the HMVC extensions.


CodeIgniter WebSite

  • Once downloaded, extract the content to htdocs/www folder of the local server. For eg. for MAMP: Goto Application->MAMP->htdocs. Your folder should be similar to the one displayed in the image below:

Screen Shot 2013-09-03 at 7.57.44 PM

  • Now download the HMVC extensions for the following location: HMVC Extensions
  • Extract the content of the compressed file. The contents of the folder will be similar to the one displayed below:

Screen Shot 2013-09-03 at 7.55.42 PM

  • In your CI installation add a folder named modules to it. Your CI application folder will look similar to this. The configuration of the Codeigniter on the local server is not specified in this post, It is assumed that you know how to configure the other properties in the CI config directory. You can also use some other name for your modules folder if you wish to, but in such a case you will be required to specify the path to folder in the config file. This can be done in a following way:

$config['modules_locations'] = array(APPPATH.'modules/' => '../modules/');

Screen Shot 2013-09-03 at 7.59.36 PM

  • Now copy the “core” and “third party” folder from your HMVC extension folder and copy it to the applications folder.
  • Your installation is ready to use the HMVC architecture.
  • Now create the module with a specified name, In this case the name of the module is core_main.
  • Create the folder for controllers,models and views respectively into the core_main folder. In HMVC, each module has their own respective controller,model and view folders.
  • Please note the name of the module should match the controller name else in such a case if you are using the routing feature in codeigniter then you have to specify the folder name along with the name of the controller.

Screen Shot 2013-09-03 at 8.03.02 PM

  • Defining a Controller – Normally we extend the CI_Controller class to extend the functionality of our classes but when using HMVC we will extend our controller class with MX_Controller. So now we define the controller in the following way:

class Core_main extends MX_Controller

Your system is now ready to use the HMVC extension.