2017 © Pedro Peláez
 

library pagination-service-provider

pagination service provider for the Silex microframework.

image

qckanemoto/pagination-service-provider

pagination service provider for the Silex microframework.

  • Thursday, June 28, 2018
  • by ttskch
  • Repository
  • 3 Watchers
  • 6 Stars
  • 296 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 7 Versions
  • 0 % Grown

The README.md

PaginationServiceProvider

Build Status Latest Stable Version Total Downloads, (*1)

This service provider allows you to use KnpPaginatorBundle in your Silex application., (*2)

Requirements

  • 3.x: PHP 5.5.9+
  • 1.x: PHP 5.3+

Getting Started

For Silex 2.x

$ composer require ttskch/pagination-service-provider

For Silex 1.x

$ composer require ttskch/pagination-service-provider:~1.0

And enable this service provider in your application:, (*3)

$app->register(new Ttskch\Silex\Provider\PaginationServiceProvider());

If you need, you can configure default query parameter names and templates as below (almost same as origin):, (*4)

$app['knp_paginator.options'] = array(
    'default_options' => array(
        'sort_field_name' => 'sort',
        'sort_direction_name' => 'direction',
        'filter_field_name' => 'filterField',
        'filter_value_name' => 'filterValue',
        'page_name' => 'page',
        'distinct' => true,
    ),
    'template' => array(
        'pagination' => '@knp_paginator_bundle/sliding.html.twig',
        'filtration' => '@knp_paginator_bundle/filtration.html.twig',
        'sortable' => '@knp_paginator_bundle/sortable_link.html.twig',
    ),
    'page_range' => 5,
);

Then you can create pagination instance and render it in view:, (*5)

// in your controller.

$pagination = $app['knp_paginator']->paginate($someData);

return $app['twig']->render('index.html.twig', array(
    'pagination' => $pagination,
));
{# in your twig template #}

{{ knp_pagination_render(pagination) }}

Usage

KnpPaginatorBundle can paginate many things. But in Silex application we may use for:, (*6)

  • Array
  • Doctrine\DBALQueryBuilder

However KnpPaginatorBundle doesn't sort or filter these data automatically via request query parameter. If you want to sort or filter these data you should do by hand., (*7)

Sort or filter array

When you want to sort or filter simple two-dimensional array, you can use Cake\Utility\Hash (autoloaded) like as below:, (*8)

// in your controller.

$array = /* some two dimensional array */;

$sort = $request->get('sort');
$direction = $request->get('direction', 'asc');
$filterField = $request->get('filterField');
$filterValue = $request->get('filterValue');

$array = Hash::extract($array, "{n}[{$filterField}=/{$filterValue}/]");
$array = Hash::sort($array, "{n}.{$sort}", $direction);

$pagination = $app['knp_paginator']->paginate($array); // You can get filtered and sorted pagination object.

See the official document for more information of usage of Hash class., (*9)

Sort or filter Doctrine\DBALQueryBuilder

When you use Doctrine\DBALQueryBuilder you can sort or filter by SQL clauses like as below:, (*10)

// in your controller.

$sort = $request->get('sort');
$direction = $request->get('direction', 'asc') === 'asc' ? 'asc' : 'desc';
$filterField = $request->get('filterField');
$filterValue = $request->get('filterValue');

$qb = $app['db']->createQueryBuilder()
    ->select('t.*')
    ->from('table', 't')
    ->where("{$app['db']->quoteIdentifier($filterField)} like {$app['db']->quote('%' . $filterValue . '%')}")
    ->orderBy($app['db']->quoteIdentifier($sort), $direction)
;

$pagination = $app['knp_paginator']->paginate($qb);

Demo

You can see demo code here. You also can run demo easily on your local by following command., (*11)

$ git clone git@github.com:ttskch/PaginationServiceProvider.git
$ cd PaginationServiceProvider
$ composer install
$ composer demo

Now you see demo on http://localhost:8888 like below., (*12)

image, (*13)

Additional features

This service provider also provides bootstrap3-based beautiful pagination and filtration templates. You can use it as below:, (*14)

$app['knp_paginator.options'] = array(
    'template' => array(
        'pagination' => '@ttskch_silex_pagination/pagination-bootstrap3.html.twig',
        'filtration' => '@ttskch_silex_pagination/filtration-bootstrap3.html.twig',
    ),
);

When you use the pagination-bootstrap3.html.twig template, you can configure the list of Items per page selector., (*15)

$app['knp_paginator.limits'] = array(10, 25, 50, 100, 200, 500),

You also can define translations for some labels in the messages domain., (*16)

$app['translator.domains'] = array(
    'messages' => array(
        'ja' => array(
            'Previous' => '前へ',
            'Next' => '次へ',
        ),
    ),
);
$app['translator.domains'] = array(
    'messages' => array(
        'ja' => array(
            'Items per page' => '1ページの件数',
            'Filter' => '絞り込み',
        ),
    ),
);

Note

This service provider depends on TwigServiceProvider and TranslationServiceProvider. Please register them before register PaginationServiceProvider., (*17)

The Versions