-
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 is 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, automatic, and with a minimum of configuration. 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
- Introduces $this->load->module() via a Loader library extension
- Support for both version 1.7.2 and 2.0
- Support for the default index() method and the _remap() method
[b]Solution:[/b]
By default, Modular CI expends modules to be installed in the directory 'modules', in the same directory as your index.php. If you alter the location, you have to inform the loader that there is a different location. You do that via [code]// set the location of our modules $this->load->module_path( 'modules' );[/code]
If you have modules in multiple locations, all you have to to is you alter the location by calling the module_path() method again, with the new location.
Once the path is set, you can initialize a module by using: [code]// Make the "my_module" module available $this->load->module('my_module');[/code]
[i]A module remembers to location it is loaded from. After initializing it, you can change the module path without affecting the access to the modules methods.[/i]
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. Note that you don't have to load a library or a module before using it. It will be autoloaded as soon as you reference it: [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]Routing:[/b]
- work in progress -
[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