codeigniter4 route optional parameter code example

Example 1: codeigniter4 route optional parameter

$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')

Example 2: how to set optional third parameter in routes of codeigniter

$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;
}

Tags:

Php Example