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.
- 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.
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,
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:
- 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
- 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
- 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
Hot Questions