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

Overriding default routes

Hi All! I have a router rules which looks like

$router = new \Phalcon\Mvc\Router(); //note that 'false' is ommitted to support default behavior
$router->add(
    '/admin/settings/rename/{type}/{id}',
    array(
        'controller' => 'api',
        'action' => 'rename'
    )
);

So according to the sample above, I want all requests like '/admin/settings/rename/mytype/1' will be redirected to '/api/rename' with params 'type' and 'id', respectively. But it calls admin controller with settings action every time. 'AdminController' and 'ApiController' exists. I want only selected routes to redirect to 'ApiController' while others should go default way. How to override this concrete route and redirect it? I don't want to create 'renameAction' inside of 'AdminController' and forward request to 'ApiController'. Is it possible to override it using Router?

So the main idea is to check defined routes first and after that (if no such route found) rollback to the default ones (:controller/:action/:params model).



98.9k

Routes are checked in reverse order, there are two routes added to the router by default when you add new ones those are tried first then default routes are used as last resource.

Hmm, that was strange... I've removed

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

and now it works well. Thanks!