How hooks are working in CodeIgniter?

Hooks feature provides a means to tap into and modify the inner workings of the framework without hacking the core files.
So, if you want to put a functionality which can run whenever a controller’s function initialized then you should call a function through hooks.

The following is a list of available hook points.

  • pre_system Called very early during system execution. Only the benchmark and hooks class have been loaded at this point. No routing or other processes have happened.
  • pre_controller Called immediately prior to any of your controllers being called. All base classes, routing, and security checks have been done.
  • post_controller_constructor Called immediately after your controller is instantiated, but prior to any method calls happening.
  • post_controller Called immediately after your controller is fully executed.
  • display_override Overrides the _display() method, used to send the finalized page to the web browser at the end of system execution. This permits you to use your own display methodology. Note that you will need to reference the CI superobject with $this->CI =& get_instance() and then the finalized data will be available by calling $this->CI->output->get_output().
  • cache_override Enables you to call your own method instead of the _display_cache() method in the Output Library. This permits you to use your own cache display mechanism.
  • post_system Called after the final rendered page is sent to the browser, at the end of system execution after the finalized data is sent to the browser.

Below are some examples of different type of hooks and uses.

I have defined some hooks to config/hooks.php.

<?php  
$hook['pre_controller'] = array( 'class' => 'MyHookClass',
        'function' => 'MyHookfunction',
        'filename' => 'MyHookclass.php',
        'filepath' => 'hooks',
        'params'   => array('beer', 'wine', 'snacks')
);
$hook['post_controller'] = array(
        'class'    => 'MyHookPostClass',
        'function' => 'MyHookPostfunction',
        'filename' => 'MyHookPostclass.php',
        'filepath' => 'hooks',
        'params'   => array('name'=>'Rupam', 'age'=>24)
);
$hook['post_controller_constructor'] = array(
        'class'    => 'MyHookPostControllerConstructorClass',
        'function' => 'MyHookPostControllerConstructorfunction',
        'filename' => 'MyHookPostControllerConstructorclass.php',
        'filepath' => 'hooks',
        'params'   => 'PostControllerConstructor'
);
$hook['post_system'] = array(
        'class'    => 'MyHookPostSystemClass',
        'function' => 'MyHookPostSystemfunction',
        'filename' => 'MyHookPostSystemclass.php',
        'filepath' => 'hooks',
        'params'   => 'Post System'
);
$hook['pre_system'] = array(
        'class'    => 'MyHookPreSystemClass',
        'function' => 'MyHookPreSystemfunction',
        'filename' => 'MyHookPreSystemclass.php',
        'filepath' => 'hooks',
        'params'   => 'Pre System'
);
$hook['display_override'] = array(
        'class'    => 'ReplaceToken',
        'function' => 'replacePlaceholderCode',
        'filename' => 'ReplaceToken.php',
        'filepath' => 'hooks'
);

After that, i have created some hooks files which are defined on above $hooks like ReplaceToken.php,MyHookPreSystemclass.php on application/hooks folder.
Then i have put class, function, parameters which are in hooks.
hooks/MyHookclass.php

<?php
class MyHookClass extends CI_Controller{
	public function MyHookfunction($params){
	print_r($params);
        echo "Hook Call";
	}
} ?>

hooks/MyHookPostClass.php

<?php
class MyHookPostClass extends CI_Controller{
	public function MyHookPostfunction($params){
	print_r($params);
	echo "Hook Call for post controller";
	}
} ?>

hooks/MyHookPostControllerConstructorClass.php

<?php
class MyHookPostControllerConstructorClass{
	public function MyHookPostControllerConstructorfunction($params){
	print_r($params);
	echo "Called immediately after your controller is instantiated, but prior to any method calls happening.
post_controller Called immediately after your controller is fully executed.";
	}
}
?>

hooks/MyHookPostSystemClass.php

<?php
class MyHookPostSystemClass extends CI_Controller{
	public function MyHookPostSystemfunction($params){
	print_r($params);
	echo "post_system Called after the final rendered page is sent to the browser, at the end of system execution after the finalized data is sent to the browser.";
	}
}
?>

hooks/MyHookPreSystemClass.php

<?php
class MyHookPreSystemClass{
	public function MyHookPreSystemfunction($params){
	print_r($params);
	echo "pre_system Called very early during system execution. Only the benchmark and hooks class have been loaded at this point. No routing or other processes have happened.";
	}
} 
?>

hooks/ReplaceToken.php

<?php class ReplaceToken { public function replacePlaceholderCode() { // load the instance $this->CI =& get_instance();
       
      // get the actual output
      $contents = $this->CI->output->get_output();
       
      // replace the tokens
      $this->CI->load->helper('date');
      $contents = str_replace("[DATETIME]", standard_date(), $contents);
       
      // set the output
      echo $contents;
      return;
  }
}
?>

After that, I have created a controller and view for output.
Below the Output.
If you follow the screenshot very carefully then you see the how the output shows according to calling different hooks.

Please follow and like us:

Leave a Reply

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