3

I am trying to add event for Laminas Framework that will fire when \Laminas\Mvc\MvcEvent::EVENT_DISPATCH is triggered. But absolutelly nothing happends, like this triggers not exists. What am I doing wrong?

This is the code under the module\Application\src\Module.php:

use Laminas\ModuleManager\ModuleManager;
use Laminas\Mvc\MvcEvent;

class Module
{
    public function init(ModuleManager $moduleManager)
    {
        ini_set("display_errors", '1');
        $eventManager = $moduleManager->getEventManager();
        $eventManager->attach(MvcEvent::EVENT_DISPATCH, [$this, 'onDispatch']);
    }

    public function onDispatch(\Laminas\EventManager\Event $event)
    {
        var_dump('ok');die;
    }
}
Dmitry Leiko
  • 3,970
  • 3
  • 25
  • 42
b4rt3kk
  • 1,419
  • 3
  • 17
  • 26

2 Answers2

4

I think you need use another method in Module it's should be something like this:

use Laminas\Mvc\MvcEvent;

class Module
{
    public function onBootstrap(MvcEvent $event)
    {
        $application = $event->getApplication();
        $eventManager = $application->getEventManager();

        $eventManager->attach(MvcEvent::EVENT_DISPATCH, [$this, 'onDispatch']);
    }

    public function onDispatch(MvcEvent $event)
    {
        var_dump('ok');
        die;
    }
}

In this case it onBootstrap. Hope help you

Dmitry Leiko
  • 3,970
  • 3
  • 25
  • 42
4

On init you'll need to get the shared event manager from the module manager:

<?php

use Laminas\ModuleManager\Feature\InitProviderInterface;
use Laminas\ModuleManager\ModuleManagerInterface;
use Laminas\Mvc\Application;
use Laminas\Mvc\MvcEvent;

final class Module implements InitProviderInterface
{

    public function init(ModuleManagerInterface $manager): void
    {
        $sharedEventManager = $manager->getEventManager()->getSharedManager();
        $sharedEventManager->attach(
            Application::class,
            MvcEvent::EVENT_DISPATCH,
            function () {
                var_Dump('dispatch from init');
            }
        );
    }

}

The SharedEventManager is usually (or should be) shared between all event manager instances. This makes it possible to call or create events from other event manager instances. To differentiate between event names an identifier is used (so you can have more then one event with the same name). All MvcEvents belong to the Laminas\Mvc\Application identifier. Laminas\ModuleManager\ModuleManager has it's own EventManager instance, that is why you'll need to add the event to the SharedEventManager (init() is called by the ModuleManager and Laminas\ModuleManager\ModuleEvent is used).

onBootstrap() will be called by Laminas\Mvc\Application, that why you get the correct EventManager instance there.

As @Dimitry suggested: you should add that event in onBootstrap() as the dispatching process is part of the application and not the module manager. In init() you should only add bootstrap events.

And btw: you should use the Laminas\ModuleManager\Feature\* interfaces to make your application a bit more robust to future updates.

crash
  • 603
  • 4
  • 11