Route with optional parameter is not working in Symfony 2

拈花ヽ惹草 提交于 2019-12-13 04:35:01

问题


I am experiencing some issues with optional parameters in my routes. I think I did it properly according to the documentation but still it is not working.

So I have the following route defined:

test_wizard:
    pattern:  /test/wizard/{testName}/{step}/
    defaults: { _controller: TestBundle:Wizard:wizard, step: 1 }

and would like the route to be able to be called by /test/wizard/someTestName and then fill in the step parameter with the default value of 1 but everytime I call the route just with the test name I get the following instead:

No route found for "GET /test/wizard/someTestName" 

When I call the route by /test/wizard/someTestName/1/ itworks just fine. Why is my defined default value for step not working? Any suggestions? Thanks.


回答1:


It is not possible to make a parameter optional if you have a character after it (/ in your case). You have to define two routes:

test_wizard:
    pattern:  /test/wizard/{testName}
    defaults: { _controller: TestBundle:Wizard:wizard }

test_wizard_optional:
    pattern:  /test/wizard/{testName}/{step}/
    defaults: { _controller: TestBundle:Wizard:wizard, step: 1 }


来源:https://stackoverflow.com/questions/21078154/route-with-optional-parameter-is-not-working-in-symfony-2

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