ericksonreyes/pagination
最新稳定版本:v1.0.2
Composer 安装命令:
composer require ericksonreyes/pagination
包简介
Simple Pagination Class for PHP 8
README 文档
README
Nothing fancy. I just created a pagination class that I've been copy-pasting over and over again.
Installation
composer require ericksonreyes/pagination
Example (Laravel)
Controller
namespace App\Http\Controllers; use App\Repository\UserRepository; use Illuminate\Http\Request; use Illuminate\Http\Response; use Illuminate\Routing\Controller as BaseController; class Users extends BaseController { private const DEFAULT_PAGE_SIZE = 35; public function index(Request $request, UserRepository $repository): Response { $page = (int) $request->get('page', 1); $size = (int) $request->get('size', self::DEFAULT_PAGE_SIZE); if ($page < 1) { $page = 1; } if ($size < 1) { $size = self::DEFAULT_PAGE_SIZE; } $offset = $page - 1; $limit = $size; $count = $repository->countUsers(); $data['users'] = $repository->getUsers($offset, $limit); $data['pagination'] = new Pagination( recordsFound: $count, recordsPerPage: 10, currentPage: $page ); return response()->view('list', $data); } }
View (Blade Templating)
@if(isset($pagination) && $pagination->hasPages()) <ul class="pagination"> @if($pagination->hasPreviousPage()) <li> <a href="{{ route('records.list', ['page' => $pagination->previousPage()]) }}"> Previous </a> </li> @endif @if($pagination->hasFirstPage()) <li> <a href="{{ route('records.list', ['page' => $pagination->firstPage()]) }}"> {{ $pagination->firstPage() }} </a> </li> <li>...</li> @endif @foreach($pagination->pages() as $page) @if($pagination->currentPage() === $page) <li><span class="span--strong">{{ $page }}</span></li> @else <li> <a href="{{ route('records.list', ['page' => $page]) }}"> {{ $page }} </a> </li> @endif @endforeach @if($pagination->hasLastPage()) <li>...</li> <li> <a href="{{ route('records.list', ['page' => $pagination->lastPage()]) }}"> {{ $pagination->lastPage() }} </a> </li> @endif @if($pagination->hasNextPage()) <li> <a href="{{ route('records.list', ['page' => $pagination->hasNextPage()]) }}"> Next </a> </li> @endif </ul> @endif
View (Vanilla PHP)
<?php if(isset($pagination) && $pagination->hasPages()) { ?><ul class="pagination"><?php if($pagination->hasPreviousPage()) { ?> <li> <a href="<?php echo route('records.list', ['page' => $pagination->previousPage()]) ?>"> Previous </a> </li> <?php } if ($pagination->hasFirstPage()) { ?> <li> <a href="<?php echo route('records.list', ['page' => $pagination->firstPage()]) ?>"> <?php echo $pagination->firstPage() ?> </a> </li> <li>...</li> <?php } foreach($pagination->pages() as $page) { if($pagination->currentPage() === $page) { ?> <li> <span class="span--strong"><?php echo $page; ?></span> </li> <?php } else { ?> <li> <a href="<?php echo route('records.list', ['page' => $page]) ?>"> <?php echo $page ?> </a> </li> <?php } } if($pagination->hasLastPage()) { ?> <li>...</li> <li> <a href="<?php echo route('records.list', ['page' => $pagination->lastPage()]) ?>"> <?php echo $pagination->lastPage() ?> </a> </li> <?php } if($pagination->hasNextPage()) { ?> <li> <a href="<?php echo route('records.list', ['page' => $pagination->hasNextPage()]) ?>"> Next </a> </li> <?php } ?></ul><?php } ?>
Author
License
See LICENSE
Gitlab
This project is also available in GitLab
统计信息
- 总下载量: 4
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-12-19