Welcome Guest, Not a member yet? Register   Sign In
Route with optional parameter
#1

(This post was last modified: 07-17-2020, 10:54 AM by Cyto5.)

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');
$routes->get('logout''Login::logout'); 


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')
$routes->get('logout/(:any)''Login::logout/$1')
$routes->get('logout/(:any)/(:any)''Login::logout/$1/$2')
$routes->get('logout/(:any)/(:any)/(:any)''Login::logout/$1/$2/$3')
$routes->get('logout/(:any)/(:any)/(:any)/(:any)''Login::logout/$1/$2/$3/$4')
$routes->get('logout/(:any)/(:any)/(:any)/(:any)/(:any)''Login::logout/$1/$2/$3/$4/$5'
Reply


Messages In This Thread
Route with optional parameter - by Cyto5 - 07-17-2020, 10:52 AM
RE: Route with optional parameter - by tgix - 07-17-2020, 11:03 PM
RE: Route with optional parameter - by jreklund - 07-18-2020, 02:12 AM
RE: Route with optional parameter - by tgix - 07-18-2020, 02:57 AM
RE: Route with optional parameter - by MGatner - 07-22-2020, 02:03 PM
RE: Route with optional parameter - by edyahui - 11-16-2020, 06:27 PM



Theme © iAndrew 2016 - Forum software by © MyBB