We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

The 'view' events not fired

Hello, I am using PhalconPHP v.1.3.2 I am trying to attach a listener to some of the 'view' events. The code I use is correct as it works fine when testing with any of the 'dispatcher' events. I found that there are only 'dispatcher' events fired. Does someone knows a way to display all the fired events or maybe a way to enable the view events?

Thanks a lot!



98.9k
Accepted
answer

Just attach an events manager as you probably did with 'dispatcher' events:

<?php

$di->set('view', function() {

    //Create an events manager
    $eventsManager = new Phalcon\Events\Manager();

    //Attach a listener for type "view"
    $eventsManager->attach("view", function($event, $view) {
        file_put_contents('debug.txt', $event->getType() . ' - ' . $view->getActiveRenderPath() . PHP_EOL);
    });

    $view = new \Phalcon\Mvc\View();
    $view->setViewsDir("../app/views/");

    //Bind the eventsManager to the view component
    $view->setEventsManager($eventsManager);

    return $view;

}, true);

Thanks a lot!

I forgot to add "$view->setEventsManager($eventsManager);"