Modules in CodeIgniter

April 30, 2009 | In: CodeIgniter, modules

Last time i discussed about Controllers, now it’s module time.

Why module? or let’s say why you want to make a module? Well, have you experienced when you are working on a specific functionality, you are looking all over the place, in views, libraries, models, etc, and you wish they were in one location? If you say “yes”, then this is the answer.

But CodeIgniter doesn’t support module on its default install, instead I use a “third-party” library called “Matchbox“.

In creating modules, I established my own simple “convention” that i follow in building all my modules. First, i create a “per-module” specific Controller that all of my controllers(children) will become their parent. Say i create a “clicktrack” module, I will make an “Clicktrack_Module_Controller.php” file with the following codes:

class Clicktrack_Module_Controller extends MY_Controller {
      function __construct() {
          parent::__construct();
      }
}

Why do I have to do that? Isn’t it another layer to make a “slow” application? Well, yes and no, it depends on how you define “slow”. As part of my “module convention”, I load my module-specific configuration file in this controller too, so all of the children controllers will inherit it so you don’t have to repeat the config loading on each and every succeeding controllers.

The modified code above will now look like this :

class Clicktrack_Module_Controller extends MY_Controller {
        function __construct() {
            parent::__construct();
            $this->load->module_config('clicktrack');
        }
}

where “clicktrack” is the module name, given the fact that you named your configuration file “$approot/modules/clicktrack/config/config.php”.

The live and working module will be the next topic(with complete source code for download).

Stay tuned!



4 Responses to Modules in CodeIgniter

Avatar

handoyo

September 25th, 2009 at 3:28 am

Hi,thanks for the articles.. What is the advantage of using modules instead of libraries..Thanks.. ^_^

Avatar

admin

September 28th, 2009 at 3:05 am

Hi Handoyo,

Actually you cannot compare them, they are totally two different things.

When you say module(CI perspective), it is a ‘sub-system’, you have a controller, library, model and a view. So it’s actually a working system packed together.

While a library is a ‘component’ to create/build a new system(sub-system).

You can actually use a library inside your module.

Hope that helps and thanks for the comment.

Avatar

Click-Track-Go Module for CodeIgniter» FROST : FRee and Open Source Technologies

May 10th, 2010 at 3:18 am

[…] post i wrote is about modules , today, this module will track the clicks(referer and destination), save it, and […]

Avatar

FROST » Blog Archive » Click-Track-Go Module for CodeIgniter

June 28th, 2010 at 6:15 am

[…] post i wrote is about modules , today, this module will track the clicks(referer and destination), save it, and redirect. You can […]

Comment Form