Setup HMVC with Codeigniter 3

HMVC stands for Hierarchical Model View Controller. Modular Extensions make the Codeigniter PHP framework modular. Modules are groups of independent components, typically model, controller and view, arranged in an application modules sub-directory that can be dropped into other Codeigniter applications. This allows easy distribution of independent components (MVC) in a single directory across other CodeIgniter applications.

You can also use HMVC with Codeigniter 2. Please read here


XAMPP in Windows
Netbeans 8.1
Codeigniter 3.0.6

Configure XAMPP and Netbeans

From Netbeans IDE go to Tools->Options. Click on PHP. Now on tab “General” browse the file for “PHP 5 Interpreter”. The php interpreter file generally placed inside the <physical drive in Windows OS>:xamppphpphp.exe

Configure Codeigniter and Netbeans

Create a new PHP project in Netbeans. Then remove the index.php file from the newly created project. Now copy the extracted files from Codeigniter 3.0.6 to the newly created project directory.

Setup HMVC with CodeIgniter

1. Download the modular package here hmvc
2. Extract the downloaded archive. Copy files from core directory to CodeIgniter’s application/core directory and copy files from third-party/MX to CodeIgniter’s application/third-party/MX.

codeigniter 3 hmvc
3. Create a new folder called modules under application (application/modules), where you will create your HMVC modules for a project.

codeigniter 3 hmvc
4. Open application/config/config.php file and add below code snippets to it.

Example of HMVC in action

Now if you want to use module specific configurations then you can put those config, routes etc. under each module’s config folder. Similarly you need to put module specific controller, model, view under each module’s controllers, models and views folders respectively.

codeigniter 3 hmvc

Site Module

codeigniter 3 hmvc

Create site directory under modules folder.

Create config, controllers, models, views folders under site directory.

Create MY_Controller class under application/core

Create a controller class Site in site.php file under modules/site/controllers with below source code

Create a view file called home.php under modules/site/views with below source code

Signin Module

codeigniter 3 hmvc
Create signin directory under modules folder.

Create config, controllers, models, views folders under signin directory.

Create a controller class Signin in signin.php file under modules/signin/controllers with below source code

Create a view file called signin.php under modules/signin/views with below source code

Now change the default controller in application/config/routes.php file because site module is our welcome or home module

Also make changes in application/config/autoload.php file

Run XAMPP control panel and start Apache server.

Now do right-click on the project and run it you will get below output.

codeigniter 3 hmvc

home page

codeigniter 3 hmvc

signin page
codeigniter 3 hmvc

Thanks for reading.

Soumitra Roy Sarkar

I am a professional Web developer, Enterprise Application developer, Software Engineer and Blogger. Connect me on Twitter Facebook  Google Plus Linkedin

12 Replies to “Setup HMVC with Codeigniter 3”

      1. I would like to do HMVC with directories.
        Not Route []. If I make records and within the MVC triad (Customers, Users, pordutos). Create new directory with name and tools within it a new MVC (Config, etc, etc). But if you call the url mysite / registrations / customers, does not work.
        I want HMVC with MVC modules within each folder that is inside modules
        modules-> register [customers (MVC), Products (MVC)]
        modules-> Tools [Config (MVC), Rules (MVC)]
        Sorry my English is translated.

  1. Hi very good your tutorial, I suffered a little bit to understand that it was necessary to change the ‘MY_’ prefix for ‘MX_’ thank you.

  2. Hello,

    I have problem to use this example HMVC, my CI version custom based in constructor functions giving me problem to load.

    Thanks by your help!

Leave a Reply

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