2016-02-01 17:50
浏览 41


I'm currently facing an issue I have no idea how to solve. For a route, I can have the following annotation :

@Route("/foo/{choice}", name="foo_route", defaults={"choice" = "bar"})

This route can be reached by :

  • /foo/helloworld
  • /foo/bar
  • /foo

The last case is rather problematic. I would like the choice to be always visible in the url, even if it is the default choice. Is such a thing possible ?

图片转代码服务由CSDN问答提供 功能建议

我目前正面临一个问题,我不知道如何解决。 对于一条路线,我可以拥有 以下注释:

  @Route(“/ foo / {choice}”,name =“foo_route”,defaults = {“choice”=“bar”})
 <  / code>  


  • / foo / helloworld
  • / foo / bar
  • / foo

    最后一种情况相当棘手。 我希望选择始终在URL中可见,即使它是默认选择。 这样的事情可能吗?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doutandusegang2961 2016-02-01 17:55

    One way to do this would be something like this:

     * @Route("/foo/{choice}", name="foo_route")
    function fooAction($choice = NULL) {
        if ($choice === NULL) {
            return $this->redirectToRoute('foo_route', ['choice' => 'bar']);

    That is, remove the defaults, allow $choice === NULL, and force the redirect to your "default" value.

    Alternatively, you could embed JavaScript logic in the page to use history manipulation to modify the url. This would allow you to avoid forcing a redirect. However, forcing the redirect is probably easier, and keeps the logic for handling this entirely in the PHP.

    解决 无用
    打赏 举报

相关推荐 更多相似问题