ttskch/pagination-service-provider
Composer 安装命令:
composer require ttskch/pagination-service-provider
包简介
pagination service provider for the Silex microframework.
README 文档
README
This service provider allows you to use KnpPaginatorBundle in your Silex application.
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:
$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):
$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:
// 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:
- 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.
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:
// 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.
Sort or filter Doctrine\DBALQueryBuilder
When you use Doctrine\DBALQueryBuilder you can sort or filter by SQL clauses like as below:
// 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.
$ git clone git@github.com:ttskch/PaginationServiceProvider.git
$ cd PaginationServiceProvider
$ composer install
$ composer demo
Now you see demo on http://localhost:8888 like below.
Additional features
This service provider also provides bootstrap3-based beautiful pagination and filtration templates. You can use it as below:
$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.
$app['knp_paginator.limits'] = array(10, 25, 50, 100, 200, 500),
You also can define translations for some labels in the messages domain.
$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.
ttskch/pagination-service-provider 适用场景与选型建议
ttskch/pagination-service-provider 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6.24k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2016 年 06 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「silex」 「pager」 「paginator」 「pagination」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ttskch/pagination-service-provider 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ttskch/pagination-service-provider 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ttskch/pagination-service-provider 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Object-oriented CRUD for Doctrine DBAL
Doctrine Collections adapter for Rekapager pagination library
Core functionality of Rekapager pagination library
Common classes for Symfony integration.
Symfony DataGridBundle
Symfony bundle for the Rekapager library
统计信息
- 总下载量: 6.24k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 13
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-06-04
