【问题标题】:Symfony2 Routing: how to don't omit default parameters automaticallySymfony2 路由:如何不自动省略默认参数
【发布时间】:2015-09-10 18:36:50
【问题描述】:

如果我定义这样的路线:

search:
    path:     /search/{country}/{xxx}-3-{xxxId}/{page}/{limit}
    defaults: 
        _controller: SearchBundle:Search:index
        page: 0
        limit: 8

如果 {page} 和 {limit} 参数使用默认参数传递,它将省略它们。

使用 page=2 和 limit=4 构建路由将返回

/search/country/xxx-3-xxxId/2/4

但是当 {limit} 用 8 传递时,它会返回

/search/country/xxx-3-xxxId/2

而不是

/search/country/xxx-3-xxxId/2/8

他们是否有一种方法可以防止参数等于其定义的默认值时自动省略?

【问题讨论】:

  • 只是不要设置默认limit,如果不需要,为什么要设置默认?
  • 唯一的原因是让首页的URL为“/search/country/xxx-3-xxxId”,没有任何页面和限制。但是从第二页开始,应该总是有页面并且总是限制在 url
  • 我明白了,检查我的答案。

标签: php symfony routing


【解决方案1】:

你可以为同一个动作定义多个路由:

search_main:
    path: /search/{country}/{xxx}-3-{xxxId}
    defaults: 
        _controller: SearchBundle:Search:index

search:
    path: /search/{country}/{xxx}-3-{xxxId}/{page}/{limit}
    defaults: 
        _controller: SearchBundle:Search:index

控制器:

public function indexAction($country, $xxx, $xxxId, $page = 0, $limit = 8)
{
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-02
    • 1970-01-01
    • 1970-01-01
    • 2016-03-12
    • 1970-01-01
    相关资源
    最近更新 更多