当内部调用方法时触发钩子的方法

时间:2015-12-10 07:38:09

标签: php codeigniter class methods hook

我正在使用CodeIgniter钩子,我已经定义如下:

$hook['post_controller'][] = 
    array(
        'class'    => 'notify',
        'function' => 'sendEmail',
        'filename' => 'notify.php',
        'filepath' => 'controllers'
    );

每当用户浏览网址时,它就会被调用。每个方法执行完毕后触发上面的钩子。这样做很好,就像我想要的那样。

假设我正在浏览类似:https://localhost/dashboard/index的内容,它将运行dashboard控制器index方法,然后触发我的钩子。

现在问题是我正在从list方法调用另一个方法index的仪表板类。因此,当索引方法调用list方法并且它已被执行时,我也想触发挂钩,这现在没有发生。

任何人都可以帮助我!

1 个答案:

答案 0 :(得分:0)

你可以使用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..


}`
相关问题