ericksonreyes / pagination
Simple Pagination Class for PHP 8
v1.0.2
2023-03-12 21:19 UTC
Requires
- php: 8.1.*
Requires (Dev)
- behat/behat: ^3.11
- fakerphp/faker: 1.20.*
- friends-of-phpspec/phpspec-code-coverage: 6.1.*
- mockery/mockery: 1.5.*
- phing/phing: 2.17.*
- php-coveralls/php-coveralls: 2.5.*
- php-parallel-lint/php-console-color: 1.0.*
- php-parallel-lint/php-console-highlighter: 1.0.*
- php-parallel-lint/php-parallel-lint: 1.3.*
- php-parallel-lint/php-var-dump-check: 0.*
- phpmd/phpmd: 2.13.*
- phpspec/phpspec: 7.2.*
- phpunit/phpunit: 9.5.*
- povils/phpmnd: 3.0.*
- sebastian/phpcpd: 6.0.*
- squizlabs/php_codesniffer: 3.7.*
This package is auto-updated.
Last update: 2024-10-21 02:39:48 UTC
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