要将新事件附加到侦听器,我使用如下代码并且工作正常:
class Listener implements ListenerAggregateInterface
{
/**
* @var \Zend\Stdlib\CallbackHandler[]
*/
protected $listeners = array();
/**
* {@inheritDoc}
*/
public function attach(EventManagerInterface $events)
{
$sharedEvents = $events->getSharedManager();
$this->listeners[] = $sharedEvents->attach('*', '*', array($this, 'log'));
}
public function detach(EventManagerInterface $events)
{
foreach ($this->listeners as $index => $listener) {
if ($events->detach($listener)) {
unset($this->listeners[$index]);
}
}
}
public function log($event)
{
// Log feature here
}
}
我只想关联一些动作,而不是“*”,例如关联以“.post”结尾的动作,如下所示:
(...)
$this->listeners[] = $sharedEvents->attach('*', '*.post', array($this, 'log'));
(...)
但是不起作用,我该怎么做呢?(避免听所有动作)