我想檢索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 %}
謝謝,我想問你我可以使用選擇列表來按名稱desc或價格對數據進行排序,例如使用KnpPaginatorBundle進行排序? – hous 2014-11-24 23:42:00
查看下面的答案,我無法在評論中提出 - 這太長了。 – 2014-11-25 06:58:04