Using events with micro applications

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



30.9k

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.



81.2k

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);

http://docs.phalconphp.com/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?