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

Router matches wrong route

Hi,

We have a problem with setting language in our backend module. We use Phalcon 2.1.0 with PHP 5.6.14.

The corresponding part of our router looks like this:

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

$router->setDefaultModule("frontend");

//This is the desired route
$router->add("/admin/set-language/{language:[a-z]+}", array(
    'module' => 'backend',
    'namespace' => 'App\Modules\Backend\Controllers',
    'controller' => 'index',
    'action' => 'setLanguage'
));

/* have some other routes here */

//This route is matched
$router->add("/admin/:controller/:action(/)?", array(
    'module' => 'backend',
    'namespace' => 'App\Modules\Backend\Controllers',
    'controller' => 1,
    'action'     => 2
));

/* have some other routes here */

The language setter buttons look like <a href="/admin/set-language/hu">HU</a>.

The language setter function is in the backend's ControllerBase like setLanguageAction (it's like in the Phalcon examples).

When we click on the language setter button, we get: App\Modules\Backend\Controllers\SetLanguageController handler class cannot be loaded.

As we dumped out the router match, it showed that not the desired route has matched, but the other one which threats the part after "/admin/" as controller.

Why the route is not mached even if it's correct? Is there any mistake we can't see?

As we know, we should list the custom routes from the specific ones to the more general ones, right? So what's the problem here?

Thanks in advance.



145.0k
Accepted
answer
edited Apr '16

Just add this:

//This is the desired route
$router->add("/admin/set-language/{language:[a-z]+}", array(
    'module' => 'backend',
    'namespace' => 'App\Modules\Backend\Controllers',
    'controller' => 'index',
    'action' => 'setLanguage'
));

after:

$router->add("/admin/:controller/:action(/)?", array(
    'module' => 'backend',
    'namespace' => 'App\Modules\Backend\Controllers',
    'controller' => 1,
    'action'     => 2
));

Second route overrites the first one right now. I don't know how in other frameworks but in phalcon you have to specific routes from generic to specific.

Yes, it was the problem. It seems we know the order rule wrong, but it's clear now.

Thanks for the help!