-
Notifications
You must be signed in to change notification settings - Fork 0
Modular CI
[b]Introduction:[/b]
Modular extensions for CodeIgniter have been around for quite some time: Modular Extensions, HMVC, Matchbox and Modular Separation.
While designing an application framework based on CodeIgniter, I needed a much more flexible system than what is on offer by the extensions mentioned above.
A module in my definition was a complete CodeIgniter mini-application. It needed to support all elements you have in a regular application folder: controllers, models, views, libraries and helpers. All these elements must be loadable using the CodeIgniter standard, via $this->load. And this included controllers loading other controllers, and models loading other models, to support true HMVC. At the same time, the solution should support modulair routing. Not with a fixed format (for example the first URI segment has to be the module name), but using regular CI routing. Also, I wanted as little impact on a standard CodeIgniter installation, as every extension of a core library method might break some CodeIgniter functionality in the future.
[b]Features:[/b]
- Location of your modules is configurable
- Supports routing to a module controller
- Supports cross module calls, also to controller methods
- Uses standard CodeIgniter routing, no Router library modifications
- Support for both version 1.7.2 and 2.0
- Support for the default index() method and the _remap() method
[b]Solution:[/b]
The new Module library can be loaded from inside your controller, like any other library: [code]$this->load->library('module', array('path' => 'modules') );[/code]
You can pass a config array to the library to define the location of your modules. This path is relative to your application directory. You can also use a configuration file for this purpose.
You can also load the Module library via the autoload config file. If you do, you have to use a configuration file to inform the library of the location of your modules: [code]<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
| /* |
|---|
| Modules path |
| ------------------------------------------------------------------------- |
| | This variable must contain the name of your "modules" folder. | Include the path if the folder is not in the same directory | as you applications index.php file. | */
$config['path'] = 'modules';
/* End of file module.php / / Location: ./application/config/module.php */[/code]
Once the Module library is loaded, you can initialize a module by using: [code]// Make the "my_module" module available to us $this->module->load('my_module');[/code]
This is all you have to do. From this moment on, all files from this module can be accessed in a way similar to the way you would normally load them: [code]// call a method in a library of our module $this->my_module->library->testlib->func('varA', 'varB');
// call a method in a model of our module $this->my_module->model->testmodel->func('var1', 'var2');
// call a method in a controller of our module $this->my_module->controller->testcntrlr->func('varX', 'varY', 'varZ');
// load a view from our module $this->my_module->view('test');
// load a helper from our module $this->my_module->helper('test');
// and see if it works testhelper();[/code]
[b]Download:[/b] The latest version of the zip file can be found in the forum thread.
[b]Discuss:[/b] See this [url=http://codeigniter.com/forums/viewthread/164350/]forum thread[/url].
Developed by WanWizard