We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

404 redirect, controller/action doesnt seems to work

so I do the registering to handle 404 error:

<?php
  public function registerServices(\Phalcon\DiInterface $di)
  {
    $di->set('dispatcher', function() use ($di) {
      $dispatcher = new \Phalcon\Mvc\Dispatcher();
      $dispatcher->setDefaultNamespace('Application\My\Controllers');
      $evManager = $di->getShared('eventsManager');
      $evManager->attach('dispatch:beforeException', function($event, $dispatcher, $exception) {
        switch ($exception->getCode())
        {
          case \Phalcon\Mvc\Dispatcher::EXCEPTION_HANDLER_NOT_FOUND:
          case \Phalcon\Mvc\Dispatcher::EXCEPTION_ACTION_NOT_FOUND:
            $dispatcher->forward(array(
              'namespace'  => 'Application\My\Controllers',
              'controller' => 'errors',
              'action'     => 'show404'
            ));
            return false;
        }
      });
      $dispatcher->setEventsManager($evManager);
      return $dispatcher;
    });
  }

and now the views/errors/show404.phtml runs - however, controllers/ErrorsController::show404Action win NOT. Interestingly, if I remove/rename show404Action method, it chrashes.



222
edited Mar '16

Is not a typo?

'controller' => 'error',

And make sure, that you are using the correct namespace n ErrorController, and extends just "\Phalcon\Mvc\Controller".



28.2k

yea, just a typo, fixed in my post. It still doesnt work :)



222

Are you using some "ControllerBase" extending the "ErrorController"? Maybe, there is some before() method executing actions before your show404.

Another thing. You cannot access /errors/show404, because it's not been a route.