This is only work if the action is not found, try this:
//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 backend module
$dispatcher->setDefaultNamespace("Baseapp\Backend\Controllers");
//Bind the EventsManager to the dispatcher
$dispatcher->setEventsManager($eventsManager);
return $dispatcher;
});