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.

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. https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/wiki/Home

2.http://net.tutsplus.com/tutorials/php/hvmc-an-introduction-and-application/

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

modules/guestbook/controllers

modules/guestbook/models

modules/guestbook/views

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

load->library('form_validation');
	}

	public function index()
	{
		$this->load->model("model_guestbook");
		$this->form_validation->set_rules('name', 'Name', 'required');
		$this->form_validation->set_rules('message', 'Message', 'required');
		if ($this->form_validation->run() == FALSE)
		{
			$data['data']=$this->model_guestbook->get_entry();
			$this->load->view('guestbook',$data);
		}
		else
		{
			$data=array(
			"name"=>$this->input->post("name"),
			"message"=>$this->input->post("message")
			);
			$this->model_guestbook->insert_entry($data);
			redirect("guestbook/index");
		}
	}

}

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

db->insert('guestbook', $data);
    }

	function get_entry()
    {
		$query=$this->db->get("guestbook");
		return $query;
	}
}

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




	
	Guestbook






Guestbook

result_array() as $row) { echo $row['name']; echo "

".$row['message']."


"; } ?>
Name
Messaage

Powered by Codeigniter

You can download my complete file here and the database here

9 thoughts on “HMVC in Codeigniter

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

  2. 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!!

    • I dont think that is possible .. Ive searched a lot but 2 controllers inside a single controllers folder of a module seemed inpossible in HMVC for codeigniter..

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

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

*
*
Website