CodeIgniter optional parameter

▼魔方 西西 提交于 2019-12-18 19:41:13

问题


I'm trying to use routing in CI to create a signup form

signup is re-routed to user/signup

But my signup function can contain a paramater: function signup($type = 1)

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


回答1:


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

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



回答2:


For anyone else reading this in due course - I believe the answer should be $route['signup/?(:num)?'] which makes the number optional as well. I had similar issues on something else.




回答3:


The trouble with @Ukuser32's answer is that it allows for URIs like signup69 to be accepted, which in this case might be innocuous but in the general case is undesirable. Just put the slash in with the captured :num

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

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



来源:https://stackoverflow.com/questions/14183231/codeigniter-optional-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!