Z
Z
zenaku2015-06-15 04:55:51
Zend Framework
zenaku, 2015-06-15 04:55:51

How to register listeners in onBootstrap(zf2)?

I'm learning zf2 and ran into a problem.
I need to register EventManager listeners in onBootstrap for logging (and more). But I ran into a problem: when I registered a listener in onBootstrap-e, it fires on a trigger inside onBootstrap, but completely ignores the triggers in the controller. More precisely, it’s not registered at all (var_dump($this->getEventManager())). It just isn't passed to the EventManager in the controller, or those listeners are removed

//$eventManager = $e->getApplication()->getEventManager(); <-- первым вариантом
  $sm = $e->getApplication()->getServiceManager();
  $eventManager = $sm->get('eventmanager');
  /*имя события и функция - не суть, потому что оно прекрасно срабатывает внутри onBootstrap*/
        $eventManager->attach('%name%', %function%);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
Cat Anton, 2015-06-15
@27cm

ZF2 Shared Modules Event Manager
Try this:

$sem = $e->getApplication()->getEventManager()->getSharedManager();
$sem->attach('Application\Controller\IndexController', '%name%', %function%);

More about SharedEventManager is well written here:
videoforme.ru/wp-content/uploads/2011/09/RM.pdf

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question