HMVC in Codeigniter

Modular HMVC = Multiple MVC triads
This is most useful when you need to load a view and its data within a view. Think about adding a shopping cart to a page. The shopping cart needs its own controller which may call a model to get cart data. Then the controller needs to load the data into a view. So instead of the main controller handling the page and the shopping cart, the shopping cart MVC can be loaded directly in the page. The main controller doesn’t need to know about it, and is totally isolated from it.

In CI we can’t call more than 1 controller per request. Therefore, to achieve HMVC, we have to simulate controllers. It can be done with libraries, or with this “Modular Extensions HMVC” contribution.

The differences between using a library and a “Modular HMVC” HMVC class is: 1) No need to get and use the CI instance within an HMVC class 2) HMVC classes are stored in a modules directory as opposed to the libraries directory.The image below illustrates how this works

Each triad functions independently from one another. A triad can request access to another triad via their controllers. Both of these points allow the application to be distributed over multiple locations, if needed. In addition, the layering of MVC triads allows for a more in depth and robust application development. This leads to several advantages which brings us to our next point.

Key advantages to implementing the HMVC pattern in your development cycle:

  • 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.

These advantages will allow you to get M.O.R.E out of your application with less headaches.

[ad name=”ad-1″]

Modular Extensions installation

1) Start with a clean CI install.

2) Set $config[‘base_url’] correctly for your installation.

3) Download Modular extension here

4) Drop Modular Extensions third_party files into the CI 2.0 application/third_party directory.

5) Drop Modular Extensions core files into application/core, the MY_Controller.php file is not required unless you wish to create your own Controller extension.

6) Access the URL /index.php/welcome => shows Welcome to CodeIgniter

7) Create module directory structure application/modules/welcome/controllers.

8) Move controller application/controllers/welcome.php to application/modules/welcome/controllers/welcome.php.

9) Access the URL /index.php/welcome => shows Welcome to CodeIgniter

10) Create directory application/modules/welcome/views.

11) Move view application/views/welcome_message.php to application/modules/welcome/views/welcome_message.php

12) Access the URL /index.php/welcome => shows Welcome to CodeIgniter

You should now have a running Modular Extensions installation.

Source 1.


Now we try to make Guestbook example

  1. create the  guestbook table consist of 2 3 field(id_guestbook,name,message)
  2. set your database based on your config in ci_directory/application/config/database.php
  3. set autoload library(database),helper(url,form)
  4. Create directory guestbook under directory modules

the complete directory similar to these




5.Create controller guestbook.php and save it under modules/guestbook/controllers

<?php if ( ! defined(‘BASEPATH’)) exit(‘No direct script access allowed’);

class Guestbook extends MX_Controller {

function __construct(){

public function index()
$this->form_validation->set_rules(‘name’, ‘Name’, ‘required’);
$this->form_validation->set_rules(‘message’, ‘Message’, ‘required’);
if ($this->form_validation->run() == FALSE)


6.Create Model model_guestbook.php and save it under modules/guestbook/models

class Model_Guestbook extends CI_Model {
function __construct()
// Call the Model constructor

function insert_entry($data)
$this->db->insert(‘guestbook’, $data);

function get_entry()
return $query;

7. Create view guestbook.php and save it under modules/guestbook/views/

<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8">

<style type="text/css">

body {
background-color: #fff;
margin: 40px;
font-family: Lucida Grande, Verdana, Sans-serif;
font-size: 14px;
color: #4F5155;

a {
color: #003399;
background-color: transparent;
font-weight: normal;

h1 {
color: #444;
background-color: transparent;
border-bottom: 1px solid #D0D0D0;
font-size: 16px;
font-weight: bold;
margin: 24px 0 2px 0;
padding: 5px 0 6px 0;

code {
font-family: Monaco, Verdana, Sans-serif;
font-size: 12px;
background-color: #f9f9f9;
border: 1px solid #D0D0D0;
color: #002166;
display: block;
margin: 14px 0 14px 0;
padding: 12px 10px 12px 10px;


foreach($data->result_array() as $row)
echo $row[‘name’];
echo "<p>".$row[‘message’]."</p><hr>";
<?php echo validation_errors(); ?>
<?php echo form_open(‘guestbook/index’); ?>
<td>Name</td><td><input type="text" name="name"></td>
<td>Messaage</td><td><textarea name="message" cols="16" rows="5"></textarea></td>
<td colspan="2"><input type="submit" name="submit" value="submit"><td>
<p>Powered by Codeigniter</p>



You can download my complete file here and the database here


9 Responses to “HMVC in Codeigniter”

  1. Thank’s so much for great tutorial…

  2. Everything is very open with a clear description of the challenges. It was really informative. Your site is very useful. Many thanks for sharing!

  3. Some really interesting information, well written and generally user friendly.

  4. Hello, i feel that i noticed you visited my website thus i got here to “go back the want”.I am trying to in finding issues to improve my site!I assume its good enough to make use of a few of your ideas!!

  5. Thanks. It really helps me to understand HMVC

  6. Rose Anne Says:
    May 15, 2012 at 3:58 am

    How to put two or more controller name in one module?

  7. Thank you stephanie for this great post

  8. I am glad to see this you tube video at this website, therefore right now I am also going to add all my movies at YouTube web site.

Leave a Reply