Hooks – Extending the Framework Core
CodeIgniter’s Hooks feature provides a means to tap into and modify the inner workings of the framework without hacking the core files. When CodeIgniter runs it follows a specific execution process, diagramed in the Application Flow page. There may be instances, however, where you’d like to cause some action to take place at a particular stage in the execution process. For example, you might want to run a script right before your controllers get loaded, or right after, or you might want to trigger one of your own scripts in some other location.
The hooks feature can be globally enabled/disabled by setting the following item in the
$config['enable_hooks'] = TRUE;
Defining a Hook
Hooks are defined in
application/config/hooks.php file. Each hook is specified as an array with this prototype:
$hook['pre_controller'] = array( 'class' => 'MyClass', 'function' => 'Myfunction', 'filename' => 'Myclass.php', 'filepath' => 'hooks', 'params' => array('beer', 'wine', 'snacks') );
The array index correlates to the name of the particular hook point you want to use. In the above example the hook point is
pre_controller. A list of hook points is found below. The following items should be defined in your associative hook array:
classThe name of the class you wish to invoke. If you prefer to use a procedural function instead of a class, leave this item blank.
functionThe function name you wish to call.
filenameThe file name containing your class/function.
filepathThe name of the directory containing your script. Note: Your script must be located in a directory INSIDE your
applicationfolder, so the file path is relative to that folder. For example, if your script is located in
application/hooks, you will simply use
hooksas your filepath. If your script is located in
application/hooks/utilitiesyou will use
hooks/utilitiesas your filepath. No trailing slash.
paramsAny parameters you wish to pass to your script. This item is optional.
Multiple Calls to the Same Hook
If want to use the same hook point with more than one script, simply make your array declaration multi-dimensional, like this:
$hook['pre_controller'] = array( 'class' => 'MyClass', 'function' => 'Myfunction', 'filename' => 'Myclass.php', 'filepath' => 'hooks', 'params' => array('beer', 'wine', 'snacks') ); $hook['pre_controller'] = array( 'class' => 'MyOtherClass', 'function' => 'MyOtherfunction', 'filename' => 'Myotherclass.php', 'filepath' => 'hooks', 'params' => array('red', 'yellow', 'blue') );
Notice the brackets after each array index:
This permits you to have the same hook point with multiple scripts. The order you define your array will be the execution order.
The following is a list of available hook points.
Called very early during system execution. Only the benchmark and hooks class have been loaded at this point. No routing or other processes have happened.
Called immediately prior to any of your controllers being called. All base classes, routing, and security checks have been done.
Called immediately after your controller is instantiated, but prior to any method calls happening.
Called immediately after your controller is fully executed.
_display()function, used to send the finalized page to the web browser at the end of system execution. This permits you to use your own display methodology. Note that you will need to reference the CI superobject with
$this->CI =& get_instance()and then the finalized data will be available by calling
Enables you to call your own function instead of the
_display_cache()function in the output class. This permits you to use your own cache display mechanism.
Called after the final rendered page is sent to the browser, at the end of system execution after the finalized data is sent to the browser.