你走在正确的道路上。您唯一需要做的就是调用detach()
您的NotifyListener
而不是EvenManager
. 下面的示例来自 Zend Framework 手册EventManager
。
链接: http: //framework.zend.com/manual/2.0/en/modules/zend.event-manager.event-manager.html#examples
use Zend\Cache\Cache;
use Zend\EventManager\EventCollection;
use Zend\EventManager\ListenerAggregateInterface;
use Zend\EventManager\EventInterface;
class CacheListener implements ListenerAggregateInterface
{
protected $cache;
protected $listeners = array();
public function __construct(Cache $cache)
{
$this->cache = $cache;
}
public function attach(EventCollection $events)
{
$this->listeners[] = $events->attach('get.pre', array($this, 'load'), 100);
$this->listeners[] = $events->attach('get.post', array($this, 'save'), -100);
}
public function detach(EventManagerInterface $events)
{
foreach ($this->listeners as $index => $listener) {
if ($events->detach($listener)) {
unset($this->listeners[$index]);
}
}
}
public function load(EventInterface $e)
{
// some code to load
}
public function save(EventInterface $e)
{
// some code to save
}
}
这个例子很好地说明了如何准备一个监听器(例如AuthenticationListener
)实现ListenerAggregateInterface
。
Module
假设与您的对象相同的情况:
public function onBootstrap($e)
{
$eventManager = $e->getApplication()->getEventManager();
$cacheListener = new CacheListener($cache);
$cacheListener->detach($eventManager);
}
因此,假设您的NotifyListener
实现ListenerAggregateInterface
或只是有一个detach()
方法,您可以编写onBootstrap()
方法来分离侦听器,如下所示:
public function onBootstrap($e)
{
$eventManager = $e->getApplication()->getEventManager();
$notifyListener = new NotifyListener();
$notifyListener->detach($eventManager);
}
希望这会有所帮助,反馈将不胜感激:)
斯托扬