Andy Hawthorne

I write and I code.

HMVC: an Implementation Example with CodeIgniter

HMVC and modular separation can make life easier, when you need to add new features to a web app. I’ve been working on a project using the CodeIgniter PHP framework. Recently, there has been a feature request for a review system. There are 2 main aspects to the requirement: a moderation capability on the corporate intranet, and a place for customers to write their reviews on the production web site.

Modular separation and HMVC made this much easier. Read on to find out why…

The Problem

I can’t go into too much detail about the client, or the specific requirements. What I can do, is describe how modular separation made what could have been a nightmare, into something manageable.

The main problem was: a major re-build of the intranet had been completed, and was sitting on the clients staging server. The reviews feature was needed immediately. The client couldn’t merge the releases because final testing was still ongoing. So ideally, I needed a ‘drop-in ‘ solution that would barely impact on the production applications.

As I said, the 2 applications were already built on the CodeIgniter framework. They were not built on top of the same installation though.

The intranet already contained some code for handling the CRUD operations for reviews. The company in question were sending out paper-based reviews, and then using the system to post the replies to the production web site, via the intranet.

The Requirements

What they now wanted to do was:

  1. send out an email link to a review form, for the product the customer had purchased
  2. have the emails generated automatically at the point where the date was 6 weeks from the customers purchase date.
  3. Have the customer responses post into a moderation system that could be accessed via the corporate intranet.

The HMVC Solution

As I said, there was some code already looking after the CRUD operation. So what I did was the following:
  1. Added the HMVC Modular Extensions for CodeIgniter to both the production web site and the intranet
  2. Created a module for reviews in the intranet build. Then I moved the existing CRUD code in. The only modification it needed really was to extend the controller classes with MX_Controller rather than CI_Controller. That step wasn’t strictly necessary, but it gave me the option to use the powerful Modules::run feature should I need it.
  3. I could then de-couple the code almost completely. The reviews module has it’s own models, views, and controllers. If it needed them, it could have helpers and libraries too.
  4. Installing the modular extensions doesn’t require much effort, and importantly, doesn’t impact on the rest of the application. Only the controllers that need to respond to calls to Modules::run have to extend MX_Controller. Everything else can stay the same – hello modular separation.
  5. The screenshot below is obviously not the actual code, but it demonstrates how the implementation works – and one of the major benefits of modular separation.  The config option seen in the screenshot is important:

    hmvc

    HMVC extensions installed in CodeIgniter 2.1

    $config['modules_locations'] = array('modules/' => '../../modules/',
    );
    

    It allows the modules directory to be held outside of the application directory.

  6. I did much the same thing for the production web site. By including the files necessary for customers to add their reviews in a module, it isolated the code from everything else. It still integrated it with the application as a whole though.

What About the HMVC?

At the moment what we have is a good example of modular separation.  To add a really simple example of HMVC:

  1. I’ll actually create a reviews controller in modules/reviews/controllers.
  2. We’ll create another module too. Call it ‘example’, and add a sub-folder for the controllers.
  3. Then create an example controller in modules/example. Name it example.php.
  4. Add the following to the example controller:
    class Example extends MX_Controller
    {
        public function __construct()
        {
            parent:: __construct();
        }
        public function print_a_message()
        {
            return "Hello, from HMVC.";
        }
    }
    

    Here, we are just creating a simple method that we will call from the reviews controller by cross loading it via HMVC.

  5. Back in the reviews controller, we can load our example controller, and call the print_a_message() method:
    class Reviews extends MX_Controller
    {
        public function __construct()
        {
            parent:: __construct();
            //$this->load->module('module/controller');
            $this->load->module('example/example');}
    
        public function index()
        {
            echo $this->example->print_a_message();
        }
    }
    

    As the comment in the constructor describes, $this->load->module is used to load a controller from any other module. You might be wondering what the point of that is. Think of it this way: with HMVC you can cross-load the methods in any controller within the module hierarchy. That means you can create reusable ‘triads’ of code (models, views, and controllers), not just reusable in this application, but also in others too. You might need to play with the code a little, in fact definitely play around with it. You’ll soon get the idea.

Final Words…

My modules of code were tested and pushed live in one afternoon. Nothing broke (phew!), and the client was happy. The modular separation solution solved the coding challenge.

Tags: , ,


Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Most Valued Blogger - DZone

 

February 2012
M T W T F S S
« Jan    
 12345
6789101112
13141516171819
20212223242526
272829  

Random Quote

By all means let’s be open-minded, but not so open-minded that our brains drop out. — Professor Richard Dawkins

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Categories

Follow Me