Php 触发钩';当一个方法在内部调用时
我使用的是CodeIgniter钩子,我定义如下:Php 触发钩';当一个方法在内部调用时,php,codeigniter,class,methods,hook,Php,Codeigniter,Class,Methods,Hook,我使用的是CodeIgniter钩子,我定义如下: $hook['post_controller'][] = array( 'class' => 'notify', 'function' => 'sendEmail', 'filename' => 'notify.php', 'filepath' => 'controllers' ); 每当用户浏览url时,就会调用它。每个方法执行后
$hook['post_controller'][] =
array(
'class' => 'notify',
'function' => 'sendEmail',
'filename' => 'notify.php',
'filepath' => 'controllers'
);
每当用户浏览url时,就会调用它。每个方法执行后,都会触发上面的钩子。这正是我想要的工作方式
假设我正在浏览类似的内容:https://localhost/dashboard/index
它将运行仪表板
控制器索引方法,然后触发我的挂钩
现在的问题是,我正在从index
方法调用dashboard类的另一个方法list
。所以当index方法调用list
method并执行它时,我也想触发钩子,但现在还没有发生
有人帮我吗 您可以使用flag来决定是否运行钩子。例如: 在仪表板控制器上的列表方法中:
$this->process_email = FALSE;
然后在你的钩子里。您需要在执行任何进程之前检查标志
function sendEmail(){
$CI =& get_instance();
if (property_exists($CI, "process_email") && $CI->process_email === FALSE)
{
return;
}
//`do your stuff here to send email..
}`
如果您从index方法调用$this->list(),它不会像调用普通的类基函数那样触发钩子,您可以做一件事,而不是调用$this->list(),您可以调用redirect(“dashboard/list”);另一个解决方案是您可以像这样手动调用-->