How to Use Hooks in CodeIgniter

From WikiHTP

Let's see quickly how to use hooks with CodeIgniter, imagine that we have a great portal and we do not want to let any user access some pages, we could put on each page a check of whether the session exists or not, but that in CodeIgniter it is 'wrong'.

Thanks to the hooks we can do that in a single function and telling you when we want to do the checks, for example, we will do it in the post_controller_constructor that is when the constructor is instantiated but when still no method has been called, let's see the options that we give CodeIgniter.

CodeIgniter options[edit]

  • pre_system:

Called very early during the execution of the system. At this point, only the hook and benchmark classes were loaded. No routing or other process occurred.

  • pre_controller:

Called immediately before any of its controllers have been called. They were all base classes, routing, and security checks.

  • post_controller_constructor

Called immediately after your controller was instantiated, but before any call to a method has occurred.

  • post_controller

Called immediately after your driver was completely executed.

  • display_override

Overrides the _display() function, used to send the completed page to the web browser at the end of system execution. This allows you to use your own printing methodology. Note that you will need to reference the CI super-object with $this->CI = &get_instance() and then the finished data will be available by calling $this->CI->output->get_output().

  • cache_override

It allows you to call its own function instead of the _display_cache() function in the Output class. This allows 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 the system execution and after the finalized data is sent to the browser.

With that we can adapt each case to our needs, we will make a basic example to avoid that users who do not log in cannot access the portal and they are taken to a supposed login page, let's see.

Enable the Hooks[edit]

The first thing we must do is open the config file, once done we must enable the hooks with the line.

$config['enable_hooks'] = TRUE;

Then we must place an encryption key, otherwise, we cannot use the sessions.

Once done, we will open the application/config/hooks file and place the following code.

$hook['post_controller_constructor'] = array(
                                'class'    => 'Home',
                                'function' => 'check_login',
                                'filename' => 'Home.php',
                                'filepath' => 'hooks'
                                );

With that code we are telling CodeIgniter that in the file Home.php there is a class called Home and inside there is a function called check_login, all this in the hooks directory, because in the event post_controller_constructor check what that function does, let's see what he does

We create a new file in the application/hooks folder and inside we place the following code.

<?php
if (!defined( 'BASEPATH')) exit('No direct script access allowed'); 
class Home
{
	private $ci;
	public function __construct()
	{
		$this->ci =& get_instance();
		!$this->ci->load->library('session') ? $this->ci->load->library('session') : false;
		!$this->ci->load->helper('url') ? $this->ci->load->helper('url') : false;
	}	

	public function check_login()
	{
		if($this->ci->session->userdata('id') == FALSE)
		{
			redirect(base_url('login);
		}
	}
}
/*
/end hooks/home.php
*/

As we see it is a really simple function that all it does is check if the session exists, if it does not exist it will always redirect the user to the login, in the constructor what we do is instantiate the super object of CodeIgniter with $this->ci = &get_instance();

We check if the session library and the helper URL are loaded and that's it, so we can use the hooks in CodeIgniter.

As the documentation says, if we want to call more than one method in the same hook point we should simply go back to the application/config/hooks hooks file and make our multidimensional array, let's see.

$hook['post_controller_constructor'][] = array(
                                'class'    => 'Home',
                                'function' => 'check_login',
                                'filename' => 'Home.php',
                                'filepath' => 'hooks'
                                );

$hook['post_controller_constructor'][] = array(
                                'class'    => 'Home',
                                'function' => 'saluda',
                                'filename' => 'Home.php',
                                'filepath' => 'hooks'
                                );

As we can see we have two hooks in the post_controller_constructor event, the only thing we have done is to add the brackets[] to make the multidimensional array, now we can have two calls in the same hook.

About This Tutorial

This page was last edited on 14 February 2019, at 22:57.