kosinix/paginator
Composer 安装命令:
composer require kosinix/paginator
包简介
A generic PHP class to split large data into smaller chunks for use in web apps
关键字:
README 文档
README
A generic PHP class to split large data into smaller chunks for use in web apps.
Requirements
- PHP >= 5.3.3
Installation
Manual
- Download the zip file from the Github repository.
- Unpack the zip file and include the files in your project.
- Include the class in /src/:
require_once '/path/to/src/Kosinix/Paginator.php'; // Change this to the correct path
Composer
Inside your project directory, open the command line and type:
composer require kosinix/paginator:dev-master --prefer-dist
Include the autoload.php found in vendor/:
require_once '/path/to/vendor/autoload.php'; // Change this to the correct path
Usage
Include the class and pass the required parameters
require_once 'src/Kosinix/Paginator.php'; $total = 23; // This will come from your app. Eg. do an SQL count: 'SELECT COUNT(*) AS `total` FROM user' $current_page = 2; // This will come from your app. Eg. $current_page = $_GET['page']; $per_page = 10; // This will also come from your app. $paginator = new \Kosinix\Paginator($total, $current_page, $per_page); $sql = sprintf('SELECT * FROM users LIMIT %d,%d', $paginator->getStartIndex(), $paginator->getPerPage()); // Run sql query here
The constructor accepts the following parameters:
- total - The total number of records.
- current_page - The current page to display. Defaults to 1.
- per_page - The number of records in a page. Defaults to 10.
Terms are best explained by this image
Silex Service Provider
You can also create a service provider for paginator for use in Silex:
// PaginatorServiceProvider.php use Silex\Application; use Silex\ServiceProviderInterface; use Kosinix\Paginator; class PaginatorServiceProvider implements ServiceProviderInterface { public function register(Application $app) { $app['paginator.per_page'] = isset($app['paginator.per_page']) ? (int)$app['paginator.per_page'] : 10; $app['paginator'] = $app->protect( function ($total, $page, $per_page=null) use ($app) { if(null === $per_page){ $per_page = $app['paginator.per_page']; } return new Paginator($total, $page, $per_page); } ); } public function boot(Application $app) { } }
Then in your app:
// Paginator $app->register(new PaginatorServiceProvider());
In your controller:
$sql = 'SELECT COUNT(*) AS `total` FROM product'; $count = $app['db']->fetchAssoc($sql); $count = (int) $count['total']; /** @var \Kosinix\Paginator $paginator */ $paginator = $app['paginator']($count, $page); // $page would come from your web app
Test
- Go to the project folder and run phpunit in the command line.
- You need to have phpunit installed globally.
License
- MIT
kosinix/paginator 适用场景与选型建议
kosinix/paginator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 791 次下载、GitHub Stars 达 1, 最近一次更新时间为 2015 年 05 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「paginator」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 kosinix/paginator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 kosinix/paginator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 kosinix/paginator 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Symfony DataGridBundle
MongoDB Adapter to be used with Laminas Paginator
ThinkPHP6分页驱动
Normalize an index range from user input
A paginator bundle for Symfony
A lightweight PHP paginator, for generating pagination controls in the style of Stack Overflow and Flickr. The 'first' and 'last' page links are shown inline as page numbers, and excess page numbers are replaced by ellipses.
统计信息
- 总下载量: 791
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 17
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-05-26
