如何在Symfony2路由中具有可选参数?(How can i have optional parameters in Symfony2 route)

编程入门 行业动态 更新时间:2024-10-26 11:26:38
如何在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 found

How 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) {

更多推荐

本文发布于:2023-08-06 12:01:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1448876.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:可选   路由   参数   如何在   route

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!