CodeIgniter Forums
Route Hierarchy (order) - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Route Hierarchy (order) (/showthread.php?tid=82751)



Route Hierarchy (order) - SoccerGuy3 - 08-18-2022

Not seeing an obvious answer to my question in the docs or forums, so....
I am wondering about routes and specifically how CI (4.2) handles two similar routes. Does it take the first one it comes across and stop processing the routes or does it keep reading and process the second one? 
Let's say I have the following routes (obviously not real, just for examples):

Code:
$routes->get('/', 'Home::index');
$routes->get('/(:any)', 'Home::cms_page/$1');
$routes->get('/admin/settings', 'Admin::settings');
Then I have the following URLs... what happens with each?
Code:
https://domain.com
I would expect this to match the first route. Would it?
Code:
https://domain.com/about-us
I would expect this one to match the second route. Would it?
Code:
https://domain.com/admin/settings
I would expect this one to match the third route, but concerned it would actually get processed on line 2.
In reading the docs, there is a short discussion about priority, would adding a level of say "5" to the first two all the third URL to process as intended? Or would it be better to just put the first two routes at the very bottom of the routes list?
TIA


RE: Route Hierarchy (order) - iRedds - 08-18-2022

The routes are iterated to the first match, so the URL https://domain.com/admin/settings will be handled by the second route.
You need to move the second route to the very bottom or assign a queue (priority) greater than 0 to it.


RE: Route Hierarchy (order) - SoccerGuy3 - 08-18-2022

(08-18-2022, 10:40 AM)iRedds Wrote: The routes are iterated to the first match, so the URL https://domain.com/admin/settings will be handled by the second route.
You need to move the second route to the very bottom or assign a queue (priority) greater than 0 to it.

Thanks for the info. Kinda what I thought but needed confirmation! Smile


RE: Route Hierarchy (order) - kenjis - 08-18-2022

It seems there is no clear description for route priority in the user guide,
I sent a PR: https://github.com/codeigniter4/CodeIgniter4/pull/6391


RE: Route Hierarchy (order) - InsiteFX - 08-18-2022

Thank you @kenjis .


RE: Route Hierarchy (order) - SoccerGuy3 - 08-19-2022

(08-18-2022, 06:31 PM)kenjis Wrote: It seems there is no clear description for route priority in the user guide,
I sent a PR: https://github.com/codeigniter4/CodeIgniter4/pull/6391

Thanks for doing that! @kenjis


RE: Route Hierarchy (order) - kenjis - 08-19-2022

See https://codeigniter4.github.io/CodeIgniter4/incoming/routing.html#route-priority


RE: Route Hierarchy (order) - SoccerGuy3 - 08-19-2022

(08-19-2022, 04:12 PM)kenjis Wrote: See https://codeigniter4.github.io/CodeIgniter4/incoming/routing.html#route-priority

Nice!