laminas/laminas-paginator
Laminas Paginator provides flexible pagination for PHP apps, with adapters for arrays, iterators, and database results. Generate page ranges and navigation data, and integrate with Laminas MVC or use standalone for paged listings.
The paginator can be used stand-alone, outside a Mezzio or laminas-mvc application.
The example uses the following directory structure:
example-app/
|-- public/
| `-- index.php
`-- vendor
`-- …
Create a paginator and a related adapter,
set the item count for one page and the current page number in public/index.php:
// Create paginator
$paginator = new Laminas\Paginator\Paginator(
new Laminas\Paginator\Adapter\ArrayAdapter($albums)
);
// Configure paginator
$paginator->setItemCountPerPage(4);
$paginator->setCurrentPageNumber((int) ($_GET['page'] ?? 1));
$albums = [
[
'artist' => 'David Bowie',
'title' => 'The Next Day (Deluxe Version)',
],
[
'artist' => 'Bastille',
'title' => 'Bad Blood',
],
[
'artist' => 'Bruno Mars',
'title' => 'Unorthodox Jukebox',
],
[
'artist' => 'Emeli Sandé',
'title' => 'Our Version of Events (Special Edition)',
],
[
'artist' => 'Bon Jovi',
'title' => 'What About Now (Deluxe Version)',
],
[
'artist' => 'Justin Timberlake',
'title' => 'The 20/20 Experience (Deluxe Version)',
],
[
'artist' => 'Bastille',
'title' => 'Bad Blood (The Extended Cut)',
],
[
'artist' => 'P!nk',
'title' => 'The Truth About Love',
],
[
'artist' => 'Sound City - Real to Reel',
'title' => 'Sound City - Real to Reel',
],
[
'artist' => 'Jake Bugg',
'title' => 'Jake Bugg',
],
];
The data of each sub-array is returned by iteration over the paginator:
foreach ($paginator as $item) {
var_dump($item['artist']); // "Bon Jovi", "Justin Timberlake", …
var_dump($item['title']); // "What About Now (Deluxe Version)", "The 20/20 Experience (Deluxe Version)", …
}
Retrieving the current status data of the paginator:
var_dump($paginator->getPages()->previous); // 1
var_dump($paginator->getPages()->next); // 3
How can I help you explore Laravel packages today?