|
Introduction
Familiarizes you with the concept of modules, teaches you how to make them and teaches you how to use them.
The Blessing of ModularityA module is a self-contained component of your application and can contain an arbitrary number of resources of any kind (controllers, libraries, views, etc.). Modularity have several purposes, the first being helping you neatly organize your application. As sites increases in size, resource folders tend to become bloated and confusing to navigate. Using modules you can break your application into smaller components that are easier to manage. Secondly, if you should ever need to reuse a component in another application you don't have to search for every resource required by the component since you have already stored them in one directory and so you only have to copy one folder. Lastly there are the ready-made downloadable modules that can be installed by simply dropping its folder in your application and firing up the module. Have It Your Way ThenBefore you get your hands dirty you have to decide where to store your modules. You will most likely want to store them in /application/modules (which is the standard setting), however this is configurable. In the configuration file there is an array, $config['directories'] with just one item, 'modules'. This array contains paths relative your application folder in which to look for modules. You can add as many paths as you like, however the more you add, the more searches will be made with each page view. Also note that the searches are performed in the order of the array. When you are done configuring the module directories, do remember to create them. Mastering the Directory StructureModules have a strict folder structure and it is important that you adhere to this when developing. Here is an example. - modules (1)
- example (2)
- controllers (3)
example.php
- libraries (3)
examplelib.php
- views (3)
example_view.php
Controllers? Controllers!The controllers themselves work like they always have. However, the way you store and access them are slightly different. At its simplest this is how it works. www.example.com/module/controller/method/parameter
As you may have noticed the only difference from the standard style is the module segment in front. There are two exceptions, though.
Them There ResourcesThe process of loading resources will do all the thinking for you. When you try to load something, the resource will be looked for in the module in which the current controller reside and, failing that, in the resource folders in your application directory and then the system directory. If the controller is in the standard folder, then the module checking will simply be skipped. However, you might want to load resources from other modules (side-wide authentication resources for instance) and if that is the case you have two options.
If you wish to autoload a resource that is located in a module, you will have to tell the autoloader where to look. This is done by adding a key to the array items that are in modules. You can still autoload resource that are not in modules. Here is an example. $autoload['libraries'] = array('database', 'session', 'module_name' => 'library_name');A Little Assistance?I know I said that loading resources required no thinking on your behalf. However, in a few cases you do have to give out a few instructions before you can load your resources without problems. Basically what happens is that when trying to figure out which module requested a particular resource, the file that called e.g. $this->load->view('filename') will be used to determine the module. However, in some cases you might need to exclude certain files in this search. Perhaps you are using a custom view library. In that case, whenever you call the custom method, the library will be considered the caller instead of your controller and so the module cannot be determined. Thus you will need to add the file to the configuration array, $config['callers']. If you have multiple files with the same name, but you only wish to exclude one of them, you can also add a bit of the filepath to distinguish it from the others. Still Here?That pretty much covers the basics of modularity. However, if you wish to start developing modules for release to the public, I encourage you to read the Standards which covers good practice when developing open source modules. |
Sign in to add a comment
COOL,COOL,COOL, this Matchbox is really COOOOL!
little more detailed docs, please guys :)
yeah, documentation would be good one.
Hi,
I create a file modules/my_module/libraries/MY_Controller.php, the MY_Controller.php content is
`class Admin_Controller? extends Controller { ...`
then I try to extend Admin_Controller? in modules/my_module/controllers/dashboard.php using
`class Dashboard extends Admin_Controller? { ...`
but somehow I got this error message
Fatal error: Class 'Admin_Controller' not found in
did I missing something?
Please help me.
Thank you.
the Admin_Controller? is not loaded by default, you have to include it.
require 'path/to/your/Admin_Controller?.php';