Question Detail

How to use Hooks in Codeigniter?

6 years ago Views 2749 Visit Post Reply

I want to call a function to check is user login or not, so i have found it can be done by hooks in codeigniter 3, Can anybody explain with the example to better understanding? It will comunity people like me who is learning Codeigniter.


Thread Reply

Nick Johnson

- 6 years ago

Below is the lifecycle How it actually works.
When you want to run a script right before your controllers get loaded, or right after, or you might want to trigger one of your own scripts in some other location.
CodeIgniter application flow

The hooks feature can be globally enabled/disabled by setting the following item in the application/config/config.php file:

$config['enable_hooks'] = TRUE;

 

There are different kind of hooks point which are distinguished by time when they are called in an execution process . The different types of hooks points  are as follows,

  • post_controller_constructor
  • pre_controller
  • pre_system
  • post_system
  • cache_override
  • display_override
  • post_controller

Initially when you download a fresh copy of CodeIgniter this file is empty except some comments specified. Basically the syntax to specify the hooks is as follows,

$hook[‘post_controller_constructor’] = array(
‘class’ => ‘App_auth’,
‘function’ => ‘index’,
‘filename’ => ‘App_auth.php’,
‘filepath’ => ‘controllers’,
‘params’ => ”
);

Let’s take an example of post_controller_constructor hook point with an example.Specifying  post_controller_constructor as a hook point means, calling the script  after the  contructor is  initiated, but before any function is called e.g. (user/userlist) etc.

As seen above we will write above code in a hook file. It means that calling index() method of controller App_auth (located in application/controller folder) when any Class contructor is initiated. We can also specify parameters if required. Our App_auth controller will look as follows.

class Auth_module
{
private $CI;

function Auth_module()
{
$this->CI = &get_instance();
}

function index()
{
if ($this->CI->session->userdata(‘user_id’) == “” )  // If no session found redirect to login page.
{
redirect(site_url(“login”));
}
}

Now, if user does any activity and if valid session is missing from system, user will be redirected to login page.

 

The array index correlates to the name of the particular hook point you want to use. In the above example the hook point is pre_controller. A list of hook points is found below. The following items should be defined in your associative hook array:

  • class The name of the class you wish to invoke. If you prefer to use a procedural function instead of a class, leave this item blank.
  • function The function (or method) name you wish to call.
  • filename The file name containing your class/function.
  • filepath The name of the directory containing your script. Note: Your script must be located in a directory INSIDE your application/ directory, so the file path is relative to that directory. For example, if your script is located in application/hooks/, you will simply use ‘hooks’ as your filepath. If your script is located inapplication/hooks/utilities/ you will use ‘hooks/utilities’ as your filepath. No trailing slash.
  • params Any parameters you wish to pass to your script. This item is optional.

Hemant Sharma

- 6 years ago

Above Description is good, I have tried some easier with coding :
 

Filepath : application/hooks

Filename : CheckLogin.php

<?php
	class CheckLogin {
	private $CI;
	function CheckLogin(){
	$CI = &get_instance();
	}
	 
	/**
	* This function used to check my Login Session.
	*/
	function isLogin(){
	 
	if($CI->session->userdata("loginSession")){
	echo "Yes I am Login";
	}else{
	echo "Ohh No. Session Expire!";
	}
	}
	}
	?>

Also, you need to config this.

config path : application/config/hooks.php

	$hook['pre_controller'] = array(
	'class' => 'CheckLogin',
	'function' => 'isLogin',
	'filename' => 'CheckLogin.php',
	'filepath' => 'hooks',
	'params' => ""
	);
 

Also you need to allow hooks in your configurations. ie. application/config/config.php

Hemant Sharma

- 6 years ago

Above Description is good, I have tried some easier with coding :
 

Filepath : application/hooks

Filename : CheckLogin.php

<?php
	class CheckLogin {
	private $CI;
	function CheckLogin(){
	$CI = &get_instance();
	}
	 
	/**
	* This function used to check my Login Session.
	*/
	function isLogin(){
	 
	if($CI->session->userdata("loginSession")){
	echo "Yes I am Login";
	}else{
	echo "Ohh No. Session Expire!";
	}
	}
	}
	?>

Also, you need to config this.

config path : application/config/hooks.php

	$hook['pre_controller'] = array(
	'class' => 'CheckLogin',
	'function' => 'isLogin',
	'filename' => 'CheckLogin.php',
	'filepath' => 'hooks',
	'params' => ""
	);
 

Also you need to allow hooks in your configurations. ie. application/config/config.php

pushpendra thakur

- 1 years ago

Hooks is used to change existing functionality of codeigniter without modified core files. For more details please follow this link

https://www.techlesson.co.in/2022/02/how-to-use-hooks-in-codeigniter.html