Welcome Guest, Not a member yet? Register   Sign In
Route Optional Parameter
#1

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
Reply
#2

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.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

Here are the chapter you where looking for: https://codeigniter.com/user_guide/incom...uting.html
Reply
#4

(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
Reply
#5

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

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

You need to route user as well.

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

(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
Reply




Theme © iAndrew 2016 - Forum software by © MyBB