Codeigniter Hooks Example

Codeigniter Hooks Example

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')
);
  1. $hook['pre_controller']: Specifies the hook point, which is “pre_controller”.
  2. = array(: Indicates the start of the array defining hook settings.
  3. 'class' => 'MyClassName',: Specifies the class name containing the method to be called.
  4. 'function' => 'MyfunctionName',: Specifies the name of the function/method to be called.
  5. 'filename' => 'Filename.php',: Specifies the file where the class containing the method is defined.
  6. 'filepath' => 'hooks',: Specifies the directory path where the file containing the class is located.
  7. '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

  1. pre_system: Executes very early during system execution, loading only the hook class and benchmark.
  2. pre_controller: Runs before controller invocation, after base classes, security checks, and routing.
  3. post_controller_constructor: Runs immediately after controller instantiation, but before any method call.
  4. post_controller: Executed right after the controller’s method has finished executing.
  5. display_override: Allows overriding the default page display behavior at the end of file execution.
  6. cache_override: Enables replacing the default caching method with a custom one.
  7. 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

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *