class Pagination

Holds pagination state and builds page URLs. Rendering is delegated to a PaginationRenderer so the same object can drive Bootstrap, Tailwind, a custom layout, or a React view (via toArray()).

Properties

protected int $currentPage
protected int $limit
protected int $totalItems
protected array $query

Query params preserved in every page link (search, filters, sort).

protected string $path

Base path for links. Empty string = relative to the current URL.

protected string $pageName

Name of the page query parameter.

protected int $window

Number of page links shown on each side of the current page.

static protected PaginationRenderer|null $defaultRenderer

Renderer used by render() when none is passed.

Methods

__construct(int $currentPage = 1, int $limit = 10, int $totalItems = 0)

Constructor for Pagination class

static Pagination
fromRequest(object $request, int $limit, int $totalItems)

Convenience factory that reads the page number from the request.

static int
currentPage(object $request, string $pageName = 'page')

Retrieves and validates the current page from the GET request.

withFilter(SearchFilter $filter)

Preserve the filter's active query params (search term, filters, sort) in every generated page link.

withQuery(array $query)

Preserve arbitrary query params in every generated page link.

withPath(string $path)

Set a base path for links (e.g. '/admindashboard').

withPageName(string $pageName)

Change the page query parameter name (useful for two lists on one page).

withWindow(int $window)

Number of page links on each side of the current page.

static void
setDefaultRenderer(PaginationRenderer $renderer)

Set the renderer used when render() is called without one.

int
currentPageNumber()

No description

int
limit()

No description

int
totalItems()

No description

string
pageName()

No description

int
offset()

Calculates offset needed for pagination.

int
totalPages()

Returns total number of pages we need to handle.

bool
hasPages()

True when there is more than one page (renderers output nothing otherwise).

bool
onFirstPage()

No description

bool
onLastPage()

No description

int
firstItem()

1-based index of the first item on this page (0 when empty).

int
lastItem()

1-based index of the last item on this page (0 when empty).

string
url(int $page)

Builds the URL for a page, keeping search/filter/sort params intact.

string|null
previousPageUrl()

No description

string|null
nextPageUrl()

No description

array
pageRange()

Sliding window of page numbers. null marks a gap (render as an ellipsis).

array
queryParams(SearchFilter $filter)

Builds find() params from a SearchFilter plus limit/offset.

array
paginationParams(mixed $conditions = '', array $bind = [], string $order = '')

Builds parameters for pagination query (original API, kept for existing callers).

string
render(PaginationRenderer|null $renderer = null)

Renders the controls with the given renderer, or the default one.

array
toArray()

Serializable state for React views or JSON APIs.

static string
pagination(int $current_page, int $total_pages, string $base_url = '?page=') deprecated

Renders Bootstrap 5 pagination controls (original static API).

string
paginationSummary(string $noun)

Renders pagination summary.

Details

at line 40
__construct(int $currentPage = 1, int $limit = 10, int $totalItems = 0)

Constructor for Pagination class

Parameters

int $currentPage

The current page that is selected.

int $limit

The maximum number of items for a page.

int $totalItems

The total number of items for a list.

at line 55
static Pagination fromRequest(object $request, int $limit, int $totalItems)

Convenience factory that reads the page number from the request.

Parameters

object $request

The request object.

int $limit

The maximum number of items for a page.

int $totalItems

The total number of items for a list.

Return Value

Pagination

at line 66
static int currentPage(object $request, string $pageName = 'page')

Retrieves and validates the current page from the GET request.

Parameters

object $request

The request object.

string $pageName

Name of the page query parameter.

Return Value

int

The current page number (1 if missing or invalid).

at line 83
Pagination withFilter(SearchFilter $filter)

Preserve the filter's active query params (search term, filters, sort) in every generated page link.

Parameters

SearchFilter $filter

Return Value

Pagination

at line 88
Pagination withQuery(array $query)

Preserve arbitrary query params in every generated page link.

Parameters

array $query

Return Value

Pagination

at line 95
Pagination withPath(string $path)

Set a base path for links (e.g. '/admindashboard').

Parameters

string $path

Return Value

Pagination

at line 101
Pagination withPageName(string $pageName)

Change the page query parameter name (useful for two lists on one page).

Parameters

string $pageName

Return Value

Pagination

at line 107
Pagination withWindow(int $window)

Number of page links on each side of the current page.

Parameters

int $window

Return Value

Pagination

at line 113
static void setDefaultRenderer(PaginationRenderer $renderer)

Set the renderer used when render() is called without one.

Parameters

PaginationRenderer $renderer

Return Value

void

at line 121
int currentPageNumber()

No description

Return Value

int

at line 122
int limit()

No description

Return Value

int

at line 123
int totalItems()

No description

Return Value

int

at line 124
string pageName()

No description

Return Value

string

at line 131
int offset()

Calculates offset needed for pagination.

Return Value

int

The offset

at line 140
int totalPages()

Returns total number of pages we need to handle.

Return Value

int

The total number of pages to be managed by pagination.

at line 145
bool hasPages()

True when there is more than one page (renderers output nothing otherwise).

Return Value

bool

at line 149
bool onFirstPage()

No description

Return Value

bool

at line 150
bool onLastPage()

No description

Return Value

bool

at line 153
int firstItem()

1-based index of the first item on this page (0 when empty).

Return Value

int

at line 158
int lastItem()

1-based index of the last item on this page (0 when empty).

Return Value

int

at line 172
string url(int $page)

Builds the URL for a page, keeping search/filter/sort params intact.

Parameters

int $page

The page number.

Return Value

string

The URL (not HTML-escaped; renderers escape it).

at line 177
string|null previousPageUrl()

No description

Return Value

string|null

at line 181
string|null nextPageUrl()

No description

Return Value

string|null

at line 191
array pageRange()

Sliding window of page numbers. null marks a gap (render as an ellipsis).

Example for page 10 of 20, window 2: [1, null, 8, 9, 10, 11, 12, null, 20]

Return Value

array

at line 219
array queryParams(SearchFilter $filter)

Builds find() params from a SearchFilter plus limit/offset.

Parameters

SearchFilter $filter

The filter built from the request.

Return Value

array

Params ready for Model::find().

at line 235
array paginationParams(mixed $conditions = '', array $bind = [], string $order = '')

Builds parameters for pagination query (original API, kept for existing callers).

Parameters

mixed $conditions

The conditions for the query.

array $bind

What we want to bind.

string $order

The order we want the results returned.

Return Value

array

An array of params tailored for pagination.

at line 253
string render(PaginationRenderer|null $renderer = null)

Renders the controls with the given renderer, or the default one.

Parameters

PaginationRenderer|null $renderer

Return Value

string

at line 261
array toArray()

Serializable state for React views or JSON APIs.

Return Value

array

at line 285
static string pagination(int $current_page, int $total_pages, string $base_url = '?page=') deprecated

deprecated Use $pagination->render() instead. A custom $base_url is no longer honored; use withPath()/withQuery().

Renders Bootstrap 5 pagination controls (original static API).

Parameters

int $current_page
int $total_pages
string $base_url

Return Value

string

at line 295
string paginationSummary(string $noun)

Renders pagination summary.

Parameters

string $noun

The phrase that describes the dataset.

Return Value

string

Contents of the pagination summary.