Symfony2,为表单发送的路由添加其他参数

时间:2014-11-24 19:10:19

标签: php symfony doctrine-orm routing

我想检索表单发送的参数(method =" get")并将它们添加到路径中。

这是路线

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

所以我希望在选择一个选项&#34; sort&#34; :

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

现在,当我选择一个选项时,我得到一个像这样的非功能性网址:

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

所以我的问题是如何在路径 frontend_list 中添加这些参数,并将它们添加到参数页面旁边的树枝视图中的路径,以获得具有分页:

                        {% 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 个答案:

答案 0 :(得分:2)

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

我建议你看看KnpPaginatorBundle - SEO友好的Symfony2 paginator进行排序和分页。

但是,如果你需要使用上面写的代码。我建议你做第二个选择,分开选择ASC / DESC。

答案 1 :(得分:2)

我认为在安装包之后你必须覆盖第一个模板“ KnpPaginatorBundle:Pagination:sortable_link.html.twig ”。由于默认模板用于链接,因此您应为select选项创建新模板。为此,请在项目结构中创建“ app / Resources / KnpPaginatorBundle / views / Pagination / 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)