Optional CodeIgniter

I am trying to use routing in CI to create a registration form

signup redirects to user/signup

But my registration function may contain a parameter: function signup($type = 1)

How can I make this optional through routing? I tried $route['signup/?(:num)'] = 'user/signup/$1'; but when switching to /signup I get 404, only /signup/1/ works.

+10
codeigniter routing routes
source share
3 answers

The clearest way to express this would probably be to declare both routes:

 $route['signup'] = "user/signup"; $route['signup/(:num)'] = "user/signup/$1"; 
+16
source share

For anyone reading this over time, I believe the answer should be $route['signup/?(:num)?'] , Which makes the number optional. I had similar problems on something else.

+13
source share

The problem with @ Ukuser32's answer is that it allows you to accept URIs such as signup69, which in this case can be harmless, but generally undesirable. Just put a slash with the one captured: num

 $route['signup(/:num)?'] = "user/signup$1" 

And note that if you have several optional segments, you will need to nest them ....

+2
source share

All Articles