CodeIgniter Forums
Route Optional Parameter - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forum-28.html)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forum-30.html)
+--- Thread: Route Optional Parameter (/thread-76268.html)



Route Optional Parameter - segungreat - 04-28-2020

Hello

I am trying to add optional parameter to my and seem not getting it. The documentation did not mention anything about it.

Some thing like this
PHP Code:
$routes->get('user/(optional)'"Home::user"); 


Please any idea

Thanks


RE: Route Optional Parameter - InsiteFX - 04-28-2020

PHP Code:
$routes->get('user/(:any)'"Home::user/$1");

// or

$routes->get('user/(:num)'"Home::user/$1"); 

There are other things that you can do also.

A route with (:any) should be the last route in the route list.


RE: Route Optional Parameter - jreklund - 04-28-2020

Here are the chapter you where looking for: https://codeigniter.com/user_guide/incoming/routing.html


RE: Route Optional Parameter - segungreat - 04-28-2020

(04-28-2020, 08:04 AM)InsiteFX Wrote:
PHP Code:
$routes->get('user/(:any)'"Home::user/$1");

// or

$routes->get('user/(:num)'"Home::user/$1"); 

There are other things that you can do also.

A route with (:any) should be the last route in the route list.

Thank you for your reply

I have tried all this, it's not working, when i go the "mysite/user/anything_here" it works but when I go to "mysite/user" it show 404 error (Can't find a route for 'user')

Any other option pls



RE: Route Optional Parameter - segungreat - 04-28-2020

(04-28-2020, 09:51 AM)jreklund Wrote: Here are the chapter you where looking for: https://codeigniter.com/user_guide/incoming/routing.html

Thank you, I have gone through the documentation, it has no information about optional parameter


RE: Route Optional Parameter - jreklund - 04-28-2020

You need to route user as well.

PHP Code:
$routes->get('user/(:num)'"Home::user/$1"); 
$routes->get('user'"Home::user"); 



RE: Route Optional Parameter - segungreat - 04-28-2020

(04-28-2020, 09:56 AM)jreklund Wrote: You need to route user as well.

PHP Code:
$routes->get('user/(:num)'"Home::user/$1"); 
$routes->get('user'"Home::user"); 
Thank you, now working.
Thought i can just use one route to do that just like in laravel