John Davidson

php - Knp Pagination in symfony 5

0 comments
Message:


I installed "Knp Paginator" and everything works fine but if i try to do a search in the search bar (this search bar does not depend on Knp Paginator) i have this error :


"Knp\Bundle\PaginatorBundle\Twig\Extension\PaginationExtension::render(): Argument #2 ($pagination) must be of type Knp\Bundle\PaginatorBundle\Pagination\SlidingPaginationInterface, array given, called in... /var/cache/dev/twig/36/36da62d992e743004744882a10f47b6d89340c107d735e823c151b7c459ca09f.php on line 214"
=>see image.


If i disable Knp Paginator, the search bar work again.


Thank you for your help.


this is my queryBuilder of search bar (ItemRepository):


public function findBySearch($search) 
{
return $this->createQueryBuilder('a')
->andWhere('a.city LIKE :val')
->setParameter('val', '%' . $search . '%')
->orWhere('a.title LIKE :title')
->setParameter('title', '%' . $search . '%')
->orWhere('a.city LIKE :city')
->setParameter('city', '%' . $search . '%')
->orWhere('a.zipCode LIKE :zip')
->setParameter('zip', '%' . $search . '%')
->orWhere('a.hiddenDetail LIKE :hiddenDetail')
->setParameter('hiddenDetail', '%' . $search . '%')
->getQuery()
->getResult();
}

Controller (searchBar) :


   /**
*@Route("/searchItem", name="searchItem")
*
*/
public function searchItem(Request $request, ItemRepository $itemRepository)
{

$search = $request->request->get('search');
$items = $itemRepository->findBySearch($search);


return $this->render('home/listItem.html.twig', [

'items' => $items


]);
}

Controller (Knp Paginator) :


public function listItem(ItemRepository $repository, PaginatorInterface $paginator, Request $request): Response
{


$data = $repository->findAll();

// PAGINANTION KNP/PAGINATOR
$items = $paginator->paginate(
$data,
$request->query->getInt('page', 1),
8

);

return $this->render('home/listItem.html.twig', [

'items' => $items,

]);
}


My Answer:
Knp Paginator Bundle in Symfony 5 by following these steps:

1. Install the Knp Paginator Bundle using Composer:
bash
composer require knplabs/knp-paginator-bundle


2. Enable the bundle in your `config/bundles.php` file:
php
return [
// Other bundles...
Knp\Bundle\PaginatorBundle\KnpPaginatorBundle::class => ['all' => true],
];


3. Configure the bundle in your `config/packages/knp_paginator.yaml` file:
yaml
knp_paginator:
page_range: 5
default_options:
page_name: page
sort_field_name: sort
sort_direction_name: direction
distinct: true
template:
pagination: '@KnpPaginator/Pagination/twitter_bootstrap_v4_pagination.html.twig'
sortable: '@KnpPaginator/Pagination/sortable_link.html.twig'


4. Use the paginator service in your controller to paginate your data:
php
use Knp\Component\Pager\PaginatorInterface;

public function index(PaginatorInterface $paginator)
{
$query = $entityManager->createQuery('SELECT p FROM App\Entity\Post p');
$posts = $paginator->paginate(
$query,
$request->query->getInt('page', 1),
10
);

return $this->render('post/index.html.twig', [
'posts' => $posts,
]);
}


5. Update your Twig template to display the pagination links:
twig
{{ knp_pagination_render(posts) }}


That's it! You should now have Knp Paginator set up and working in your Symfony 5 project.

Rate this post

4 of 5 based on 9543 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us