LazyListenerAggregate

Ok, the upper example with LazyListenerAggregate works only when the ‘event’ attribute is one of the MVC events (The MvcEvent).

When I have “custom” event then listener needs to be attached to Shared Event Manager.

Example of registering listeners:

<?php
namespace Order;

use Laminas\Config;
use Laminas\Mvc\MvcEvent;
use Laminas\EventManager\LazyListenerAggregate;
use Laminas\EventManager\LazyListener;
use Laminas\Mvc\Controller\AbstractController;

class Module
{

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

        // register Listener Aggregates
        $listener = $serviceManager->get(Event\HideDiscountListener::class);
        $listener->attach($eventManager);
        $listener = $serviceManager->get(Event\StopTerminListener::class);
        $listener->attach($eventManager);

        // register Lazy Listeners
        $definition = array(
            'listener' => Event\DemoLazyListener::class,
            'method' => 'onEchoVal'
        );
        $sharedEventManager = $eventManager->getSharedManager();
        $sharedEventManager->attach(AbstractController::class, 'echoVal',
            new LazyListener($definition, $serviceManager), - 100);
    }
    ...
}

shared

Thank you froschdesign.

1 Like