Codeigniter Hooks
Codeigniter Hooks - Extending the Framework Core
- CodeIgniter’s Hooks feature provides a way to tap into and modify the inner workings of the framework while not hacking the core files.
- When CodeIgniter runs it follows a selected execution method, diagramed within the Application Flow page.
- There could also be instances, however, wherever you’d wish to cause some action to require place at a selected stage within the execution method.
For example, you may need to run a script right before your controllers get loaded, or right once, otherwise you would possibly need to trigger one in each of your own scripts
in some other location.
Enabling Hooks
The hooks feature may be globally enabled/disabled by setting the subsequent item within the application/config/config.php file:
Defining a Hook
-
Hooks are defined in the application/config/hooks.php file.
-
Each hook is such as Associate in Nursing array with this prototype:
<?php
$hook['pre_controller'] = array(
'class' => 'MyClass',
'function' => 'Myfunction',
'filename' => 'Myclass.php',
'filepath' => 'hooks',
'params' => array('beer', 'wine', 'snacks')
);
Notes
- The array index correlates to the name of the actual hook purpose you would like to use.
- On top of the example, the hooking purpose is pre_controller.
A list of hook points is found below.
- The following things ought to be outlined in your associative hook array:
- class The name of category|the category} you would like to invoke.
- If you like to use a procedural operate rather than a category, leave this item blank.
- function The operates (or method) name you would like to decision.
- The filename The file name containing your class/function.
- The file path The name of the directory containing your script.
- For example, if your script is found in application/hooks/, you will simply use ‘hooks’ as your filepath.
- If your script is found in application/hooks/utilities/ you may use ‘hooks/utilities’ as your filepath.
- No trailing slash.
- params Any parameters you would like to pass to your script.
- This item is optional.
- You can additionally use lambda/anonymous functions (or closures) as hooks, with a less complicated syntax:
<?php
$hook['post_controller'] = function()
{
/* do something here */
};
Multiple Calls to the Same Hook
If need to use identical hook purpose with quite one script, merely build your array declaration multi-dimensional, like this:
<?php
$hook['pre_controller'][] = array(
'class' => 'MyClass',
'function' => 'MyMethod',
'filename' => 'Myclass.php',
'filepath' => 'hooks',
'params' => array('beer', 'wine', 'snacks')
);
$hook['pre_controller'][] = array(
'class' => 'MyOtherClass',
'function' => 'MyOtherMethod',
'filename' => 'Myotherclass.php',
'filepath' => 'hooks',
'params' => array('red', 'yellow', 'blue')
);
- This permits you to possess a similar hook purpose with multiple scripts.
- The order you outline your array is going to be the execution order.
Hook Points
-
The following may be a list of accessible hook points.
-
pre_system Called very early during system execution.
-
Only the benchmark and hooks category are loaded at now.
-
No routing or other processes have happened.
-
pre_controller referred to as straightaway before any of your controllers being referred to as.
-
All base classes, routing, and security checks are done.
-
post_controller_constructor Called immediately after your controller is instantiated, but prior to any method calls happening.
-
post_controller referred to as straightaway once your controller is totally dead.
-
display_override Overrides the _display() technique, wont to send the finalized page to the online browser at the tip of system execution.
-
This permits you to use your own show methodology.
Note - that you will need to reference the CI super object with $this->CI =& get_instance() and then the finalized data will be available by calling $this->CI->output->get_output().
- cache_override permits you to decide your own technique rather than the _display_cache() technique within the Output Library.
- This permits you to use your own cache show mechanism.
- post_system known as when the ultimately rendered page is shipped to the browser, at the tip of system execution when the finalized knowledge is shipped to the browser.