PHP code example of anime-db / pagination-bundle

1. Go to this page and download the library: Download anime-db/pagination-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

anime-db / pagination-bundle example snippets


// app/AppKernel.php
public function registerBundles()
{
    return array(
        // ...
        new AnimeDb\Bundle\PaginationBundle\AnimeDbPaginationBundle(),
        // ...
    );
}

namespace Acme\DemoBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Sensio\Bundle\FrameworkExtraBundle\Configuration;

class ArticleController extends Controller
{
    /**
     * @Configuration\Route("/article/", name="article_index")
     * @Configuration\Method({"GET"})
     *
     * @param Request $request
     *
     * @return Response
     */
    public function indexAction(Request $request)
    {
        $per_page = 100; // articles per page
        $em = $this->get('doctrine.orm.entity_manager');
        $router = $this->get('router');

        // get total articles
        $total = (int)$em
            ->createQueryBuilder()
            ->select('COUNT(*)')
            ->from('AcmeDemoBundle:Article', 'a')
            ->getQuery()
            ->getSingleScalarResult();

        // build pagination
        $pagination = $this
            ->get('pagination')
            ->paginate(
                ceil($total / $per_page), // total pages
                $request->query->get('page') // correct page
            )
            ->setPageLink(function($page) use ($router) { // build page link
                return $router->generate('article_index', ['page' => $page]);
            })
            ->setFirstPageLink($router->generate('article_index')); // build link for first page

        // get articles chunk
        $articles = $em
            ->createQueryBuilder()
            ->select('*')
            ->from('AcmeDemoBundle:Article', 'a')
            ->setFirstResult(($pagination->getCurrentPage() - 1) * $per_page)
            ->setMaxResults($per_page)
            ->getQuery()
            ->getResult();

        // template parameters
        return $this->render('AcmeDemoBundle:Article:index.html.twig', [
            'total' => $total,
            'articles' => $articles,
            'pagination' => $pagination
        ]);
    }
}

namespace Acme\DemoBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Sensio\Bundle\FrameworkExtraBundle\Configuration;
use Acme\DemoBundle\Entity\Article;

class ArticleController extends Controller
{
    /**
     * @var int
     */
    const PER_PAGE = 100;

    /**
     * @Configuration\Route("/article/", name="article_index")
     * @Configuration\Method({"GET"})
     *
     * @param Request $request
     *
     * @return Response
     */
    public function indexAction(Request $request)
    {
        // create get articles query
        // would be better move this query to repository class
        $query = $this
            ->getDoctrine()
            ->getRepository('AcmeDemoBundle:Article')
            ->createQueryBuilder('a')
            ->where('a.status = :status')
            ->setParameter('status', Article::STATUS_ENABLED);

        // build pagination
        $pagination = $this
            ->get('pagination')
            ->paginateQuery(
                $query, // query
                self::PER_PAGE, // articles per page
                $request->query->get('page') // correct page
            )
            ->setPageLink(function($page) { // build page link
                return $this->generateUrl('article_index', ['page' => $page]);
            })
            ->setFirstPageLink($this->generateUrl('article_index')); // build link for first page

        // template parameters
        return $this->render('AcmeDemoBundle:Article:index.html.twig', [
            'total' => $pagination->getTotalPages(), // total pages
            'articles' => $query->getQuery()->getResult(), // get articles chunk
            'pagination' => $pagination
        ]);
    }
}