How to Implement Hooks in Codeigniter

How to Implement Hooks in Codeigniter

In this codeigniter hooks tutorial – we would love to share with you how to implement custom hooks and active this hooks. In this tutorial you will learn about hooks and it’s uses.

CodeIgniter Hooks

CodeIgniter hook allows you to execute a script with the 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 after the controller is created, then you specify that script path in the hook.

CodeIgniter’s hook feature is a means of tapping and modifying the internal workings of the framework without hacking core files.

Enabling Hooks

The hook feature can be enabled / disabled globally, which can set the following item in the app / config / config.php file.

$config['enable_hooks'] = TRUE;

Defining a Hook

The hooks are defined in the app / config / hooksf file. 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')
   );
class The name of the class you wish to invoke. If you prefer to use a procedural function instead of a class, leave this item blank. function The function (or method) name you wish to call.filename The file name containing your class/function. filepath The name of the directory containing your script. Note: Your script must be located in a directory INSIDE your application/ directory, so the file path is relative to that directory. For example, if your script is located in application/hooks/, you will simply use ‘hooks’ as your filepath. If your script is located in application/hooks/utilities/ you will use ‘hooks/utilities’as your filepath. No trailing slash. params Any parameters you wish to pass to your script. This item is optional.

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

Hook Example

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 directory:

$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 *