路径中的可选前缀部分

时间:2015-10-21 14:04:42

标签: symfony

我试图创建一个相当复杂的路线架构而且我被卡住了。

我想要匹配的路线

/books                  indexAction
/books/show             showAction
/books/authorname       indexAction
/books/authorname/show  showAction

当前设置

的routing.yml

actions:
  resource: routing/actions.yml
  prefix: /books/{authorname}
  requirements:
      authorname: ".*"
  defaults:
      authorname: ''

路由/ actions.yml

books_index:
    path:   ""
    defaults: { _controller:bookController:indexAction }

books_show:
    path:   "/show"
    defaults: { _controller:bookController:showAction }

此设置目前仅匹配:

/books/                  indexAction
/books/show             showAction
/books/authorname       indexAction
/books/authorname/show  showAction

但不是

/books

我将这些路由分成两个文件的原因是,实际上操作包含更多路由,还有其他路由类别,然后是操作。

我知道我可以在没有前缀的情况下手动定义 / books 路由,但我想避免这种情况,因为此模式将用于许多不同的模块。

1 个答案:

答案 0 :(得分:2)

在任何情况下都需要/book前缀。

一种解决方案是在routing.yml中为此定义单独的条目。一个使用/books前缀,第二个使用/books/{authorname}

index:
  resource: routing/actions.yml
  prefix: /books

show:
  resource: routing/actions.yml
  prefix: /books/{authorname}

它不是那么优雅,但在这种情况下,你可以摆脱额外的requirements部分。

希望这有帮助!