$eventsManager = $di->getShared('eventsManager'); //Attach a listener for type "dispatch" $eventsManager->attach("dispatch", function () { new Security(); }); $eventsManager->attach('dispatch:beforeException', function ($event, $dispatcher, $exception) { switch ($exception->getCode()) { case PhDispatcher::EXCEPTION_HANDLER_NOT_FOUND: case PhDispatcher::EXCEPTION_ACTION_NOT_FOUND: $dispatcher->forward(['controller' => 'error', 'action' => 'route404']); return false; } }); $dispatcher = new Phalcon\Mvc\Dispatcher(); $dispatcher->setDefaultController('Index'); $dispatcher->setDefaultAction('index'); $dispatcher->setEventsManager($eventsManager); return $dispatcher; }, true); /** * Translate */ $di->set('t', function () use($di) { //Ask browser what is the best language $language = $di->getShared('request')->getBestLanguage(); //Check if we have a translation file for that lang if (file_exists(__DIR__ . "/../messages/" . $language . ".php")) { $messages = (require __DIR__ . "/../messages/" . $language . ".php"); } else { // fallback to some default $messages = (require __DIR__ . "/../messages/zh-cn.php");