PHP Classes

OKA Symfony Pagination Twig: Bundle for pagination support using Twig templates

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 34 All time: 11,063 This week: 206Up
Version License PHP version Categories
oka-pagination 2.7.0MIT/X Consortium ...5.3HTML, PHP 5, Libraries
Description 

Author

This package is a bundle for pagination support using Twig templates.

Applications should register the bundle to use its pagination features.

A class should be provided to implement an action to list the data to be displayed with pagination controls on a Web page.

The pagination output is defined using Twig templates. Several other aspects of the way pagination is generated can be configured with a separate file.

Picture of Cedrick Oka
  Performance   Level  

 

Documentation

Getting Started With OkaPaginationBundle

This bundle provides a flexible pagination system.

Prerequisites

The OkaPaginationBundle has the following requirements: - PHP 5.5 - Symfony 2.7+ - Twig Extension

Installation

Installation is a quick (I promise!) 4 step process:

  1. Download OkaPaginationBundle
  2. Enable the Bundle
  3. Configure the OkaPaginationBundle
  4. Use bundle and enjoy!

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:

$ composer require coka/pagination-bundle

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

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:

<?php
// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...
            
            new Oka\PaginationBundle\OkaPaginationBundle(),
        );
        
        // ...
    }

    // ...
}

Step 3: Configure the Bundle

Add the following configuration to your config.yml.

# app/config/config.yml
oka_pagination:
    db_driver: orm
    model_manager_name: default
    item_per_page: 10                                           # Global number of items to show by page
    max_page_number: 4000                                       # Global number max of page to show
    template: OkaPaginationBundle:Pagination:paginate.html.twig # Global twig template used for shown pagination menu
    request:
        query_map:
            page: page                                          # Global page query parameter name
            item_per_page: item_per_page                        # Global number of items by page query parameter name
            sort: sort                                          # Global sort field query parameter name
            desc: desc                                          # Global sort direction query parameter name
        sort:
            delimiter: ','                                      # Global sort query delimiter value
            attributes_availables: ['name']                     # Global sort query value availables attributes
    twig:
        enable_extension: true
        enable_global: false
    pagination_managers:
        foo:
            db_driver: orm
            model_manager_name: default
            class: Acme\DemoBundle\Entity\Foo
            item_per_page: 10
            max_page_number: 4000
            template: OkaPaginationBundle:Pagination:paginate.html.twig
            request:
                query_map:
                    page: page
                    item_per_page: item_per_page
                    sort: sort
                    desc: desc
                    filters:
                        name:
                            type: string
                        enabled:
                            field: enabled         # Not required if the filter name is equal to the field name
                            type: boolean          # The type in which the value of the filter will be casted
                sort:
                    delimiter: ','
                    attributes_availables: ['name', 'createdAt']

Step 4: Use the bundle is simple

The goal of this bundle is to paginate some Entity (ORM) or Document (MongoDB) class. You can use it in two ways.

  1. Use default pagination manager.
  2. Use custom pagination manager.

In Controllers

Initialize pagination

// Acme\DemoBundle\Controller\FooController.php

public function listAction(Request $request)
{
    / @var \Oka\PaginationBundle\Service\PaginationManager $pm */
    $pm = $this->get('oka_pagination.manager');
    
    // Use default pagination manager
    / @var \Oka\PaginationBundle\Util\PaginationResultSet $page */
    $page = $pm->paginate(Foo::class, $request, [], ['name' => 'ASC']);
    
    // Or use custom pagination manager
    // / @var \Oka\PaginationBundle\Util\PaginationResultSet $page */
    // $page = $pm->paginate('foo', $request, [], ['name' => 'ASC']);
    
    // parameters to template
    return $this->render('AcmeDemoBundle:Foo:list.html.twig', ['page' => $page]);
}

In Views (Twig)

{# total items count #}
<div class="count">
    {{ page.getFullyItems() }}
</div>

<table>
{# table body #}
{% for item in page.items %}
<tr {% if loop.index is odd %}class="color"{% endif %}>
    <td>{{ item.id }}</td>
    <td>{{ item.title }}</td>
</tr>
{% endfor %}
</table>

{# display navigation #}
<div class="navigation">
    {# Use the current pagination manager #}
    {{ paginate('foo_path' , {'query': 'query'}) }}
    
    {# Or use a specific pagination manager #}
    {# {{ paginate_foo('foo_path' , {'query': 'query'}) }} #}
</div>

Advanced Usage

// Acme\DemoBundle\Controller\FooController.php

public function listAction(Request $request)
{
    / @var \Oka\PaginationBundle\Service\PaginationManager $pm */
    $pm = $this->get('oka_pagination.manager');
    
    // Use default pagination manager
    / @var \Oka\PaginationBundle\Util\PaginationResultSet $page */
    $pm->createQuery('foo', $request, [], ['name' => 'ASC'])
       ->setCountItemsCallable(function(EntityRepository $er, array $criteria){
           // Here your code to return the number of elements
           // ...
       })
       ->setSelectItemsCallable(function(EntityRepository $er, array $criteria, array $orderBy, $limit, $offset){
           // Here your code to return the elements list
           // ...
       });
    
    / @var \Oka\PaginationBundle\Util\PaginationResultSet $page */
    $page = $pm->fetch();
    
    // parameters to template
    return $this->render('AcmeDemoBundle:Foo:list.html.twig', ['page' => $page]);
}

Details

OkaPaginationBundle

This bundle provides a flexible pagination system.

Latest Stable Version Total Downloads Latest Unstable Version License Monthly Downloads Daily Downloads SensioLabsInsight

Latest updates

For notes about latest changes please read CHANGELOG, for required changes in your code please read UPGRADE chapter of documentation.

Documentation

Read the Resources/doc/index.md.

Installation

All the installation instructions are located in the documentation.

Original Credits

License

This bundle is under the MIT license. See the complete license in the bundle.


  Files folder image Files (38)  
File Role Description
Files folder imagesrc (1 file, 8 directories)
Accessible without login Plain text file CHANGELOG.md Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:34
This week:0
All time:11,063
This week:206Up