2017 © Pedro PelĂĄez
 

symfony-bundle pagination-bundle

Symfony Pagination Twig Extension Bundle

image

tangoman/pagination-bundle

Symfony Pagination Twig Extension Bundle

  • Sunday, April 8, 2018
  • by Tangoman
  • Repository
  • 0 Watchers
  • 0 Stars
  • 136 Installations
  • HTML
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 1 Open issues
  • 12 Versions
  • 1 % Grown

The README.md

Usage

This Bundle allows easy and optimal use of the Doctrine\Paginator method to paginate your requests and make results optimized for SEO. The pagination always use the GET $page to control on wich page you are, so you don't have to worry about the route. Two different responsive templates are available, default.thml.twig and smart.html.twig., (*1)

Installation

Step 1: Download the Bundle

Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:, (*2)

$ composer require tangoman/pagination-bundle

This command requires you to have Composer installed globally, as explained in the installation chapter of the Composer documentation., (*3)

Step 2: Enable the Bundle

Then, enable the bundle by adding it to the list of registered bundles in the app/AppKernel.php file of your project:, (*4)

<?php
// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
    // ...

    public function registerBundles()
    {
        $bundles = array(
            // ...
            new TangoMan\PaginationBundle\TangoManPaginationBundle(),
        );

        // ...
    }
}

Step 3: Configure your repository

<?php
// Bundle/Entity/Repository/User.php

use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Tools\Pagination\Paginator;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

class User extends EntityRepository
{
    public function findByPage($page = 1, $limit = 10)
    {
        if(!is_numeric($page)) {
            throw new \InvalidArgumentException(
                '$page must be an integer ('.gettype($page).' : '.$page.')'
            );
        }

        if(!is_numeric($limit)) {
            throw new \InvalidArgumentException(
                '$limit must be an integer ('.gettype($limit).' : '.$limit.')'
            );
        }

        $dql = $this->createQueryBuilder('user');
        $dql->orderBy('user.lastname', 'DESC');

        $firstResult = ($page - 1) * $limit;

        $query = $dql->getQuery();
        $query->setFirstResult($firstResult);
        $query->setMaxResults($limit);

        $paginator = new Paginator($query);

        if(($paginator->count() <=  $firstResult) && $page != 1) {
            throw new NotFoundHttpException('Page not found');
        }

        return $paginator;
    }
}

Step 4: Make the request in the controller

<?php
// Bundle/Controller/DefaultController.php
namespace Bundle\Controller;

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

class DefaultController extends Controller
{
    /**
     * @Route("/user/", name="app_list_user")
     */
    public function listUserAction(Request $request)
    {
        $db = $this->getDoctrine()->getManager();

        $listUser = $db->getRepository('AppBundle:User')->findByPage(
            $request->query->getInt('page', 1),
            5
        );

        return $this->render('listUser.html.twig', array(
            'listUser' => $listUser
        ));
    }
}

Step 5: Integrate in Twig

<table class="table">
    <thead>
        <tr>
            <th>Lastname</th>
            <th>Firstname</th>
        </tr>
    </thead>
    <tbody>
        {% for user in listUser %}
            <tr>
                <td>{{ user.lastname | upper }}</td>
                <td>{{ user.firstname | capitalize }}</td>
            </tr>
        {% else %}
            <tr>
                <td colspan="2" class="text-center">
                    <em>No Users</em>
                </td>
            </tr>
        {% endfor %}
    </tbody>
    <tfoot>
        <tr>
            <td colspan="2">
                {{ pagination(listUser) }}
            </td>
        </tr>
    </tfoot>
</table>

If you want to use "smart" pagination add following argument:, (*5)

{{ pagination(listUser, 'smart') }}

You can display limit result per page selector with following code:, (*6)

{{ pagination(listUser, 'select') }}

You can use your own pagination template as well:, (*7)

{{ pagination(listUser, '@AppBundle/pagination/custom.html.twig') }}

Step 6: Integrate rel="canonical" rel="next" and rel="prev" markup

Inside your views :, (*8)

<head>
    {{ pagination(listUser, 'meta') }}
</head>

Step 7: Enjoy

<table class="table">
    <thead>
        <tr>
            <th>Lastname</th>
            <th>Firstname</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>HENRY</td>
            <td>Thibault</td>
        </tr>
        <tr>
            <td>LAZZAROTTO</td>
            <td>Fabrice</td>
        </tr>
        <tr>
            <td>MORIN</td>
            <td>Matthias</td>
        </tr>
        <tr>
            <td>MAHÉ</td>
            <td>Alexandre</td>
        </tr>
        <tr>
            <td>GRÉAUX</td>
            <td>Tony</td>
        </tr>
        <tr>
            <td>CICHOWLAS</td>
            <td>CĂ©dric</td>
        </tr>
    </tbody>
    <tfoot>
        <tr>
            <td colspan="2">
                <ul class="pagination">
                    <li class="page-item">
                        <a href="/app_dev.php/user/?page=1" class="page-link">
                            &lt;&lt;
                        </a>
                    </li>
                    <li class="page-item">
                        <a href="/app_dev.php/user/?page=2" class="page-link">
                            &lt;
                        </a>
                    </li>
                    <li class="page-item hidden-xs">
                        <a href="/app_dev.php/user/?page=1" class="page-link">
                            1
                        </a>
                    </li>
                    <li class="page-item hidden-xs">
                        <a href="/app_dev.php/user/?page=2" class="page-link">
                            2
                        </a>
                    </li>
                    <li class="page-item active">
                        <a href="/app_dev.php/user/?page=3" class="page-link">
                            3
                        </a>
                    </li>
                    <li class="page-item hidden-xs">
                        <a href="/app_dev.php/user/?page=4" class="page-link">
                            4
                        </a>
                    </li>
                    <li class="page-item">
                        <a href="/app_dev.php/user/?page=4" class="page-link">
                            &gt;
                        </a>
                    </li>
                    <li class="page-item">
                        <a href="/app_dev.php/user/?page=4" class="page-link">
                            &gt;&gt;
                        </a>
                    </li>
                </ul>
            </td>
        </tr>
    </tfoot>
</table>

Copyright (c) Thibault Henry, (*9)

License Distributed under the GPLv3.0 license., (*10)

If you like TangoMan Pagination Bundle please star!, (*11)

And follow me on GitHub: TangoMan75 ... And check my other cool projects., (*12)

Matthias Morin | LinkedIn, (*13)

The Versions

08/04 2018

dev-master

9999999-dev

Symfony Pagination Twig Extension Bundle

  Sources   Download

The Requires

 

by Fabrice Lazzarotto
by Matthias Morin

service twig extension bootstrap bundle symfony responsive pagination seo paginator pager tiloweb tangoman

08/04 2018

1.4.2

1.4.2.0

Symfony Pagination Twig Extension Bundle

  Sources   Download

The Requires

 

by Fabrice Lazzarotto
by Matthias Morin

service twig extension bootstrap bundle symfony responsive pagination seo paginator pager tiloweb tangoman

07/09 2017

1.4.1

1.4.1.0

Symfony Pagination Twig Extension Bundle

  Sources   Download

The Requires

 

by Fabrice Lazzarotto
by Matthias Morin

service twig extension bootstrap bundle symfony responsive pagination seo paginator pager tiloweb tangoman

07/09 2017

1.4.0

1.4.0.0

Symfony Pagination Twig Extension Bundle

  Sources   Download

The Requires

 

by Fabrice Lazzarotto
by Matthias Morin

service twig extension bootstrap bundle symfony responsive pagination seo paginator pager tiloweb tangoman

31/08 2017

1.3.4

1.3.4.0

Symfony Pagination Twig Extension Bundle

  Sources   Download

The Requires

 

by Fabrice Lazzarotto
by Matthias Morin

service twig extension bootstrap bundle symfony pagination paginator pager tiloweb tangoman

31/08 2017

1.3.3

1.3.3.0

Symfony Pagination Twig Extension Bundle

  Sources   Download

The Requires

 

by Fabrice Lazzarotto
by Matthias Morin

service twig extension bootstrap bundle symfony pagination paginator pager tiloweb tangoman

30/08 2017

1.3.2

1.3.2.0

Symfony Pagination Twig Extension Bundle

  Sources   Download

The Requires

 

by Fabrice Lazzarotto
by Matthias Morin

service twig extension bootstrap bundle symfony pagination paginator pager tiloweb tangoman

28/08 2017

1.3.1

1.3.1.0

Symfony Pagination Twig Extension Bundle

  Sources   Download

The Requires

 

by Fabrice Lazzarotto
by Matthias Morin

service twig extension bundle symfony pagination paginator pager tiloweb tangoman

25/08 2017

1.2.0

1.2.0.0

Symfony Pagination Twig Extension Bundle

  Sources   Download

The Requires

 

by Fabrice Lazzarotto
by Matthias Morin

service twig extension bundle symfony pagination paginator pager tiloweb tangoman

01/08 2016

1.1.3

1.1.3.0

/

  Sources   Download

The Requires

 

symfony2 bundle pagination paginator pager tiloweb

05/04 2016

1.1.2

1.1.2.0

/

  Sources   Download

The Requires

 

symfony2 bundle pagination paginator pager tiloweb

05/04 2016

1.1.1

1.1.1.0

/

  Sources   Download

The Requires

 

symfony2 bundle pagination paginator pager tiloweb