ViewEvent Listener

Hi everyone!

I have been trying to register a Listener for ViewEvent::EVENT_RENDERER_POST, and I have hit a wall.
This is what I have:

PostRendererListener:

<?php 

namespace Application\Listener;

use Laminas\EventManager\AbstractListenerAggregate;
use Laminas\EventManager\EventManagerInterface;
use Laminas\View\ViewEvent;

class PostRendererListener extends AbstractListenerAggregate
{
    public function attach(EventManagerInterface $events, $priority = 1)
    {
        $this->listeners[] = $events->attach(
            ViewEvent::EVENT_RENDERER_POST,
            [$this, 'onRendererPost']
        );
    }

    public function onRendererPost(ViewEvent $event)
    {
        echo "foo"; // i set a breakpoint here, which has no effect
        $event->getApplication();
    }
}
?>

I am wiring this up into the container in the module.config.php:

'service_manager' => [
        'factories' => [
            \Application\Listener\PostRendererListener::class => InvokableFactory::class
        ]
    ],
'listeners' => [
        \Application\Listener\PostRendererListener::class,
    ]

Now, the event is triggered in Laminas\View\View. The responsible eventmanager in that class comes from the ViewFactory of laminas-mvc, where it is retrieved from the Container, which should include all listeners registered under listeners in the appConfig according to Application.php#init().

Does anybody notice what I am missing? Thanks!

The view event is not handled in the context of MVC. You must add your listener to the view:

return [
    'view_manager' => [
        'strategies' => [
            Application\Listener\PostRendererListener::class,
        ],
    ],
    // …
];
1 Like

@makxk
Is everything working as expected? What is your use case?

Is everything working as expected?

Yes, everything’s working just fine, thanks :slight_smile:

What is your use case?

I wanted to display a bootstrap-css-modal when a user with a particular role logged in (the “Superadmin”, who gets a Linux-like Lecture).
I wanted it more prominently placed on the screen than a flashMessenger-message.

Rather gimmicky :smiley: