sebastiansulinski/php-paginator 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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\Request or, 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
  • 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\Collection or Illuminate\Support\Collection

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 我们能提供哪些服务?
定制开发 / 二次开发

基于 sebastiansulinski/php-paginator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 21.02k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 9
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-10-12