Learn Codeigniter

CodeIgniter Hooks

CodeIgniter's Hooks feature allow us to change the inner working of the framework without changing the core files.In Simple words, hooks allow us to execute a script with a particular path within the Codeigniter.For Example you need to check user is logged in or not before the execution of some controller.Here you can use hook and save your time to write code multiple times.

Hooks Files is located in following place in Codeigniter.

  • application/config/hooks.php
  • application /hooks

Enabling Hooks in Codeigniter

To enable Hook Codeigniter, go to application/config/config.php file and set it TRUE as given below.

$config['enable_hooks'] = TRUE;

Define Hooks in Codeigniter

A hook is defined in the application/config/hooks.php file. Each hook is defined as an array with following prototype.

$hook['pre_controller'] = array( 'class' => 'MyClassName', 'function' => 'MyfunctionName', 'filename' => 'Filename.php', 'filepath' => 'hooks', 'params' => array('beers', 'wines') );
  • class - Here, you need to mention the name of your class defined in the hooks.php file. If you are using procedural function instead of a class, you can leave it blank.
  • function - Mention function name that you are calling.
  • filename - Mention the file name containing your class/function, Which is created in application/hooks folder.
  • filepath - If your file is located inside the application/hooks folder, then your path will be "hooks". But if your file is located in application/hooks/students folder, then your path will be hooks/students.
  • params - Here, You need to specifiy parameters which you want to pass in your script and it is optional.

Enabling Hooks in Codeigniter

To enable Hook Codeigniter, go to application/config/config.php file and set it TRUE as given below.

$config['enable_hooks'] = TRUE;

Multiple Calls to the Same Hook

If want to use the same hook point with more than one script then,Use array multi-dimensional like shown below.

$hook['pre_controller'][] = array( 'class' => 'MyClassName', 'function' => 'MyfunctionName', 'filename' => 'Filename.php', 'filepath' => 'hooks', 'params' => array('beers', 'wines') ); $hook['pre_controller'][] = array( 'class' => 'MyOtherClassName', 'function' => 'MyOtherMethodName', 'filename' => 'Filename2.php', 'filepath' => 'hooks', 'params' => array('beers', 'wines') );

Hooks Points in Codeigniter

  • pre_system
  • It is called very early during system execution.Only hook class and benchmark have been loaded at this point.

  • pre_controller
  • It is called before your controller being called. At this point all base classes, security checks and routing have been done.

  • post_controller_constructo
  • It is called immediately after your controller is instantiated, but before any method call.

  • post_controller
  • It is called immediately after your controller is fully executed.

  • display_override
  • It is used to send the final page at the end of file execution.

  • cache_override
  • It enables you to call your own method instead of the _display_cache() method in the Output Library.

  • post_system
  • It is called end of system execution after the finalized data is sent to the browser.

Hooks Example in Codeigniter

Step 1

First We need to Enable Hooks as defined above.

Step 2

Create a new file inside application/controller folder.

<?php 
defined('BASEPATH') OR exit('No direct script access allowed');  
class HooksExample extends CI_Controller {  
public function index()  
    {  
        echo "CodeIgniter Tutorial By tutsway.com.";  
}  
}  
?&lgt; 
Step 3

Run "http://localhost/ci/index.php/HooksExample" Url in Browser.

CodeIgniter Tutorial By tutsway.com. Step 4

Create a hooks file MyExm.php in application/hooks folder.

<?php defined('BASEPATH') OR exit('No direct script access allowed');  
class MyExm extends CI_Controller {  
public function tut()  
    {  
        echo "Welcome to Hooks Example. This is ";  
    }  
}  
?&lgt;   
Step 4

Now we need to define hook in the application/config/hooks folder.

<?php defined('BASEPATH') OR exit('No direct script access allowed');  
  
$hook['pre_controller'] = array(  
        'class' => 'MyExm',  
        'function' => 'tut',  
        'filename' => 'MyExm.php',  
        'filepath' => 'hooks',  
        );  
        ?&lgt;  
Step 5

Now again run the old URL and you will see the following result.

Welcome to Hooks Example. This is CodeIgniter Tutorial By tutsway.com.