sebastiansulinski/php-paginator
Composer 安装命令:
composer require sebastiansulinski/php-paginator
包简介
Framework agnostic PHP Pagination component
README 文档
README
Light weight, easy drop-in pagination component for PHP 8 applications.
Installation
composer require sebastiansulinski/php-paginator
Structure
The component consist of 2 main classes:
-
Pagination- class, to which you pass- instance of a
SSD\Paginator\Requestor, if your project already makes use of\Illuminate\Http\Request, you can pass instance of it instead. - total number of records
- number of records per page
- string key representing the query string parameter associated with the current page
- instance of a
-
Paginator- a parent class for any implementations that return the html structure of a pagination. Its constructor takes 2 arguments:- instance of
Pagination - records for a given page as instance of
SSD\Paginator\CollectionorIlluminate\Support\Collection
- instance of
Package comes with one implementation of Paginator:
VueSelectPaginator
The VueSelectPaginator returns the following structure when render() method is called on its instance (all entities are decoded for clarity):
<ssd-paginator
:options="{
"1":"http://paginator.app",
"2":"http://paginator.app/?page=2",
"3":"http://paginator.app/?page=3",
"4":"http://paginator.app/?page=4",
"5":"http://paginator.app/?page=5"
}"
current="http://paginator.app/?page=2"
previous="http://paginator.app"
next="http://paginator.app/?page=3"
first="http://paginator.app"
last="http://paginator.app/?page=5"
:number-of-pages="5"
></ssd-paginator>
And to support this implementation, there is a VueJs component that ships with this package - you'll find it under resources/src/js/components/SsdPaginator:
import { createApp } from 'vue' import SsdPaginator from './components/SsdPaginator' createApp({ components: { SsdPaginator }, }).mount('#app')
To create your own implementations of Paginator all you have to do is to provide implementation of the html() method, which should return the html structure of your pagination layout.
Styling
SsdPaginator comes pre-formatted using tailwindcss v3, but you can replace its structure using the available slot and apply your own styling as required.
Usage
// import all dependencies use SSD\Paginator\Request; use SSD\Paginator\Collection; use SSD\Paginator\Pagination; use SSD\Paginator\VueSelectPaginator; // instantiate Pagination class $pagination = new Pagination( Request::capture(), 160, 10, 'page' ); // get your records as array and pass through to the Collection // in this example I just use array of numbers and get only a chunk // of records based on offset and limit, but you'd probably use // some active model to get only the records you're after $records = range(1, 160); $records = new Collection($records); $chunk = $records->splice( $pagination->offset(), $pagination->limit() ); // instantiate SelectPaginator with instance of Pagination and collection of records $paginator = new VueSelectPaginator($pagination, $chunk);
Displaying records and pagination
// loop through records using Collection::map() and implode() methods echo $paginator->records()->map(function($record) { // ... })->implode(''); // or using standard foreach loop foreach($paginator->records() as $record) { // ... } // display pagination echo $paginator->render();
Custom pagination structure
If you don't want to use Paginator class implementation, the Pagination class has all necessary methods to allow you put together pagination structure directly in your view, for instance to display list of all pages as clickable numbers with current page highlighted using class="active", you could do something like:
$pagination = new Pagination( Request::capture(), 160, 10, 'page' ); echo '<ul>'; echo $pagination->urlList()->map(function(string $url, int $page) use($pagination) { $link = '<li><a href="'.$url.'"'; $link .= $pagination->current() === $page ? ' class="active"' : null; $link .= '>'.$page.'</a></li>'; return $link; })->implode(''); echo '</ul>';
sebastiansulinski/php-paginator 适用场景与选型建议
sebastiansulinski/php-paginator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 21.02k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2017 年 10 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 sebastiansulinski/php-paginator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 sebastiansulinski/php-paginator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 21.02k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 9
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-10-12