如何为特定控制器加载挂钩
How to load hook for particular controller
我是 codeigniter 的新手。我想为管理面板控制器加载挂钩。
$hook['post_controller_constructor'][] = array(
'class' => 'AdminData',
'function' => 'myfunction',
'filename' => 'loginhelp.php',
'filepath' => 'hooks',
'params' => array()
);
请看清楚文档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.
您无法加载特定的 controller.You 需要在挂钩函数中检查控制器名称并编写代码。假设你的 post_controller_constructor
hooks 函数名称是 myfunction
你可以在函数
中检查它
$CI =& get_instance();
if($CI ->router->class=="AdminData"){//write your code}
post_controller_constructor
挂钩在 $class
加载后被调用。加载的 class 是基于路由参数的。
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');
所以如果你想在 adminData
控制器上调用一个方法,你可以这样做。
这种方法并不理想,因为它不太像 OOP,但是 CI 是从设计角度构建的方式,您必须像下面的示例那样做一些变通方法
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'));
}
}
好的,这是最简单的方法:
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;
}
希望这对您有所帮助
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');
}
}
}
}
我是 codeigniter 的新手。我想为管理面板控制器加载挂钩。
$hook['post_controller_constructor'][] = array(
'class' => 'AdminData',
'function' => 'myfunction',
'filename' => 'loginhelp.php',
'filepath' => 'hooks',
'params' => array()
);
请看清楚文档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.
您无法加载特定的 controller.You 需要在挂钩函数中检查控制器名称并编写代码。假设你的 post_controller_constructor
hooks 函数名称是 myfunction
你可以在函数
$CI =& get_instance();
if($CI ->router->class=="AdminData"){//write your code}
post_controller_constructor
挂钩在 $class
加载后被调用。加载的 class 是基于路由参数的。
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');
所以如果你想在 adminData
控制器上调用一个方法,你可以这样做。
这种方法并不理想,因为它不太像 OOP,但是 CI 是从设计角度构建的方式,您必须像下面的示例那样做一些变通方法
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'));
}
}
好的,这是最简单的方法:
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;
}
希望这对您有所帮助
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');
}
}
}
}