I have a question for you. I have a filter in the page that sort products by price. If I filter the products and for example I have 3 pages that when I switch to the page 2 the filter dissapear. I need to save this data in session? or exist another solution? My controller :
if ($form->isValid()) {
$aFilter['iMinPrice'] = $form["min_price"]->getData();
$aFilter['iMaxPrice'] = $form["max_price"]->getData();
}
//Search products
$aProducts = $repositoryProduct->getProductsOrderByDateDesc($id,null,$aFilter);
$paginator = $this->get('knp_paginator');
$pagination = $paginator->paginate(
$aProducts,
$page,
3
);
//Send data to view
return $this->render('ShopDesktopBundle:Category:category.html.twig',array(
'category' => $category,
'pagination' => $pagination,
'form' => $form->createView()
));
My repository :
if(!empty($aFilter)){
if(isset($aFilter['iMinPrice'])){
$qb->andWhere('p.product_price >= :price_min')
->setParameter('price_min',$aFilter['iMinPrice']);
}
if(isset($aFilter['iMaxPrice'])){
$qb->andWhere('p.product_price <= :price_max')
->setParameter('price_max',$aFilter['iMaxPrice']);
}
}
And my view :
<form action="{{ path('show_product_category',{ 'id':category.getId(), 'name':category.getCategoryLink() }) }}" method="post" {{ form_enctype(form) }}>
<div class="accordion-inner">
{{ form_widget(form.min_price) }}
{{ form_widget(form.max_price) }}
</div>
</form>
{{ knp_pagination_render(pagination, null, {}, {'style': 'pager', 'prev_label': '← Older', 'next_label': 'Newer →'}) }}
My route :
show_product_category:
path: /{id}/{name}/{page}
defaults: { _controller: ShopDesktopBundle:Category:showCategory, page: 1}
requirements:
id: \d+
page: \d+
_method: GET|POST
Help me please! Thx in advance!!! Any suggestions please how to append query parameters to the link?
You could try to append all GET parameters to all link of the pagination. In this way, you'll have them everytime you switch page.
With KPNPaginator, you could pass your request query to the pagination renderer, like this:
{{ knp_pagination_render(pagination, null, _requestQuery, {'style': 'pager', 'prev_label': '← Older', 'next_label': 'Newer →'}) }}
In fact, KPN documentation states:
By default when render method is triggered, pagination renders the template with standard arguments provided:
pagination parameters
, like pages in range, current page and so on..route
- which is used to generate page, sorting urlsrequest_query
, which contains all GET request parametersextra
, pagination template parametersExcept from pagination parameters, others can be modified or adapted to some use cases. Usually its possible, you might need setting a route if default is not matched correctly (because of rendering in sub requests). Or adding additional query or view parameters.