如何在Symfony2路由中具有可选参数?(How can i have optional parameters in Symfony2 route)
我有以下代码:
/** * Lists all User entities. * * @Route("/{cid}",defaults={"cid" = null},name="user") * @Template() */ public function indexAction($cid=null) {}现在,如果我键入site/user/1那么它可以工作,但如果我键入site/user/它说:
No route found我怎么能这样做呢?
I have this code below :
/** * Lists all User entities. * * @Route("/{cid}",defaults={"cid" = null},name="user") * @Template() */ public function indexAction($cid=null) {}Now if I type site/user/1 then it works, but if I type site/user/ it says:
No route foundHow can I have it that both routes work?
最满意答案
尝试去site/user (请注意,末尾没有反斜杠)。
一般它应该工作,我有相似的配置工作。
但是如果所有其他方式都失败,您可以随时为同一动作定义多个路由,即
/** * Lists all User entities. * * @Route("/", name="user_no_cid") * @Route("/{cid}", name="user") * @Template() */ public function indexAction($cid=null) {Try to go to site/user (notice no backslash at the end).
Generally it should work, I have relatively similar configuration working.
But if all else fails you can always define multiple routes for same action, i.e.
/** * Lists all User entities. * * @Route("/", name="user_no_cid") * @Route("/{cid}", name="user") * @Template() */ public function indexAction($cid=null) {更多推荐
发布评论