In code igniter hooks are the points before and after the execution where we can call some specific functions. These functions are called hooks and points where we call hooks are called hook point. Hooks are usefull when we have to make some changes throught the application , then we can do this without hacking all the files, just adding one hook at appropriate point. We can modify the way of usual rendering in code igniter application. So hooks facilitates modification in functionality without changing the core.
How we can use a hook in code igniter framework?
Before we use any hook in application we need to enable the hook in config.php file.Set the enable_hook config variable : TRUE,
$config[‘enable_hooks’] = TRUE;
The framework provide number of Hook Points to ease the development and modification in application:
The pre_system is called very early during system execution. Only the benchmark and hooks class have been loaded at this point.
As per the name just called before any of your controllers being called. When all the base classes are loaded, routing and security checks are done a pre_controller hook is called.
The hook is called just after the controller class instantiated but none of the method is called.
Called immediately after your controller is fully executed.
We have the _display() function in our core and we use it to render our view on browser after execution of all programms.
display_override allow use to display our own content at desired places.
$this->CI => get_instance();
We can call our own function instead of _display_cache() function using the cache_override hook. We can define our own cache display mechanism.
At the end of the system execution when final rendering is done post_system is called.
Define a hook :
A hook is defined in the hooks.php file located at application/config
Example for define a pre_controller hook:
$hook['pre_controller'] = array( 'class' => 'Classname', 'function' => 'functionname', 'filename' => 'filename.php', 'filepath' => 'hooks', 'params' => array('element1', 'element2', 'element3') );