2015-09-10 62 views
0

如果我這樣定義一個路線:Symfony2的路由:如何不省略自動默認參數

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

將省略{PAGE}和{}限制α參數,如果它們與已過默認參數。

建設與頁面的路線= 2和限價= 4將返回

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

但當{限制}與8時,它會返回

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

,而不是

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

是他們的一種方式,以防止該參數將自動省略,如果他們等於其定義的默認值?

+2

只是不設置默認'limit',你爲什麼默認設置,如果你不需要它? – malcolm

+0

唯一的原因是讓第一頁的URL不帶任何頁面和限制爲「/ search/country/xxx-3-xxxId」。但從第二頁上應該總是頁面,並總是在我看到的網址 – delete

+0

的限制,請檢查我的答案。 – malcolm

回答

0

您可以定義多條到相同的動作:

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