2017 © Pedro Peláez
 

symfony-bundle knp-paginator-bundle

image

tmsolution/knp-paginator-bundle

  • Thursday, April 5, 2018
  • by JacekLozinski
  • Repository
  • 2 Watchers
  • 0 Stars
  • 35 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 272 Forks
  • 0 Open issues
  • 22 Versions
  • 13 % Grown

The README.md

Intro to KnpPaginatorBundle

SEO friendly Symfony2 paginator to paginate everything, (*1)

Build Status, (*2)

Generally this bundle is based on Knp Pager component. This component introduces a different way for pagination handling. You can read more about the internal logic on the given documentation link., (*3)

knpbundles.com, (*4)

Note: Keep knp-components in sync with this bundle. If you want to use older version of KnpPaginatorBundle - use v1.0 tag in the repository which is suitable to paginate ODM mongodb and ORM 2.0 queries, (*5)

Latest updates

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

Requirements:

  • Knp pager component >=1.1
  • KnpPaginatorBundle's master compatible with symfony (>=2.0 versions).
  • Twig>=1.5 version is required if you use twig templating engine

Features:

  • Does not require initializing specific adapters
  • Can be customized in any way needed, etc.: pagination view, event subscribers.
  • Possibility to add custom filtering, sorting functionality depending on request parameters.
  • Separation of concerns, paginator is responsible for generating the pagination view only, pagination view - for representation purposes.

Note: using multiple paginators requires setting the alias in order to keep non conflicting parameters. Also it gets quite complicated with a twig template, since hash arrays cannot use variables as keys., (*7)

More detailed documentation:

Installation and configuration:

Pretty simple with Composer, run:, (*8)

composer require knplabs/knp-paginator-bundle

Add PaginatorBundle to your application kernel

// app/AppKernel.php
public function registerBundles()
{
    return array(
        // ...
        new Knp\Bundle\PaginatorBundle\KnpPaginatorBundle(),
        // ...
    );
}

, (*9)

Configuration example

You can configure default query parameter names and templates, (*10)

knp_paginator:
    page_range: 5                      # default page range used in pagination control
    default_options:
        page_name: page                # page query parameter name
        sort_field_name: sort          # sort field query parameter name
        sort_direction_name: direction # sort direction query parameter name
        distinct: true                 # ensure distinct results, useful when ORM queries are using GROUP BY statements
    template:
        pagination: 'KnpPaginatorBundle:Pagination:sliding.html.twig'     # sliding pagination controls template
        sortable: 'KnpPaginatorBundle:Pagination:sortable_link.html.twig' # sort link template

There are a few additional pagination templates, that could be used out of the box in knp_paginator.template.pagination key:, (*11)

  • KnpPaginatorBundle:Pagination:sliding.html.twig (by default)
  • KnpPaginatorBundle:Pagination:twitter_bootstrap_v3_pagination.html.twig
  • KnpPaginatorBundle:Pagination:twitter_bootstrap_pagination.html.twig
  • KnpPaginatorBundle:Pagination:foundation_v5_pagination.html.twig

Usage examples:

Controller

Currently paginator can paginate:, (*12)

  • array
  • Doctrine\ORM\Query
  • Doctrine\ORM\QueryBuilder
  • Doctrine\ODM\MongoDB\Query\Query
  • Doctrine\ODM\MongoDB\Query\Builder
  • Doctrine\ODM\PHPCR\Query\Query
  • Doctrine\ODM\PHPCR\Query\Builder\QueryBuilder
  • Doctrine\Common\Collection\ArrayCollection - any doctrine relation collection including
  • ModelCriteria - Propel ORM query
  • array with Solarium_Client and Solarium_Query_Select as elements
// Acme\MainBundle\Controller\ArticleController.php

public function listAction(Request $request)
{
    $em    = $this->get('doctrine.orm.entity_manager');
    $dql   = "SELECT a FROM AcmeMainBundle:Article a";
    $query = $em->createQuery($dql);

    $paginator  = $this->get('knp_paginator');
    $pagination = $paginator->paginate(
        $query, /* query NOT result */
        $request->query->getInt('page', 1)/*page number*/,
        10/*limit per page*/
    );

    // parameters to template
    return $this->render('AcmeMainBundle:Article:list.html.twig', array('pagination' => $pagination));
}

View

{# total items count #}


{{ pagination.getTotalItemCount }}
{# sorting of properties based on query components #} {# table body #} {% for article in pagination %} {% endfor %}
{{ knp_pagination_sortable(pagination, 'Id', 'a.id') }} {{ knp_pagination_sortable(pagination, 'Title', 'a.title') }} {{ knp_pagination_sortable(pagination, 'Release', ['a.date', 'a.time']) }}
{{ article.id }} {{ article.title }} {{ article.date | date('Y-m-d') }}, {{ article.time | date('H:i:s') }}
{# display navigation #}

Translation in view

For translating the following text: * %foo% name with translation key table_header_name. The translation is in the domain messages. * {0} No author|{1} Author|[2,Inf] Authors with translation key table_header_author. The translation is in the domain messages., (*13)

translationCount and translationParameters can be combined., (*14)



{# sorting of properties based on query components #}
{{ knp_pagination_sortable(pagination, 'Id'|trans({foo:'bar'},'messages'), 'a.id' )|raw }} {{ knp_pagination_sortable(pagination, 'Title', 'a.title')|raw }} {{ knp_pagination_sortable(pagination, 'Author'|trans({}, 'messages'), 'a.author' )|raw }}

Dependency Injection

You can automatically inject a paginator service into another service by using the knp_paginator.injectable DIC tag. The tag takes one optional argument paginator, which is the ID of the paginator service that should be injected. It defaults to knp_paginator., (*15)

The class that receives the KnpPaginator service must implement Knp\Bundle\PaginatorBundle\Definition\PaginatorAwareInterface. If you're too lazy you can also just extend the Knp\Bundle\PaginatorBundle\Definition\PaginatorAware base class., (*16)

XML configuration example


<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

    <parameters>
        <parameter key="my_bundle.paginator_aware.class">MyBundle\Repository\PaginatorAwareRepository</parameter>
    </parameters>

    <services>
        <service id="my_bundle.paginator_aware" class="my_bundle.paginator_aware.class">
            <tag name="knp_paginator.injectable" paginator="knp_paginator" />
        </service>
    </services>
</container>

The Versions

05/04 2018

v1.0

1.0.0.0

  Sources   Download

10/08 2017

dev-master

9999999-dev http://github.com/KnpLabs/KnpPaginatorBundle

Paginator bundle for Symfony2 to automate pagination and simplify sorting and other features

  Sources   Download

MIT

The Requires

 

The Development Requires

symfony2 bundle pagination knplabs paginator pager knp

10/08 2017

v2.6.1

2.6.1.0 http://github.com/KnpLabs/KnpPaginatorBundle

Paginator bundle for Symfony2 to automate pagination and simplify sorting and other features

  Sources   Download

MIT

The Requires

 

The Development Requires

symfony2 bundle pagination knplabs paginator pager knp

09/06 2017

v2.6.0

2.6.0.0 http://github.com/KnpLabs/KnpPaginatorBundle

Paginator bundle for Symfony2 to automate pagination and simplify sorting and other features

  Sources   Download

MIT

The Requires

 

The Development Requires

symfony2 bundle pagination knplabs paginator pager knp

21/03 2017

2.5.4

2.5.4.0 http://github.com/KnpLabs/KnpPaginatorBundle

Paginator bundle for Symfony2 to automate pagination and simplify sorting and other features

  Sources   Download

MIT

The Requires

 

The Development Requires

symfony2 bundle pagination knplabs paginator pager knp

15/08 2016

2.5.6

2.5.6.0 http://github.com/KnpLabs/KnpPaginatorBundle

Paginator bundle for Symfony2 to automate pagination and simplify sorting and other features

  Sources   Download

MIT

The Requires

 

The Development Requires

symfony2 bundle pagination knplabs paginator pager knp

12/08 2016

2.5.5

2.5.5.0 http://github.com/KnpLabs/KnpPaginatorBundle

Paginator bundle for Symfony2 to automate pagination and simplify sorting and other features

  Sources   Download

MIT

The Requires

 

The Development Requires

symfony2 bundle pagination knplabs paginator pager knp

20/04 2016

2.5.3

2.5.3.0 http://github.com/KnpLabs/KnpPaginatorBundle

Paginator bundle for Symfony2 to automate pagination and simplify sorting and other features

  Sources   Download

MIT

The Requires

 

The Development Requires

symfony2 bundle pagination knplabs paginator pager knp

20/04 2016

2.5.2

2.5.2.0 http://github.com/KnpLabs/KnpPaginatorBundle

Paginator bundle for Symfony2 to automate pagination and simplify sorting and other features

  Sources   Download

MIT

The Requires

 

The Development Requires

symfony2 bundle pagination knplabs paginator pager knp

23/11 2015

2.5.1

2.5.1.0 http://github.com/KnpLabs/KnpPaginatorBundle

Paginator bundle for Symfony2 to automate pagination and simplify sorting and other features

  Sources   Download

MIT

The Requires

 

The Development Requires

symfony2 bundle pagination knplabs paginator pager knp

07/09 2015

2.5.0

2.5.0.0 http://github.com/KnpLabs/KnpPaginatorBundle

Paginator bundle for Symfony2 to automate pagination and simplify sorting and other features

  Sources   Download

MIT

The Requires

 

The Development Requires

symfony2 bundle pagination knplabs paginator pager knp

20/05 2015

2.4.2

2.4.2.0 http://github.com/KnpLabs/KnpPaginatorBundle

Paginator bundle for Symfony2 to automate pagination and simplify sorting and other features

  Sources   Download

MIT

The Requires

 

The Development Requires

symfony2 bundle pagination knplabs paginator pager knp

15/09 2014

2.4.1

2.4.1.0 http://github.com/KnpLabs/KnpPaginatorBundle

Paginator bundle for Symfony2 to automate pagination and simplify sorting and other features

  Sources   Download

MIT

The Requires

 

symfony2 bundle pagination knplabs paginator pager knp

23/08 2014

dev-fix/tests

dev-fix/tests http://github.com/KnpLabs/KnpPaginatorBundle

Paginator bundle for Symfony2 to automate pagination and simplify sorting and other features

  Sources   Download

MIT

The Requires

 

symfony2 bundle pagination knplabs paginator pager knp

21/01 2014

2.4.0

2.4.0.0 http://github.com/KnpLabs/KnpPaginatorBundle

Paginator bundle for Symfony2 to automate pagination and simplify sorting and other features

  Sources   Download

MIT

The Requires

 

symfony2 bundle pagination knplabs paginator pager knp

30/07 2013

2.3.3

2.3.3.0 http://github.com/KnpLabs/KnpPaginatorBundle

Paginator bundle for Symfony2 to automate pagination and simplify sorting and other features

  Sources   Download

MIT

The Requires

 

symfony2 bundle pagination knplabs paginator pager knp

18/02 2013

v2.3.2

2.3.2.0 http://github.com/KnpLabs/KnpPaginatorBundle

Paginator bundle for Symfony2 to automate pagination and simplify sorting and other features

  Sources   Download

MIT

The Requires

 

symfony2 bundle pagination knplabs paginator pager knp

06/02 2013

v2.3.1

2.3.1.0 http://github.com/KnpLabs/KnpPaginatorBundle

Paginator bundle for Symfony2 to automate pagination and simplify sorting and other features

  Sources   Download

MIT

The Requires

 

symfony2 bundle pagination knplabs paginator pager knp

18/01 2013

v2.3

2.3.0.0 http://github.com/KnpLabs/KnpPaginatorBundle

Paginator bundle for Symfony2 to automate pagination and simplify sorting and other features

  Sources   Download

MIT

The Requires

 

symfony2 bundle pagination knplabs paginator pager knp

23/03 2012

v2.2

2.2.0.0 http://github.com/KnpLabs/KnpPaginatorBundle

Paginator bundle for Symfony2 to automate pagination and simplify sorting and other features

  Sources   Download

MIT

The Requires

 

symfony2 bundle pagination knplabs paginator pager knp

23/03 2012

v2.1

2.1.0.0 http://github.com/KnpLabs/KnpPaginatorBundle

Paginator bundle for Symfony2 to automate pagination and simplify sorting and other features

  Sources   Download

MIT

The Requires

 

symfony2 bundle pagination knplabs paginator pager knp

18/01 2012

v2.0

2.0.0.0 http://github.com/KnpLabs/KnpPaginatorBundle

Paginator bundle for Symfony2 to automate pagination and simplify sorting and other features

  Sources   Download

MIT

The Requires

 

symfony2 bundle pagination knplabs paginator pager knp