Php 如何使用事件触发器库?

Php 如何使用事件触发器库?,php,codeigniter,event-handling,Php,Codeigniter,Event Handling,试图了解如何在codeigniter中使用此事件侦听器库 我正在尝试发送邮件后提交。我的岗位主管如下 /** * Posts */ class Posts extends MX_Controller { function __construct(argument) { # code... } function post_new() { // all form validation and submission code

试图了解如何在codeigniter中使用此事件侦听器库

我正在尝试发送邮件后提交。我的岗位主管如下

/**
* Posts
*/
class Posts extends MX_Controller
{

    function __construct(argument)
    {
        # code...
    }

    function post_new()
    {
        // all form validation and submission code

        Events::trigger('add_post', 'system_events', 'string');
    }
}
我已经创建了一个控制器,我将在其中注册所有系统事件

/**
* System Events
*/
class System_Events extends MX_Controller
{

    function __construct(argument)
    {
        Events::register('add_post', array($this, 'shoot_email'));
    }


    function shoot_email()
    {
        // all email code here
    }

}
但这并不是在提交帖子时发送任何电子邮件。我不知道如何使用这个事件库


如果有任何其他方法来注册和触发事件,我也很好。但不是硬编码到系统中,而是某种API。

系统事件从不从post控制器调用,请尝试以下操作

class System_Events extends MX_Controller{

    function __construct($eventName,$methodName){
        $this->load->library('events');
        Events::register($eventName, array($this, $methodName));
    }


    function shoot_email(){
        // all email code here
        return 'Mail Send';
    }

}
现在从系统事件扩展posts控制器

class Posts extends System_Events{

    function __construct(argument){
        parent::__construct('add_post','shoot_email');//call the parent class constructor    
    }

    function post_new()
    {
        // all form validation and submission code

        Events::trigger('add_post', 'system_events', 'string');
    }
}

谢谢你详细的回答。为了确保我使用的是HMVC,
MX_控制器
来自HMVC。因此,如果我将扩展System_Events,它将
Posts
仍然作为模块工作?
System_Events扩展MX_Controller
Posts扩展System_Events
,这只是多级继承。您将能够从MX_控制器和Posts控制器的System_事件访问公共和受保护的方法