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

Using events with micro applications

I have a problem firing beforeExecuteRoute as I am using micro collections in a micro application.



58.4k

You can upload code?

Try playing with this block of code, just change it to beforeExecuteRoute: https://gist.github.com/JREAM/d0722c5996ffd0c4d26c

I am using a micro application, and the event is not fired, that's it.



98.9k

You have to use the following code:

<?php

use Phalcon\Mvc\Micro,
    Phalcon\Events\Manager as EventsManager;

//Create a events manager
$eventManager = new EventsManager();

//Listen all the application events
$eventManager->attach('micro', function($event, $app) {

    if ($event->getType() == 'beforeExecuteRoute') {
        if ($app->session->get('auth') == false) {

            $app->flashSession->error("The user isn't authenticated");
            $app->response->redirect("/")->sendHeaders();

            //Return (false) stop the operation
            return false;
        }
    }

});

$app = new Micro();

//Bind the events manager to the app
$app->setEventsManager($eventManager);

https://docs.phalcon.io/en/latest/reference/micro.html#micro-application-events

edited Jul '14

@Phalcon I am using micro collections to forward the requests to contorlloers, I need this event to be fired in certain contorllers, and others are not. Any ideas?