The HMVC framework has a lot of benefits and that is why it is always a good idea to work with it in Code Igniter. In this tutorial, I will show you how we can use the HMVC approach.
The HMVC pattern and why?
Code Igniter follows the MVC pattern by default. But it is always a bonus when we can work on HMVC. This approach really helps in applications with a lot of modular function and also these modules can become reusable. For example, once a user module is created with all the “Authentication”, “User creation”, “Forgot password” and other general functions which are required by almost all applications, such modules really help.
Get the HMVC to work with CodeIgniter
To use the HMVC architecture in Code Igniter, we use a plug-in which you can download from this link:https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/wiki/Home. This package comes with two folders, and we need to add the files in the following folders in Code Igniter. First I have added the files inside the core folder and here is the screenshot for the same:
Once this is done, we need to add the content of the folder “third party”. Here I have added the full MX folder which has 8 files inside.
And here is the screenshot of the MX folder inside the third party. This is very important that you follow these steps exactly the same way for the HMVC structure to work correctly.
Once this is done, we can add modules inside a folder called modules inside the application module. Here is the screenshot of the modules folder inside the application folder of Code Igniter folder.
This is the folder where we can create our own reusable modules. Like I mentioned at the start, here is the structure for the user module that I have created. The modules folder should follow the structure where your controller is inside the “controllers” folder; the model is inside the “models” folder and the views are inside the “views” folder.