Phalcon php router notFound not working

Ally picture Ally · May 22, 2014 · Viewed 8.5k times · Source

I'm just bashing about with Phalcon right now, trying to get my head around it as I'm thinking of porting a big project from CodeIgniter to Phalcon or another framework before development continues any further.

I'm using the router to try and catch 404 errors, but this is not working as expected. The router it's self seems to be working as I also have $router->add("/" to fetch the site index controller if index.php is not defined, and if I comment that out, going to home/ then throws a 404! However, typing in some non existent controller name e.g. test, gives me the error "PhalconException: TestController handler class cannot be loaded". My htaccess is as per the Phalcon docs tutorials.

In the bootstrap (index.php) I have;

$di->set('router', function() { require __DIR__ . '/../app/config/routes.php'; return $router; });

And my routes.php looks like this;

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

// default
$router->add("/", array(
    'controller' => 'index',
    'action' => 'index'
));

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

$router->handle();
return $router;

The site has user submitted groups that are reached by domain.com/group-name/ In CI I was using a 404 rout to load the group controller which would activate a search for that group. If found it would display the group and if not it would throw a 404 page with 404 headers. It may well be that there's a better way of doing that in Phalcon, but I would at least love to know why my notFound rout is failing to work ;)

EDIT: Not sure if this changes anything, but I am using a base controller "controllerBase" which all main controllers are extending. It just contains some vars and an initialize function and another function that can be called from controllers. I wouldn't have thought this would effect the notFound routing, but thought I better mention it any way.

Answer

jpaylor picture jpaylor · May 30, 2014

The default behavior of \Phalcon\Mvc\Router does not make use of the notFound() method.

If you'd like to use notFound(), you need to disable the default routes first as described here: http://docs.phalconphp.com/en/latest/reference/routing.html#default-behavior

In your example, you simply need to change:

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

to the following:

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