CodeIgniter Forums

Full Version: Route with optional parameter
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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'
Since the config files are running PHP, you could do something like this in Routes.php:
PHP Code:
$r = ['logout'];
$arg = [];
for (
$i 1$i <= 4$i++) {
    
$routes->get(join('/'$r), 'Home::logout/' join('/'$arg));
    
array_push($r'(:any)');
    
array_push($arg'$' $i);

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

public function 
logout($param1 null$param2 null$param3 null)
{
    
var_dump($param1$param2$param3); exit;
}

// or

public function logout(...$params)
{
    
var_dump($params); exit;


You can't however get away with just one routing rule. As (:any) can't be optional.
(07-18-2020, 02:12 AM)jreklund Wrote: [ -> ]You don't need to specify multiple (:any), you can just get away with two routes.
...
You can't however get away with just one routing rule. As (:any) can't be optional.
Cool, maybe that should be clarified in the docs...
(: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)
PHP Code:
Try This 

$routes->get('logout(:any)''Login::logout$1$2$3$5');

It's Work For Me