Welcome Guest, Not a member yet? Register   Sign In
Route Hierarchy (order)
#1

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
Reply
#2

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.
Reply
#3

(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
Reply
#4

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
Reply
#5

Thank you @kenjis .
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#6

(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
Reply
#7

See https://codeigniter4.github.io/CodeIgnit...e-priority
Reply
#8

(08-19-2022, 04:12 PM)kenjis Wrote: See https://codeigniter4.github.io/CodeIgnit...e-priority

Nice!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB