我想检索表单(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
));
}
所以我想有这样的网址,例如在选择选项"排序"时:
localhost/agence/web/app_dev.php/travels?orderby=country&orderway=aesc
现在,当我选择一个选项时,我得到一个这样的非功能性网址:
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 %}
这不是
创建可排序列表的好方法,而不是在symfony中。
我建议你看看KnpPaginatorBundle - SEO友好的Symfony2分页器来排序和分页。
但是,如果您需要使用上面编写的代码。我建议你做第二个选择,分开选择ASC/DESC。
我认为这是可能的,在安装捆绑包之后,您必须在第一个模板"KnpPaginatorBundle:Pagination:sortable_link.html.twig"中覆盖。由于默认模板是用于链接,因此您应该为选择选项创建一个新模板。为此,请在项目结构中创建"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)