2014-11-24 60 views
0

我想檢索form(method =「get」)發送的參數並將它們添加到路由中。Symfony2,爲表格發送的路由添加額外的參數

這是路線

frontend_list: 
path:  /travels/{page} 
defaults: { _controller: ProjectFrontendBundle:Frontend:list, page: 1 } 

,這是形式

  <form action="" method="get" class="form_sort" id="myForm"> 
      <span class="manage_title">Sort by:</span> 
       <select class="select_styled white_select" id="sort_list" name="sort" onChange="sendForm();"> 
         <option value="">-------</option> 
         <option value="country:asc">Country A-Z</option> 
         <option value="country:desc">Country Z-A</option> 
         <option value="destination:asc">City A-Z</option> 
         <option value="destination:desc">City Z-A</option> 
       </select> 
      </form>  

,這是控制器

public function listAction($page, Request $request) 
{ 
    $em = $this->getDoctrine()->getManager(); 

    $nbByPage = $this->container->getParameter('travel.number_by_page'); 

    if ($request->getMethod() == 'POST') 
    { 

     $sort = $request->query->get('sort'); 
     list($orderBy, $orderWay) = explode(":", $sort); //explode 

     $listTravels = $em->getRepository('ProjectTravelBundle:Travel')->getListTravelsFrontend($nbByPage, $page, $orderBy, $orderWay); 

     return $this->render('ProjectFrontendBundle:Frontend:list.html.twig',array(
      'listTravels' => $listTravels, 
      'page'  => $page, 
      'nb_page' => ceil(count($listTravels)/$nbByPage) ?: 1 
     )); 
    } 

    $orderBy = "id"; // set default orderBy 
    $orderWay = "desc"; // set default orderWay 

    $listTravels = $em->getRepository('ProjectTravelBundle:Travel')->getListTravelsFrontend($nbByPage, $page, $orderBy, $orderWay); 

    return $this->render('ProjectFrontendBundle:Frontend:list.html.twig',array(
     'listTravels' => $listTravels, 
     'page'  => $page, 
     'nb_page' => ceil(count($listTravels)/$nbByPage) ?: 1 
    )); 
} 

所以我想有網址這樣,例如選擇一個選項「排序」時:

localhost/agence/web/app_dev.php/travels?orderby=country&orderway=aesc 

現在,我得到一個非功能性的url像這樣的時候選擇一個選項:

localhost/agence/web/app_dev.php/voyages?sort=country%3Aasc 

所以我的問題是如何在路由frontend_list添加這些參數,並將它們添加到樹枝視圖的路徑參數頁面旁邊有一個正確的URL以分頁

     {% if nb_page > 1 %} 
          {% if page == 1 %} 
         <a class="link_prev">Previous</a> 
          {% else %} 
         <a href="{{ path('frontend_list', {'page': page - 1}) }}" class="link_prev">Previous</a> 
          {% endif %} 

          {% if page == nb_page %} 
         <a class="link_next">Next</a> 
          {% else %} 
         <a href="{{ path('frontend_list', {'page': page + 1}) }}" class="link_next">Next</a> 
          {% endif %} 
         {% endif %} 

回答

2

這不是創建可排序列表的好方法,不是在symfony中。

我建議你看看KnpPaginatorBundle - 搜索引擎優化友好Symfony2 paginator排序和分頁。

但是,如果你需要使用上面寫的代碼。我建議你做第二次選擇,分開選擇ASC/DESC。

+0

謝謝,我想問你我可以使用選擇列表來按名稱desc或價格對數據進行排序,例如使用KnpPaginatorBundle進行排序? – hous 2014-11-24 23:42:00

+0

查看下面的答案,我無法在評論中提出 - 這太長了。 – 2014-11-25 06:58:04

2

我認爲這是可能的,在安裝包後,您必須在第一個模板「KnpPaginatorBundle:Pagination:sortable_link.html.twig」中覆蓋。由於默認模板是用於鏈接的,因此您應該爲選擇選項創建一個新模板。要做到這一點,在項目中創建結構 「應用程序/資源/ KnpPaginatorBundle /視圖/分頁/ sortable_option.html.twig

<option {% for attr, value in options %} {{ attr }}="{{ value }}"{% endfor %}>{{ title }}</option> 

現在你的代碼需要小的修改:

<form action="" method="get" class="form_sort" id="myForm"> 
    <span class="manage_title">Sort by:</span> 
     <select class="select_styled white_select" id="sort_list" name="sort" onChange="this.options[this.selectedIndex].value && (window.location = this.options[this.selectedIndex].value);"> 
      {{ knp_pagination_sortable(pagination, 'Country', 'object.countryProperty', {'direction': 'asc'}) }} 
      {{ knp_pagination_sortable(pagination, 'Country', 'object.countryProperty', {'direction': 'desc'}) }} 
      {{ knp_pagination_sortable(pagination, 'City', 'object.cityProperty', {'direction': 'asc'}) }} 
      {{ knp_pagination_sortable(pagination, 'City', 'object.cityProperty', {'direction': 'desc'}) }} 
     </select> 
</form> 

編輯:

當然,您需要更改配置文件中的路徑

knp_paginator: 
    page_range: 5      # default page range used in pagination control 
    default_options: 
     page_name: page    # page query parameter name 
     sort_field_name: sort   # sort field query parameter name 
     sort_direction_name: direction # sort direction query parameter name 
     distinct: true     # ensure distinct results, useful when ORM queries are using GROUP BY statements 
    template: 
     pagination: KnpPaginatorBundle:Pagination:sliding.html.twig  # sliding pagination controls template 
     sortable: KnpPaginatorBundle:Pagination:sortable_option.html.twig # sort option template 
     #sortable: KnpPaginatorBundle:Pagination:sortable_link.html.twig # sort link template (default) 
+0

我得到了這個錯誤'ClassNotFoundException:嘗試從D:\ wamp \ www \ agence \ app \ AppKernel.php行中的命名空間「Knp \ Bundle \ PaginatorBundle」加載類「KnpPaginatorBundle」。您是否需要「使用」它從另一個命名空間?' – hous 2014-11-25 09:59:20

+0

appKernel:'new Knp \ Bundle \ PaginatorBundle \ KnpPaginatorBundle(),' 組件在** vendor \ knp-components **中,捆綁在** vendor \ bundles \ knp \ BundlePaginatorBundle ** – hous 2014-11-25 10:03:50

+0

您需要通過作曲家下載。 [鏈接](https://github.com/KnpLabs/KnpPaginatorBundle#installation-and-configuration) 在這裏您可以獲得項目中的軟件包安裝說明。 – 2014-11-25 10:04:51