14th
OCT

Modularise your CodeIgniter 2 applications with Modular Extensions – HMVC

Posted by Joe under CodeIgniter, Programming

CodeIgniter (CI) is a relatively lightweight PHP framework implementing the MVC pattern. It’s become quite popular amongst PHP developers largely for its simplicity and comprehensive documentation. This post discusses integrating Modular Extensions – HMVC with the CodeIgniter 2 framework, and the advantages that this gives over using the unmodified framework.

On its own, CodeIgniter only allows for one group of components (models, views, controllers, etc.) Modular Extensions – HMVC is a set of modifications to CI which introduces modules, which are “groups of independent components… arranged in one application sub-directory that can be dropped into other CodeIgniter applications.” This kind of modular separation is desirable for a number of reasons. The best reason for using modular programming is probably the separation of concerns that it provides. First, by breaking down your features into modules, you can more easily know the location in your source tree of the code for any particular feature. Most important, however, is the separation between core and module code – you don’t need to worry that changing something in one module will affect core code or any other modules.

The concept is particularly useful to us at Clear Pixel, where most of the development work we do is on our proprietary CMS “Breeze.” Because this system is deployed to a number of sites, each of which requires a different combination of modules, we need a way to distribute to each site only the modules each require along with the core code shared between them. The ability to separate the components required for a certain feature makes it easy to package the minimum code required for a particular installation. Applications can provide install and remove features for modules to allow for users to customise. Even if your application doesn’t require this kind of per-installation customisation, modules still allow you to easily add or remove features without having to worry about complex flow-on effects. Because Modular Extensions implements a hierarchical module system, several modules which need to share code between themselves, but not with other modules, can be created as sub-modules of a parent module containing that shared code. Note also that you can also use “Modules::run(’module/controller/method’, $param, $…)” to return the output of a module controller method, which is useful for displaying the output of several modules on the one page.

The process of setting up CI with Modular Extensions is simple. At the time of writing, the latest version of CodeIgniter is 2.0.3 (download). Extract the files to a directory which can be served by your web server and verify that everything is working by navigating to its URL – you should see the default CI “welcome” page. The latest version of Modular Extensions – HMVC can be found in the project’s BitBucket repository (download). Since the instructions on the project’s site are quite straightforward, I won’t reproduce them here.

For the sake of convenience, you can download this copy of CI 2.0.3 with Modular Extensions already installed.

Download CI 2.0.3 with Module Extensions (2.2MB ZIP)

Leave a Reply

(required)
(will not be published) (required)

The Lab is the web development blog for Clear Pixel that covers many subjects from PHP, CSS, jQuery, CodeIgniter, Design and XHTML / HTML