Showing hundreds of rows on one page is slow. Pagination splits a list into pages.
With a model
Replace findAll() with paginate(10). It reads the ?page= value from the URL and returns only that page's rows.
Pass the pager to the view: 'pager' => $model->pager.
Showing the links
<?= $pager->links() ?>— full links (First, 1 2 3, Last)<?= $pager->simpleLinks() ?>— just Older / Newer
Filters and search
Add conditions before paginate(): $model->like('title', $q)->paginate(10). To keep the search term in page links, use $pager->only(['q'])->links().
Custom look (Bootstrap)
Create a template, for example app/Views/pagers/bootstrap.php, register it in app/Config/Pager.php under $templates, and use it: $pager->links('default', 'bootstrap').
More than one list
Give each list a group name: paginate(10, 'posts') and $pager->links('posts').
Useful pager methods: getCurrentPage(), getPageCount(), getTotal().
Example
<?php
// app/Controllers/Posts.php
public function index()
{
$model = model(PostModel::class);
$q = trim((string) $this->request->getGet('q'));
if ($q !== '') {
$model->like('title', $q);
}
return view('posts/index', [
'posts' => $model->orderBy('id', 'DESC')->paginate(10),
'pager' => $model->pager,
'q' => $q,
]);
}
// app/Views/posts/index.php
// <?php foreach ($posts as $post): ?>
// <h3><?= esc($post['title']) ?></h3>
// <?php endforeach ?>
// <?= $pager->only(['q'])->links() ?>
// <p>Page <?= $pager->getCurrentPage() ?> of <?= $pager->getPageCount() ?></p>
/posts?q=php&page=2 shows the second page of posts with "php" in the title. Page links keep q=php, and the text shows e.g. "Page 2 of 4".