【问题标题】:Trigger hook's method when a method called internally内部调用方法时触发钩子的方法
【发布时间】:2015-12-10 07:38:08
【问题描述】:

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

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

每当用户浏览 url 时都会调用它。每个方法执行后都会触发上面的钩子。这按我想要的方式工作得很好。

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

现在问题是我从index 方法调用仪表板类的另一个方法list。因此,当 index 方法调用 list 方法并执行时,我也想触发钩子,但现在不会发生。

谁能帮帮我!

【问题讨论】:

  • 如果你从索引方法调用 $this->list() 它不会触发钩子作为它的普通类基函数调用,你可以做一件事而不是调用 $this->list( ) , 你可以调用 redirect("dashboard/list");另一种解决方案是您可以像这样手动调用它 --> link

标签: php codeigniter class methods hook


【解决方案1】:

你可以使用标志来决定你的钩子是否运行。例如: 在仪表板控制器上的列表方法中:

$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..


}`

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-23
    • 2022-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多