HMVC: Modular applications in CodeIgniter

Today we will make an introduction to how to create modular applications in the framework CodeIgniter using the HMVC extension (Hierarchical Model View Controller), so that our application gains in flexibility and we can split the project in parts that could be developed in parallel between multiple developers once the modules have been designed, with the valuable time saved to finish the project. HMVC allows us to have the same functionality offered by the “Bundles” in Symfony2 or the modules in the ZendFramework2.

Installing HMVC in CodeIgniter

The first thing we need to do in order to modularize our application is to download the HMVC extension. Once we unzip it, we find two folders inside:


We put the “third_party” folder (as it is) inside the “application” folder of our CodeIgniter application. The final location of the core folder depends on the version of CodeIgniter that we have installed. Therefore, the contents of the folder “core” will be in:

application/core (if we have CodeIgniter 2.0 or superior)
If we have CodeIgniter 1.7.3:
into application/libraries we will put the content of the “core” folder (3 files)

Well, with this we have installed the extension that allows us to modularize our application, but we need a final step. Inside the “application” folder we must create a directory called “modules” (application/modules). This directory (as its name suggests) will contain all the modules that we will develop.

Developing modules in our application

Once everything is installed and all necessary directories created, we can start to develop modules that give functionality to our web application. From here, the only limits are the programmer’s creativity and genius, but there are only a few rules to follow.

The name of the directory containing your module will determine the name of the module and hence the url that users will see in their navigation bar. That is, if the name of our module is “vehicles_module”, in which we have a “controllers” directory with 2 controllers: “car” and “truck” and in each controller we have a function called “iam_car” and “iam_truck”, the final url would be: start parameters) start parameters)

It is also important to remark that if we have a module called, for instance, “user”, which contains a controller called “user.php” (modules/user/controllers/user.php) which in turn contains a method called, for example, “login”, the url to access the method could be:


so, when the module’s name and the name of the controller match, we can ignore the latter in the url access. But only when they coincide.

Within the “modules” directory, the directories structure that we follow must be the same that we found in the “application” directory itself, hence the name HMVC (hierarchical MVC). So that, at least, we should have a “controllers” directory and a “views” directory, we could also have a “models” directory, or even a directory “libraries”, if we need specific libraries for our module that other modules do not need or should not have access to. Also a “config” directory if your module needs certain configuration variables that are not common to the entire application, etc. We can also create within the module’s directory more directories that are not part of the usual structure of CodeIgniter’s “application” folder, such as a directory “files”, where we could temporarily store certain files that a particular module generates, etc.

As we said before, from here the developer sets the limits.

For more help, you can take a look at

Leave a Reply

Your email address will not be published. Required fields are marked *