2017 © Pedro PelĂĄez
 

symfony-bundle pagination-bundle

/

image

tiloweb/pagination-bundle

/

  • Saturday, December 9, 2017
  • by Tiloweb
  • Repository
  • 2 Watchers
  • 6 Stars
  • 3,738 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 11 Versions
  • 7 % Grown

The README.md

Usage

This Bundle made easy to use the Doctrine\Paginator method to optimally paginate your requests., (*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 tiloweb/pagination-bundle "dev-master"

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 Tiloweb\PaginationBundle\TilowebPaginationBundle(),
        );

        // ...
    }

    // ...
}

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, $max = 10)
    {
        $dql = $this->createQueryBuilder('user');
        $dql->orderBy('user.lastname', 'DESC');

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

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

        $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
        ));
    }
}

Note the $request->query->getInt('page', 1), you can choose the name of the $_GET parameter, but it will be page by default., (*5)

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, 'page') }}
            </td>
        </tr>
    </tfoot>
</table>

Use the function {{ pagination(Paginator, get) }} to render the pagination. the paginator parameter is your Paginator object, and the get parameter (values page by default) is the name of the $_GET parameter you want your pagination to listen., (*6)

Step 6: 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>HOUDAYER</td>
            <td>Gaël</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 start">
                        <a href="/app_dev.php/user/?page=1" class="page-link disabled">
                            &lt;&lt;
                        </a>
                    </li>
                    <li class="page-item prev">
                        <a href="/app_dev.php/user/?page=2" class="page-link disabled" rel="prev">
                            &lt;
                        </a>
                    </li>
                    <li class="page-item">
                        <a href="/app_dev.php/user/?page=1" class="page-link">
                            1
                        </a>
                    </li>
                    <li class="page-item">
                        <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">
                        <a href="/app_dev.php/user/?page=4" class="page-link">
                            4
                        </a>
                    </li>
                    <li class="page-item next">
                        <a href="/app_dev.php/user/?page=4" class="page-link" rel="next">
                            &gt;
                        </a>
                    </li>
                    <li class="page-item end">
                        <a href="/app_dev.php/user/?page=4" class="page-link">
                            &gt;&gt;
                        </a>
                    </li>
                </ul>
            </td>
        </tr>
    </tfoot>
</table>

Change the pagination template

You can configure your own twig pagination template by calling it in your config.yml :, (*7)

tiloweb_pagination:
      template: 'Your/File.html.twig'

The Versions

09/12 2017

dev-master

9999999-dev

/

  Sources   Download

The Requires

 

symfony2 bundle pagination paginator pager tiloweb

09/12 2017

1.4.2

1.4.2.0

/

  Sources   Download

The Requires

 

symfony2 bundle pagination paginator pager tiloweb

09/12 2017

1.4.1

1.4.1.0

/

  Sources   Download

The Requires

 

symfony2 bundle pagination paginator pager tiloweb

24/10 2017

1.4

1.4.0.0

/

  Sources   Download

The Requires

 

symfony2 bundle pagination paginator pager tiloweb

24/10 2017

dev-Tilotiti-control

dev-Tilotiti-control

/

  Sources   Download

The Requires

 

symfony2 bundle pagination paginator pager tiloweb

24/10 2017

1.3

1.3.0.0

/

  Sources   Download

The Requires

 

symfony2 bundle pagination paginator pager tiloweb

14/09 2017

1.2

1.2.0.0

/

  Sources   Download

The Requires

 

symfony2 bundle pagination paginator pager tiloweb

14/09 2017

dev-customized-$_GET

dev-customized-$_GET

/

  Sources   Download

The Requires

 

symfony2 bundle pagination paginator pager tiloweb

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