我想在控制器的特定功能之后在codeigniter中使用hook。根据文档,它将引导钩子在所有控制器之后运行。但我想让我的钩子在特定功能之后运行。
请帮助一些例子...... 谢谢
答案 0 :(得分:1)
如果你想设置几个功能使用挂钩,你可以直接添加它们,
如果大多数函数使用hook,则设置hook.And判断钩子中的特定函数
这是我的例子:
配置/ config.php中
$config['enable_hooks'] = TRUE;
配置/ hook.php
// I set the hook type to 'post_system', after rendering page hook execute.
$hook['post_system'] = array(
'class' => 'HookClass',
'function' => 'abc',
'filename' => 'hookClass.php',
'filepath' => 'hooks',
'params' => ''
);
钩/ hookClass.php
class HookClass{
private $particularFunction;
private $CI;
function __construct(){
$this->particularFunction=array('f1','f2');//set particular function name
$this->CI=& get_instance(); //important!get CI class
}
function abc(){
//if method not in particular function array,execute hook
if(!in_array($this->CI->router->method, $this->particularFunction))){
//$this->CI->router->method gets the executing method name
//execute hooks
}
}
}
example_controller.php
class Example_controller extends CI_controller{
function f1(){
//f1 function,and hooks will not execute.
}
function d1(){
//d1 function and hooks will execute
}
}