CodeIgniter Forums
simpler way to handle multiple segments in URI routing for Codeigniter 3.x - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Development (https://forum.codeigniter.com/forumdisplay.php?fid=6)
+--- Forum: CodeIgniter 3.x (https://forum.codeigniter.com/forumdisplay.php?fid=17)
+--- Thread: simpler way to handle multiple segments in URI routing for Codeigniter 3.x (/showthread.php?tid=87804)



simpler way to handle multiple segments in URI routing for Codeigniter 3.x - BhayarTharya - 06-01-2023

I've upgraded from Codeigniter version 2.x to 3.x and noticed that the URI routing behaves differently when passing in multiple segments.

In version 2.x, I was able to pass the following URL variations:

domain.com/function/arg1
domain.com/function/arg1/arg2
domain.com/function/arg1/arg2/arg3

Where $route['function/(:any)'] = 'function/$1' would work for all three URL variations given that my function call is function($arg1, $arg2 = 0, $arg3 = 0) which allows arg2 and arg3 to be optional and be passed in as $1 in the routing rules.

In order for it to work in version 3.x, I find that I have to set up my routing as:

$route['function/(:any)'] = 'function/$1';
$route['function/(:any)/(:any)'] = 'function/$1/$2';
$route['function/(:any)/(:any)/(:any)'] = 'function/$1/$2/$3';

Is there any way I can simplify the routing so that it will pass all remaining segments without having to create separate routing rules for each variation of the number of possible segments?


RE: simpler way to handle multiple segments in URI routing for Codeigniter 3.x - kenjis - 06-15-2023

See https://www.codeigniter.com/userguide3/installation/upgrade_300.html#routes-containing-any