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

Routes in Micro

Application gives me a wrong action.

I've got the next situation:

//..
$collection = new \Phalcon\Mvc\Micro\Collection();
$collection->setHandler('App\Controllers\Controller', true);
$collection->get('/teams/top', 'getTop');
$collection->get('/teams/{teamid}', 'getDetails');
//..

when i make request to "/teams/top" url i expect to get to

$collection->get('/teams/top', 'getTop');

cause it stands higher then the second.

BUT am getting to this route

$collection->get('/teams/{teamid}', 'getDetails');

Then i've put "getTop" route AFTER "getDetails" and it worked CORRECT!

Why phalcon acting in this way ?! I thought it goes from the top to bottom when searching matching route template..



34.6k
Accepted
answer

Routes are traversed in reverse order, this is mentioned in the docs:

Since you can add many routes as you need using add(), the order in which routes are added indicate their relevance, latest routes added have more relevance than first added. Internally, all defined routes are traversed in reverse order until Phalcon\Mvc\Router finds the one that matches the given URI and processes it, while ignoring the rest.

https://docs.phalcon.io/en/latest/reference/routing.html#defining-routes