Hooks are events that are performed before or after the execution of programs in Codeigniter, we can execute scripts with the help of specific paths without making changes to our application core files.
For example, if we want to run a specific block of code every time before we run our application we can make a script and add a path in the hooks file.
Hooks configurations can be set in two files, first application/config/hooks.php, and second in the application/hooks folder.
For better understanding let's take an example of the login process of a user, the hooker will check if the user is already logged into the system or not before the login script even executes, this will make the process much faster than usual.
To enable Hook, all we need to is to set hooks to true in the application/config/config.php file.
$config['enable_hooks'] = TRUE;
$hook['pre_controller'] = array( 'class' => 'Classname', 'function' => 'My_functionname', 'filename' => 'My_filename.php', 'filepath' => 'hooks', 'params' => array('Myelement1', My'element2', 'Myelement3') );
class - class element is used to store our class name from the hooks.php file while using the procedural function we leave this class name blank.
function - function attribute is the attribute that sets the function name we call.
filename - filename holds the name of our file in the application/hooks folder the file contains our class and functions.
File path - file path contains the name of a directory in which all scripts are written inside the application folder.
We use a multidimensional array to call the same hooks several times.
$hook['pre_controller'] = array( 'class' => 'MyClassname1', 'function' => 'Myfunctionname1', 'filename' => 'Myfilename1.php', 'filepath' => 'Myhooks', 'params' => array('Myelement1', 'Myelement2', 'Myelement3') ); $hook['pre_controller'] = array( 'class' => 'MyClassname2', 'function' => 'Myfunctionname2', 'filename' => 'Myfilename2.php', 'filepath' => 'Myhooks', 'params' => array('Myelement4', 'Myelement5', 'Myelement6') );
Hook class and benchmark are located here.
Pre_controller is a priority call to the controller.
Post controller constructor is called before method calls and immediately after a controller call.
Once our controller is executed completely the post_controller is called.
Once all the files are executed display override sends a final page.
Cache override helps us to call our functions.
At the end of the system execution post_sytem is called to send the final page.