Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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..



33.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.phalconphp.com/en/latest/reference/routing.html#defining-routes