# LazyListenerAggregate

**URL:** <https://discourse.laminas.dev/t/lazylisteneraggregate/1811>\
**Category:** Components & MVC\
**Tags:** laminas-eventmanager, laminas-mvc\
**Created:** [September 3, 2020, 3:03pm UTC](https://discourse.laminas.dev/t/lazylisteneraggregate/1811 "2020-09-03T15:03:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomstryja](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/tomstryja/32/1075_2.png) [@tomstryja](https://discourse.laminas.dev/u/tomstryja)\
**Post date:** [September 3, 2020, 3:03pm UTC](https://discourse.laminas.dev/t/lazylisteneraggregate/1811/1 "2020-09-03T15:03:40Z")

</div>

I would like to implement listener, which is not required on every request (that is the purpose of LazyListener). But I can’t figure out working example. Here is the code:

```
<?php
namespace Order;

use Laminas\Config;
use Laminas\Mvc\MvcEvent;
use Laminas\EventManager\LazyListenerAggregate;

class Module
{

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

        // demo lazy
        $definition = array(
            'listener' => Event\DemoLazyListener::class,
            'method' => 'onEchoVal',
            'event' => 'echoVal',
            'priority' => - 100
        );
        $aggregate = new LazyListenerAggregate([$definition], $application->getServiceManager());
        $aggregate->attach($eventManager);
       // dump($aggregate); exit;
    }
   .....
}

```

Listener is class with factory to access services later.

```
<?php
namespace Order\Event;

use Laminas\ServiceManager\ServiceLocatorInterface;
use Laminas\EventManager\EventInterface;

class DemoLazyListener
{

    public $serviceManager;

    public function __construct(ServiceLocatorInterface $serviceManager)
    {
        $this->serviceManager = $serviceManager;
    }

    public function onEchoVal(EventInterface $event)
    {
        $param = $event->getParam('param');

        $writer = new \Laminas\Log\Writer\Stream( __DIR__. '/../../../../data/log/event_demo.log');
        $logger = new \Laminas\Log\Logger();
        $logger->addWriter($writer);
        $logger->info('Param ' . $param . '.');
    }
}

```

Event is triggered in controller action.

```
$this->getEventManager()->trigger('echoVal', null, [
    'param' => 'demo'
]);

```

When I dump the LazyListenerAggregate the _listener_ key is null. I’m not sure if this is not the case.

![listener](https://canada1.discourse-cdn.com/flex032/uploads/zendframework/original/1X/614e1c78d018da8e91d12a246f1927dbaacba59b.png)

Thank you in advance for help.

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [September 4, 2020, 9:38am UTC](https://discourse.laminas.dev/t/lazylisteneraggregate/1811/2 "2020-09-04T09:38:52Z")

</div>

Hello and welcome to the forums! 😃

> [@tomstryja](#):
>
> But I can’t figure out working example.

Please test the following example. (_I hope I remember correctly._)

```php
use Laminas\EventManager\LazyListener;
use Laminas\Mvc\MvcEvent;
use Laminas\Mvc\Controller\AbstractController;

class Module
{
    public function onBootstrap(MvcEvent $event) : void
    {
        $application = $event->getApplication();
        $container = $application->getServiceManager();
        $eventManager = $application->getEventManager()->getSharedManager();

        $eventManager->attach(
            AbstractController::class,
            'echoVal',
            new LazyListener(
                [
                    'listener' => Event\DemoLazyListener::class,
                    'method' => 'onEchoVal',
                ],
                $container
            )
        );
    }

    // …
}

```

---

<div class="post-metadata">

**Author:** ![tomstryja](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/tomstryja/32/1075_2.png) [@tomstryja](https://discourse.laminas.dev/u/tomstryja)\
**Post date:** [September 7, 2020, 10:30am UTC](https://discourse.laminas.dev/t/lazylisteneraggregate/1811/3 "2020-09-07T10:30:17Z")

</div>

Ok, the upper example with LazyListenerAggregate works only when the _‘event’_ attribute is one of the MVC events ([The MvcEvent](https://docs.laminas.dev/laminas-mvc/mvc-event/)).

**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](https://canada1.discourse-cdn.com/flex032/uploads/zendframework/original/1X/a7e20a5b9276fbf71666cb657639ab1dda445f15.png)

Thank you froschdesign.
