CodeIgniter Forums
Codeigniter 4 routing with optional placeholder - 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: Codeigniter 4 routing with optional placeholder (/showthread.php?tid=77693)



Codeigniter 4 routing with optional placeholder - AndreaL - 10-06-2020

Hello,
I am fairly new to Codeigniter and have a problem with routing when passing a parameter.
The problem is that the parameter I need to pass is a variable not always present.
Essentially I need the following two cases to be valid when routing

$routes->add('area-riservata','Areariservata::index');
$routes->add('area-riservata/(:any)','Areariservata::index/$1');

But cannot have them both at the same time in the Routes.php file.
How can I have the above two cases coexist at the same time?

Thanks for your help.


RE: Codeigniter 4 routing with optional placeholder - John_Betong - 10-06-2020

Try reversing the two  routes because as soon as an exact match is found the routes stop searchingSmile


RE: Codeigniter 4 routing with optional placeholder - AndreaL - 10-06-2020

Such simple solution!! Works perfectly.
Thanks so much! You made me save a lot of time