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

Custom 404 Page

So i'm trying to add a router to a custom 404 page but there's some few problems :

1 - index will not display anything if not inputed : 127.0.0.1/index

2 - if i go to a page that does not exists it justs says that the controller does not exist yet .

Routes :

$di['router'] = function () {

$router = new Router();

$router->setDefaultModule('frontend');
$router->setDefaultNamespace('Frontend\Controllers');
$router->removeExtraSlashes(true);

$router->notFound([
    'module'        => 'frontend',
    'namespace'     => 'Frontend\Controllers',
    'controller'    => 'error',
    'action'        => 'show404'
]);

$router->add("/admin", [
    'module'     => 'backend',
    'namespace'  => 'Backend\Controllers',
    'controller' => 'index',
    'action'     => 'auth',
]);
return $router;
};

Controller

namespace Frontend\Controllers;

class ErrorController extends ControllerBase
{
public function indexAction()
{

}

public function show404Action()
{
    echo "132";
}

}


473

Hey man

This is tip, if is true you can share here https://phalcontip.com

edited Mar '15

Hello,

I had a bit of headache with this problem too and I ended up doing it beforeException in bootstrap

    $response = new \Phalcon\Http\Response();
    $di->set('response', $response);

    $eventsManager = new \Phalcon\Events\Manager();

    $dispatcher = new \Phalcon\Mvc\Dispatcher();

    $eventsManager->attach(
        "dispatch:beforeException",
        function($event, $dispatcher, $exception) use ($response)
        {
            switch ($exception->getCode()) {
                case \Phalcon\Mvc\Dispatcher::EXCEPTION_HANDLER_NOT_FOUND:
                case \Phalcon\Mvc\Dispatcher::EXCEPTION_ACTION_NOT_FOUND:
                    // since you can't do forward between modules, we use a permanent redirect
                    $response->redirect('error/show404', false, 301);
                    return false;
            }
        }
    );

    //Bind the EventsManager to the Dispatcher
    $dispatcher->setEventsManager($eventsManager);

    $di->set('dispatcher', $dispatcher);

Or you can take a look here. It works, but since you remove the default routing behavior (new Router(false)) you have to define all the routes yourself or you get a 404 page.

https://forum.phalcon.io/discussion/1394/404-page-not-work

You can further read about this:

https://docs.phalcon.io/en/latest/reference/dispatching.html#handling-not-found-exceptions

https://docs.phalcon.io/en/latest/reference/routing.html#default-behavior

https://docs.phalcon.io/en/latest/api/Phalcon_Http_Response.html

use this simple solution instead (just add notFound route to your router):

$router = new Phalcon\Mvc\Router();

// add routes .....

$router->notFound(array(
  'module' => 'default',
  "controller" => "error",
  "action" => "notFound"
));

// blah blah blah ....

$di->set('router', $router);

that's what he already said that he tried and doesn't work because of the default routing behavior. The default route is looking for a controller and an action; when the controller can't be found you get an error and not a not found header. Read the links that I provided and you'll understand.