/** * Register specific services for the module * * @package base-app * @version 2.0 * * @param object $di dependency Injector * * @return void */ public function registerServices(\Phalcon\DiInterface $di) { //Registering a dispatcher $di->set('dispatcher', function () { //Create/Get an EventManager $eventsManager = new \Phalcon\Events\Manager(); //Attach a listener $eventsManager->attach("dispatch", function ($event, $dispatcher, $exception) { //controller or action doesn't exist if ($event->getType() == 'beforeException') { switch ($exception->getCode()) { case \Phalcon\Dispatcher::EXCEPTION_HANDLER_NOT_FOUND: case \Phalcon\Dispatcher::EXCEPTION_ACTION_NOT_FOUND: $dispatcher->forward(array('controller' => 'index', 'action' => 'notFound')); return false; } } }); $dispatcher = new \Phalcon\Mvc\Dispatcher(); //Set default namespace to documentation module $dispatcher->setDefaultNamespace("Baseapp\\Documentation\\Controllers"); //Bind the EventsManager to the dispatcher $dispatcher->setEventsManager($eventsManager); return $dispatcher; }); //Registering the view component $di->set('view', function () use($di) { $view = new \Phalcon\Mvc\View(); $view->setViewsDir(__DIR__ . '/views/'); $view->registerEngines(\Baseapp\Library\Tool::registerEngines($view, $di)); return $view; }); }
/** * Catch the exception and log it, display pretty view * * @param \Exception $e */ public static function exception(\Exception $e) { $config = \Phalcon\DI::getDefault()->getShared('config'); $errors = array('error' => get_class($e) . '[' . $e->getCode() . ']: ' . $e->getMessage(), 'info' => $e->getFile() . '[' . $e->getLine() . ']', 'debug' => "Trace: \n" . $e->getTraceAsString() . "\n"); if ($config->app->env == "development") { // Display debug output $debug = new \Phalcon\Debug(); $debug->onUncaughtException($e); } else { // Display pretty view of the error $di = new \Phalcon\DI\FactoryDefault(); $view = new \Phalcon\Mvc\View\Simple(); $view->setDI($di); $view->setViewsDir(APP_PATH . '/app/frontend/views/'); $view->registerEngines(\Baseapp\Library\Tool::registerEngines($view, $di)); echo $view->render('error', array('i18n' => I18n::instance(), 'config' => $config)); // Log errors to file and send email with errors to admin \Baseapp\Bootstrap::log($errors); } }