带有可选参数的路由在 Symfony 2 中不起作用 [英] Route with optional parameter is not working in Symfony 2

查看:43
本文介绍了带有可选参数的路由在 Symfony 2 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的路由中的可选参数遇到了一些问题.我想我根据 文档 但还是不行.

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 }

并且希望路由能够被/test/wizard/someTestName 调用,然后使用默认值 1 填充 step 参数,但是每次我只使用测试名称调用路由时,我都会得到以下信息:

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" 

当我通过/test/wizard/someTestName/1/调用路由时,它工作得很好.为什么我为 step 定义的默认值不起作用?有什么建议么?谢谢.

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.

推荐答案

如果参数后面有字符(在您的情况下为 /),则无法将参数设为可选.您必须定义两条路线:

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 }

这篇关于带有可选参数的路由在 Symfony 2 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆