/** * Bootstrap the micro application. * @return \Georeferencer\Application\Micro **/ private function bootstrapApplication() { $this->app = new \Phalcon\Mvc\Micro(); $this->app->setDI(new \Phalcon\DI\FactoryDefault()); $this->app->setEventsManager(new \Phalcon\Events\Manager()); return $this; }
<?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("/"); //Return (false) stop the operation return false; } } }); $app = new Micro(); //Bind the events manager to the app $app->setEventsManager($eventManager);
public function testMicroEvents() { $this->specify("Micro event handlers don't work as expected", function () { $trace = []; $eventsManager = new EventsManager(); $eventsManager->attach('micro', function ($event) use(&$trace) { $trace[$event->getType()] = true; }); $app = new Micro(); $app->setEventsManager($eventsManager); $app->map("/blog", function () { }); $app->handle("/blog"); expect($trace)->equals(["beforeHandleRoute" => true, "beforeExecuteRoute" => true, "afterExecuteRoute" => true, "afterHandleRoute" => true]); }); }