2014-11-24 120 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