在可选参数注解Symfony2的路线

编程入门 行业动态 更新时间:2024-10-27 12:25:35
本文介绍了在可选参数注解Symfony2的路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我创建了可选的参数控制这样的路线:

i created a route with optional parameter in controller like this:

/** * League action * * @Route("/association/{assoc}/{league}", name="league", requirements={"league" = "\d+"}, defaults={"game" = null}) * @Route("/association/{assoc}/{league}/{game}") * @Template() * * @param $assoc * @param $league * @param $game * @return array */ public function leagueAction($assoc, $league, $game)

但是,如果我尝试创建与这个命名路线的链接,可选参数被省略:

but if i try to create a link with this named route, the optional parameter is ommitted:

{{ path('league', {'assoc': association.short, 'league': league.id, 'game': g.id}) }}

生成的链接是

/协会/ BVNR / 7

/association/BVNR/7

我想什么?

推荐答案

在下面的定义,

* @Route("/association/{assoc}/{league}", name="league", requirements={"league" = "\d+"}, defaults={"game" = null}) * @Route("/association/{assoc}/{league}/{game}")

两条路线都与你的行动,第一个(名为联盟这没有任何默认参数和第二无名一(因为你没'不要再增加name属性),这也没有任何缺省参数。

two routes are related to your action, the first one (named "league" which doesn't have any default parameter and a second unnamed one (as you didn't add name attribute) which also doesn't have any default parameter.

如何解决...

  • 添加名称你的第二个路线,并调用它,因为它包含了游戏参数。
  • 将游戏参数的默认值,你的第二个路线(因为它是唯一一个有一个游戏参数。
  • (你并不真的需要定义两条路线,看看在如何提高?我的答案部分)。
  • Add a name to your second route and call it as it contains "game" parameter.
  • Move the default value of "game" parameter to your second route (As it the only one to have a game parameter.
  • (You don't really need to define two routes, take a look at the "How to improve ..." part of my answer).

试试这个...

* @Route("/association/{assoc}/{league}/{game}", name="league_game", requirements={"league" = "\d+"}, defaults={"game" = null})

虽然你应该叫league_game而不是联盟,

{{ path('league_game', {'assoc': association.short, 'league': league.id, 'game': g.id}) }}

如何提高...

请确保你确实需要定义两条路线,因为我建议只保留一条路径。

Make sure you really need to define two routes, because I would suggest keeping only one route.

至于有一个默认值游戏在下面的定义,

As there's a default value for "game"in the following definition,

@Route("/association/{assoc}/{league}/{game}", name="league", requirements={"league" = "\d+"}, defaults={"game" = null}

然后,它涵盖了两个版本,有和没有游戏。

更多推荐

在可选参数注解Symfony2的路线

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

发布评论

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

>www.elefans.com

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