In this tutorial, we would love to share how to create, enable and use custom hooks in CodeIgniter projects.
CodeIgniter hooks allow you to execute a script with specific path within the CodeIgniter execution process without updating or modifying core files. If you need to execute a code that should be run every time a controller is created, you specify that script path in the hook.
Let’s start to create, enable and use hooks in CI:
Enabling Hooks
The hooks feature can be enabled or disabled by setting the following items in the app/config/config.php
file.
$config['enable_hooks'] = TRUE;
Defining a Hook
The hooks are defined in the app / config / hooks
folder. Each hook is specified as an array with this prototype:
$hook['pre_controller'] = array(
'class' => 'MyHook',
'function' => 'Myfunction',
'filename' => 'MyHook.php',
'filepath' => 'hooks',
'params' => array('element1', 'element2', 'element3')
);
$hook['pre_controller']
: Specifies the hook point, which is “pre_controller”.= array(
: Indicates the start of the array defining hook settings.'class' => 'MyClassName',
: Specifies the class name containing the method to be called.'function' => 'MyfunctionName',
: Specifies the name of the function/method to be called.'filename' => 'Filename.php',
: Specifies the file where the class containing the method is defined.'filepath' => 'hooks',
: Specifies the directory path where the file containing the class is located.'params' => array('beers', 'wines')
: Optional parameter that passes an array of parameters to the method/function being called.
Multiple calls to the same Hook
You can use multi-dimensional array to use the same hook point with more than one script, like this:
$hook['pre_controller'][] = array(
'class' => 'MyClass',
'function' => 'Myfunction',
'filename' => 'Myclass.php',
'filepath' => 'hooks',
'params' => array('element1', 'element2', 'element3')
);
$hook['pre_controller'][] = array(
'class' => 'MyClass2',
'function' => 'Myfunction2',
'filename' => 'Myclass2.php',
'filepath' => 'hooks',
'params' => array('element4', 'element5', 'element6')
);
Hook points
- pre_system: Executes very early during system execution, loading only the hook class and benchmark.
- pre_controller: Runs before controller invocation, after base classes, security checks, and routing.
- post_controller_constructor: Runs immediately after controller instantiation, but before any method call.
- post_controller: Executed right after the controller’s method has finished executing.
- display_override: Allows overriding the default page display behavior at the end of file execution.
- cache_override: Enables replacing the default caching method with a custom one.
- post_system: Executed at the end of system execution, after sending finalized data to the browser.
How to Use Hooks?
Using a text editor, create a controller called Test.php In it, place this code and save it to your application/controllers/
directory:
<?php
class Test extends CI_Controller {
public function index() {
echo " world !";
}
}
?>
Using a text editor, create a controller called myhook.php. In it, place this code and save it to your application/hooks/
directory:
<?php
class Myhook extends CI_Controller {
public function index() {
echo "Hello";
}
}
?>
We need to define the hooks in the application/config/hooks.php
file:
$hook['pre_controller'] = array(
'class' => 'Myhook',
'function' => 'index',
'filename' => 'Myhook.php',
'filepath' => 'hooks',
'params' => array()
);
Now open your site using a URL similar to this one:
http://localhost/codeigniter/index.php/test/index