I am new in codeigniter. I want to load hooks for admin panel controller.
$hook['post_controller_constructor'][] = array(
'class' => 'AdminData',
'function' => 'myfunction',
'filename' => 'loginhelp.php',
'filepath' => 'hooks',
'params' => array()
);
The post_controller_constructor
hook gets called after a $class
is loaded. The class that gets loaded is based on the route parameters.
system/core/Codeigniter.php
/**
*<code>
* http://example.com/adminData/method
*</code>
*
* $CI = new adminData(); => application/controllers/adminData.php
**/
$CI = new $class();
$EXT->call_hook('post_controller_constructor');
So if you wanted to call a method on the adminData
controller, you could do something like this.
This method is not ideal, as its not very OOP like, however the way CI is built from a design point of view, you have to do a few workarounds like the example below
application/controllers/adminData.php
class AdminData extends CI_Controller
{
public function __construct(){}
// This cannot be called directly in the browser
public function _filter()
{
/**
* Put your logic in here
*<code>
* $this->model->logic()
*</code>
**/
exit('I have just be called!');
}
}
application/hooks/loginhelp.php
class AdminData
{
protected $ci;
public function __construct()
{
global $CI;
$this->ci = $CI;
}
public function myfunction()
{
// If the class is not == AdminData, just bail
if(get_class($this->ci) != 'AdminData') return;
if(!is_callable(array($this->ci, '_filter'))) return;
//calls $AdminData->_filter()
return call_user_func(array($this->ci, '_filter'));
}
}
please read the document clearly https://ellislab.com/codeigniter/user-guide/general/hooks.html
The hooks feature can be globally enabled/disabled by setting the
following item in the application/config/config.php file:
$config['enable_hooks'] = TRUE;
Hooks are defined in application/config/hooks.php file.
You cannot load it for specific controller.You need to check controller name at hooks function and write code. suppose your post_controller_constructor
hooks function name is myfunction
you can check it inside the function
$CI =& get_instance();
if($CI ->router->class=="AdminData"){//write your code}
Ok this is the simplest way to do this:
declare a public variable in your Controller
public $is_hookable = TRUE;
Then inside your hook function do this:
$ci=&get_instance();
if($ci->is_hookable){
... enter whatever you want here;
}
hope this was helpful
Application/config/hooks.php
$hook['post_controller'] = array(
'class' => 'LogoutBlockedUser',
'function' => 'logout',
'filename' => 'LogoutBlockedUser.php',
'filepath' => 'hooks',
'params' => ""
);
Enable hooks in config.php
$config['enable_hooks'] = TRUE;
Application/hooks/LogoutBlockedUser.php
class LogoutBlockedUser {
public function __construct()
{
}
public function logout()
{
$CI =& get_instance();
if(!(empty($CI->session->userdata('user_id'))))
{
$CI->load->model('Your_model', 'web');
$result = $CI->common->select_query;
if(!empty($result))
{
$CI->session->unset_userdata('user_id');
session_destroy();
redirect(base_url() . 'yourcontroller/function');
}
}
}
}