To implement pagination on an overview page, you must calculate the current page offset and limit, validate that the requested page exists, and fetch the corresponding subset of records from the model.
Key pagination parameters include:
url: The base URL used to generate pagination links (appends the ?page= variable).limit: The maximum number of items per page (typically sourced from a backend setting).num_items: The total count of visible/published items.num_pages: The total number of pages, calculated as ceil(num_items / limit).requested_page: The page number requested by the user via the URL.offset: The starting item index for the database query, calculated as (requested_page * limit) - limit.
use Frontend\Core\Engine\Navigation as FrontendNavigation;
use Frontend\Core\Engine\Model as FrontendModel;
use Frontend\Modules\MiniBlog\Engine\Model as FrontendMiniBlogModel;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
// 1. Initialize pagination data
protected $pagination = array(
'limit' => 10,
'offset' => 0,
'requested_page' => 1,
'num_items' => null,
'num_pages' => null
);
// 2. Calculate parameters
$requestedPage = $this->url->getParameter('page', 'int', 1);
$this->pagination['url'] = FrontendNavigation::getUrlForBlock('mini_blog');
$this->pagination['limit'] = FrontendModel::getModuleSetting('mini_blog', 'overview_num_items', 10);
$this->pagination['num_items'] = FrontendMiniBlogModel::getAllCount();
$this->pagination['num_pages'] = (int) ceil($this->pagination['num_items'] / $this->pagination['limit']);
$this->pagination['requested_page'] = $requestedPage;
$this->pagination['offset'] = ($this->pagination['requested_page'] * $this->pagination['limit']) - $this->pagination['limit'];
// 3. Validate page existence
if($this->pagination['num_pages'] == 0) $this->pagination['num_pages'] = 1;
if($requestedPage > $this->pagination['num_pages'] || $requestedPage < 1) {
throw new NotFoundHttpException();
}
// 4. Fetch the paginated records
$this->records = FrontendMiniBlogModel::getAll($this->pagination['limit'], $this->pagination['offset']);