![]() |
Route with optional parameter - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28) +--- Forum: CodeIgniter 4 Feature Requests (https://forum.codeigniter.com/forumdisplay.php?fid=29) +--- Thread: Route with optional parameter (/showthread.php?tid=77085) |
Route with optional parameter - Cyto5 - 07-17-2020 Is there a way to get the route file to work without having to manually define all optional params as separate routes? For example I have a logout route that normally is just "/logout" but I have an optional param to define a code in the url that will give the user a message like your were logged out due to inactivity was the user logging themselves out. I would like just this route to take care of it: PHP Code: $routes->get('logout/(:any)', 'Login::logout/$1'); However the route does not work if you do not include the "/(:any)" in the actual url. The only way I have been able to make it work is using two routes: PHP Code: $routes->get('logout/(:any)', 'Login::logout/$1'); Since my controller has the parameter defined with a default why do I have to setup 2 routes? I am running into this situation on other pages too where I have multiple optional params and they all have defaults in the controller but I have to setup a route for every different variation of total number of params being passed in which is a little annoying and seems overkill. Maybe by default it wouldn't work this way but I don't see an option or anything in the routes to enable a specific url to work this way. Even if I had to do something like this would be okay: PHP Code: $routes->get('logout', 'Login::logout', ['optional_params'=>true,'param_count'=>5,'param_types'=>['(:any)']]); Something like this would be nice you could setup to accept up to X params and provide specific param types for each one or if none or only 1 in the array set them all to the same like (:any) Instead of PHP Code: $routes->get('logout', 'Login::logout') RE: Route with optional parameter - tgix - 07-17-2020 Since the config files are running PHP, you could do something like this in Routes.php: PHP Code: $r = ['logout']; RE: Route with optional parameter - jreklund - 07-18-2020 You don't need to specify multiple (:any), you can just get away with two routes. PHP Code: $routes->get('logout/(:any)', 'Login::logout/$1'); You can't however get away with just one routing rule. As (:any) can't be optional. RE: Route with optional parameter - tgix - 07-18-2020 (07-18-2020, 02:12 AM)jreklund Wrote: You don't need to specify multiple (:any), you can just get away with two routes.Cool, maybe that should be clarified in the docs... RE: Route with optional parameter - MGatner - 07-22-2020 (:any) is pretty much * on any other system, and while it isn’t necessarily intuitive it does *not* include the preceding slash. For example: https:://example.com/logout would match ‘logout’ but not ‘logout/*’ because there is no slash, however it would match ‘logout(:any)’ (notice how slash) RE: Route with optional parameter - edyahui - 11-16-2020 PHP Code: Try This : |