Local Modules & Controllers
As nice as it is to have a centralized collection of scripts that can be run on multiple websites, there are times when you need to create code that is website specific and not worthy of being made available system-wide. Local modules and controllers are for those times.
Local Modules
A local module is a special module that that contains no controllers. Instead, CoolBrew assumes that you are providing a controller in your website document file. There is no limit to the number of local controllers you can have within a site or across sites; they can all be run through the same local module. The default local module is called (surprise!) "local".
A local module has a folder in system/modules/ just like any other module, so you can customize it by adding libraries, plugins, hooks, or any other component normal modules can have. Any customized items that you add to the local module's folder will be in effect for all your local controllers across all your websites. If you need to have multiple local controllers with different configuations, you can define any module to be a local module by adding it to the $local_modules array in the system/coolbrew.inc.php file:
$local_modules = array('local', 'local2');
Note: If you define a module as local, any controllers in the system/modules/MODULE_NAME/controllers/ folder will be ignored except to verify that they exist. You may need to create a dummy controller file in this folder so that when the router looks to make sure the class exists, it sees a file.
Local Controllers
A local controller is a controller you write in one of your website document files and then call with a tag that references your local module and the function you want to load.
Let's look at an example:
Notice a few things about this controller:
- The class name is "Local". This name is not arbitrary; it needs to match the name of your local module. The name is capitalized, like in all controllers.
- We access the controller using the get() function, just like any module. You might be tempted to instantiate the class directly, but that won't work.
- You can have as many functions within your controller as you need.
For more information about controllers and how they work, see the Controllers section in this guide as well as the Controllers section of the CodeIgniter User Guide.