Hooks
What is CodeIgniter Hooks?

Suppose i want to run some code before open the website or after close the website, then we use hook.

Step to Create Hook

1- Enable hook in application->config->config.php file.
$config['enable_hooks'] = TRUE;

2- Specify code to run on application->config->hooks.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

$hook['pre_controller'] = array(
'class' => 'Myhook',
'function' => 'testfunction',
'filename' => 'myhook.php',
'filepath' => 'hooks',
);

3- Create php file in application->hook folder
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Myhook extends CI_Controller {
public function testfunction()
{
echo "This is run before executing the website ";
}
}
?>


Previous
Next