Soo, I am trying to handle 500 pages using dispatch:beforeException, see code:
$di->setShared('dispatcher', function () use ($di) {
/** @var Manager $eventsManager */
$eventsManager = $di->getShared('eventsManager');
$eventsManager->attach('dispatch:beforeException', function ($event, Dispatcher $dispatcher, $exception) use ($di) {
if ($exception instanceof \Phalcon\Mvc\Dispatcher\Exception) {
switch ($exception->getCode()) {
case Phalcon\Mvc\Dispatcher::EXCEPTION_HANDLER_NOT_FOUND:
case Phalcon\Mvc\Dispatcher::EXCEPTION_ACTION_NOT_FOUND:
$action = ErrorController::ACTION_SHOW_404;
break;
default:
$action = ErrorController::ACTION_SHOW_500;
}
} else {
$action = ErrorController::ACTION_SHOW_500;
}
$dispatcher->forward([
'module' => APP_MODULE,
'controller' => 'error',
'action' => $action,
]);
return false;
});
$dispatcher = new Dispatcher();
$dispatcher->setEventsManager($eventsManager);
return $dispatcher;
});
but the issue is that, when calling throw new \Exception('test') inside of controller action - everything is fine, but when calling it insade of initialize(), I get only blank page, and no error messages whatsoever.
Thinks I tested and excluded are:
-
initialize()conflict onErrorController- there it's emptypublic function initialize() { }, so it doesn't get called from ControllerBase which is having some action in there -
There are no code on 500 page, except settign status code(500) and using
echo 500, so it doesn't throw exception itself - 500 page and it's controller are super simple, they are not calling any kind of services, which could throw an exception
By checking dispatcher.zep, it should work as intended, but it doesn't... Really have no idea, what I am doing wrong.