Api pagination in symfony 3

Viewed 32

I am new to symfony I need to create api and paginate the result I used to do that easily in Laravel Is there anything that would help me do the same with symfony 3.6?

1 Answers

symfony doesnt ship that out of the box.

there is pagerfanta and KnpPaginatorBundle. I read a bunch of subjective opinions that pagerfanta is better here https://github.com/symfony/demo/issues/305

depending on the size/amount of the APIs you need to create, you should maybe look into ApiPlatform. There is as well good videos about ApiPlatform on symfonycasts for 25$ for one month (am not affiliated)

composer require babdev/pagerfanta-bundle

composer require pagerfanta/doctrine-orm-adapter

composer require pagerfanta/twig

in controller

        $pagerfanta = new Pagerfanta(new QueryAdapter($queryBuilder));
        $pagerfanta->setMaxPerPage(5);
        $pagerfanta->setCurrentPage($request->query->get('page', 1));
        return $this->render('yourpage/index.html.twig', [
            'pager' => $pagerfanta,
        ]);

in view

        {% for yourEntity in pager %}
           ...
        {% endfor %}
        {{ pagerfanta(pager) }}

see as well

https://symfonycasts.com/screencast/doctrine-relations/pager-links#play

Related