CodeIgniter Hooks
×

CodeIgniter Hooks

1942

  • In Codeigniter, hooks provide the facility to change the inner working of the framework without changing the core files.
  • Hooks are used to perform any action at a particular stage in the execution process.
  • Hook files are stored in the application/hooks directory of your project.
  • All hooks-related configurations are done into the applicaton/config/hooks.php file.

Related Topic

Codeigniter Interview Questions

Enabling Hooks:

Hooks can be enabled or disabled by changing the application/config/hooks.php file. To enable the hooks put the given line.

$config['enable_hooks'] = TRUE;

We can disable hooks by setting the above value to FALSE.


Related Topics:

Views in CodeIgniter
Models in CodeIgniter

Defining a Hook:

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

$hook['pre_controller'] = array(
'class' => 'MyClass',
'function' => 'Myfunction',
'filename' => 'Myclass.php',
'filepath' => 'hooks',
'params' => array('beer', 'wine', 'snacks')
);

Array keys description:

  • class: It represents the name of the class which you want to use. If you want to use your own function instead of an inbuilt class, you can leave it blank.
  • function: It represents the name of the function which you want to call.
  • filename: It represents the name of the file which contains your class or function. It should be stored in the application/hooks directory.
  • filepath: It represents the name of the directory in which your files are stored. For example: if your files are stored in the application/hooks folder, then it contains the value 'hooks'.
  • params: It represents an optional parameter that you want to pass into your script.

Multiple Calls to the Same Hook:

To use the same hook for more than one script, we simply declare a multidimensional array instead of a simple array.

$hook['pre_controller'][] = array(
'class' => 'MyClass',
'function' => 'MyMethod',
'filename' => 'Myclass.php',
'filepath' => 'hooks',
'params' => array('apple', 'orange', 'grapes')
);

$hook['pre_controller'][] = array(
'class' => 'MyOtherClass',
'function' => 'MyOtherMethod',
'filename' => 'Myotherclass.php',
'filepath' => 'hooks',
'params'  => array('rose', 'lilly', 'jesmine')
);

Hook Points:

Some interesting points about hooks are listed below:

  • pre_system: This function is called at the starting phase of system execution. Only the benchmark and hooks class have been loaded at this point.
  • pre_controller: This function is called immediately prior to any of your controllers being called.
  • post_controller_constructor: This function is called immediately after your controller is executed.
  • post_controller: This function is called immediately after your controller is fully executed.
  • display_override: This function is used to send the finalized page to the web browser at the end of the system. It overrides the display() method.
  • cache_override: This permits you to use your own cache display mechanism instead of the _display_cache() method.
  • post_system: This is called at the end of system execution after the finalized data is sent to the browser.

Example:

Step 1 Open the application/controllers directory and create a new controller Hook_controller.php.

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Hook_controller extends CI_Controller {
public function index()
{
echo "<strong>CodingTag</strong>";
}
}
?>

Step 2 Enter the given URL into the browser to check the output.

http://localhost/ci/index.php/Hook_controller

Step 3 Now, open the application/hooks directory and create a hook file First_hook.php.

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class First_hook extends CI_Controller {
public function first()
{
echo "This is Hooks tutorial on ";
}
}
?>

Step 4 Open application/config/hooks.php file and create a hook array.

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

$hook['pre_controller'] = array(
'class' => 'First_hook',
'function' => 'first',
'filename' => 'First_hook.php',
'filepath' => 'hooks',
);
?>

Step 5 Open the application/config/config.php file and enable the hooks.

Step 6 Now, enter the given URL into the browser to check the result.

http://localhost/ci/index.php/Hook_controller



Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments