9951 explained code solutions for 126 technologies


php-symfonyHow to implement pagination in PHP Symfony?


Pagination in PHP Symfony can be implemented using the KnpPaginatorBundle. This bundle provides a simple way to paginate the results of any query.

Example code

use Knp\Component\Pager\PaginatorInterface;

public function index(PaginatorInterface $paginator, Request $request)
{
    $query = $this->getDoctrine()->getRepository(Product::class)->findAll();

    $pagination = $paginator->paginate(
        $query,
        $request->query->getInt('page', 1),
        $request->query->getInt('limit', 10)
    );

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

Output example

Array
(
    [currentPageNumber] => 1
    [numItemsPerPage] => 10
    [items] => Array
        (
            [0] => Product Object
                (
                    [id] => 1
                    [name] => Product 1
                    [price] => 10.00
                )

            [1] => Product Object
                (
                    [id] => 2
                    [name] => Product 2
                    [price] => 20.00
                )

            [2] => Product Object
                (
                    [id] => 3
                    [name] => Product 3
                    [price] => 30.00
                )

        )

    [count] => 3
    [first] => 1
    [last] => 1
    [numItems] => 3
    [pageRange] => 1
    [firstPageInRange] => 1
    [lastPageInRange] => 1
    [current] => 1
    [totalCount] => 3
    [firstItemNumber] => 1
    [lastItemNumber] => 3
)

The code consists of the following parts:

  1. Use KnpPaginatorBundle: use Knp\Component\Pager\PaginatorInterface;
  2. Get the query results: $query = $this->getDoctrine()->getRepository(Product::class)->findAll();
  3. Paginate the query results: $pagination = $paginator->paginate($query, $request->query->getInt('page', 1), $request->query->getInt('limit', 10));
  4. Render the paginated results: return $this->render('product/index.html.twig', ['pagination' => $pagination]);

Helpful links

Edit this code on GitHub