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

How to handle 404 in phalcon MVC

Hello~ I know that there is an event called dispatch:beforeException could handle 404 exceptions.

But I found that it couldn't handle requests like /111 or /中文. PhalconPHP will treat them as requesting '/`.

Is there any solutions? Thanks!

https://docs.phalcon.io/en/latest/reference/routing.html#not-found-paths

//Set 404 paths
$router->notFound(array(
    "controller" => "index",
    "action" => "route404"
));

Thank you! I have found this but I dont know how to integrate it to my existing code. I use Phalcon's native MVC router so I dont have a $router variable. I tried to copy Phalcon's native route rules and add notFound fallbacks but it seems not working well in all circumstances.

Could you give me a detailed example ? Thank you!

Show your code

This should work:

<?php
$DI->set('router',function() use($Config){
    $Router = new \Phalcon\Mvc\Router();
    $Router->notFound(array(
        "controller" => "index",
        "action" => "route404"
    ));
    return $Router;
});

The default route rules that Phalcon\Mvc\Router() have should still be in effect.