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

Event not fired or not listened?

Hi all, I'm getting a strange behaviour of the events manager that doesn't fire my event. That's my controller:

public function generatePdfAction($id = null)
{
    $this->view->disable();
    $eventsManager=$this->di->get('eventsmanager');

    if($id) $params['id'] = $id;
    else throw new Exception("Error Processing Request. Confirm ID needed", 1);
    $eventsManager->fire("confirm:genPdf", $params);
    //debug
     die('stop debug');

    $responses = $eventsManager->getResponses();
    echo json_encode($responses);
}

That's the plugin listener

       use Phalcon\Events\Event;
       use Phalcon\Mvc\User\Plugin;
       use Phalcon\Di;
       use Phalcon\Di\FactoryDefault;

       class ConfirmEventQueue extends Plugin
        {
            protected $di, $queue, $logger;
            public function _construct(){
            $this->di = \Phalcon\DI::getDefault();
            $this->queue = $this->di->getQueue();
            $formatter = new \Phalcon\Logger\Formatter\Line("[".date("Y-m-d H:i:s")."][" . HOSTNAME . "][%type%] %message%");
            $this->logger = new \Phalcon\Logger\Adapter\File(LOG_PATH . "confirm/" . date('Y-m-d') . ".log");
            $this->logger->setFormatter($formatter);
            $this->logger->info("### ConfirmEventQueue ###");
           //debug line
            echo 'eventmanger';
          }
          .....
          public function genPdf(Event $event, $params = null)
          {
            //debug line
            echo "putted";
            $this->queue->putInTube('genPdf',$params);
          }
         .....

        }

This is how I attach the plugin as listener inside the Module startup:

       $di->get('eventsmanager')->attach(
           "confirm",
           new \App\Frontend\Plugins\ConfirmEventQueue()
       );

The plugin is attached because it writes the log line 'eventmanger', but it seems not to fire confirm:genPdf event because "putted" is not printed. Anyone has got an idea of what I'm doing wrong?



145.0k
Accepted
answer
edited Oct '17

Are you sure that eventsmanager is singleton? (service set a shared)

Just use xdebug, it will be easier to find a cause.



12.1k

You are right, it wasn't singleton. But I don't understand why it didn't work in my case. I mean, the events manager was set and invoked only once so it should act the same way, shouldn't it? maybe there is something I'm missing about the services definition and use.. Apart my doubts about the difference, thank you very much, it solved my question

It wasn't working beacause in one eventsManager you had added listener and in another instance of events manager you had fired this event. But this other instance of events manager didn't have this listener added beacause it was new object. Just two diffrents objects which doesn't know anything about them, it's normal. This is why there is singleton.