ZF2共享模块事件管理器 [英] ZF2 Shared Modules Event Manager

查看:159
本文介绍了ZF2共享模块事件管理器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在特定事件发生时,我需要在每个模块中实现事件触发。我还需要所有其他模块,当这个事件触发时必须做一些工作,请注意。

I need to implement in each of my modules an event trigger when something specific happens. I also need that all the other modules that must to do some work when that event is trigger, be aware of it.

我正在尝试创建一些常见的端点我可以发送我的触发器,并且所有模块都需要听,但是我有一些麻烦,找出如何实现。

I'm trying to create some common endpoint where I can send my triggers, and where all the modules need to be listening, but I'm having some trouble figuring out how can I achieve that.

任何想法? / p>

Any ideas?

推荐答案

根据对Andrew的回答,只要您的控制器扩展了 AbstractActionController (它最有可能的)已经是EventManager知道了,所以你可以直接在控制器操作中触发任何您喜欢的事件,只需执行...

Based on the comments to @Andrew's answer, as long as your controller extends the AbstractActionController (which it most likely does) it's already EventManager aware, so you can just go ahead and trigger any event you like in a controller action simply by doing...

<?php
namespace Application/Controller;

// usual use statements omitted for brevity ..

class IndexController extends AbstractActionController
{
    public function indexAction()
    {
        // trigger MyEvent
        $this->getEventManager()->trigger('MyEvent', $this);
    }

}

要听一些其他模块的引导,执行以下

To listen to that event in the bootstrap of some other module, do the following

public function onBootstrap(EventInterface $e)
{
    $app = $e->getApplication();
    // get the shared events manager 
    $sem = $app->getEventManager()->getSharedManager();
    // listen to 'MyEvent' when triggered by the IndexController
    $sem->attach('Application\Controller\IndexController', 'MyEvent', function($e) {
        // do something...
    });
}

这篇关于ZF2共享模块事件管理器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆