Skip to content
PHP Ecosystem
Explore

Pagination

API reference › yii › data › Pagination

classyii\data\Pagination
Extendsyii\base\BaseObject
Implementsyii\web\Linkable, yii\base\Configurable
Since2.0
Sourceframework/data/Pagination.php

Pagination represents information relevant to pagination of data items.

When data needs to be rendered in multiple pages, Pagination can be used to represent information such as total item count, page size, current page, etc. These information can be passed to pagers to render pagination buttons or links.

The following example shows how to create a pagination object and feed it to a pager.

Controller action:

public function actionIndex()
{
$query = Article::find()->where(['status' => 1]);
$countQuery = clone $query;
$pages = new Pagination(['totalCount' => $countQuery->count()]);
$models = $query->offset($pages->offset)
->limit($pages->limit)
->all();
return $this->render('index', [
'models' => $models,
'pages' => $pages,
]);
}

View:

foreach ($models as $model) {
// display $model here
}
// display pagination
echo LinkPager::widget([
'pagination' => $pages,
]);

For more details and usage information on Pagination, see the guide article on pagination.

PropertyTypeDescriptionDefined by
$defaultPageSizeintThe default page size. Default: 20.Pagination
$forcePageParamboolWhether to always have the page parameter in the URL created by createUrl(). Default: true.Pagination
$limit read-onlyintThe limit of the data.Pagination
$links read-onlyarrayThe links for navigational purpose.Pagination
$offset read-onlyintThe offset of the data.Pagination
$pageintThe zero-based current page number.Pagination
$pageCount read-onlyintNumber of pages.Pagination
$pageParamstringName of the parameter storing the current page index. Default: 'page'.Pagination
$pageSizeintThe number of items per page.Pagination
$pageSizeLimitarray|falseThe page size limits. Default: [1, 50].Pagination
$pageSizeParamstringName of the parameter storing the page size. Default: 'per-page'.Pagination
$paramsarray|nullParameters (name => value) that should be used to obtain the current page number and to create new pagination URLs. Default: null.Pagination
$routestring|nullThe route of the controller action for displaying the paged contents. Default: null.Pagination
$totalCountintTotal number of items. Default: 0.Pagination
$urlManageryii\web\UrlManager|nullThe URL manager used for creating pagination URLs. Default: null.Pagination
$validatePageboolWhether to check if $page is within valid range. Default: true.Pagination
MethodDescriptionDefined by
__construct()Constructor.yii\base\BaseObject
__call()Calls the named method which is not a class method.yii\base\BaseObject
__get()Returns the value of an object property.yii\base\BaseObject
__isset()Checks if a property is set, i.e. defined and not null.yii\base\BaseObject
__set()Sets value of an object property.yii\base\BaseObject
__unset()Sets an object property to null.yii\base\BaseObject
canGetProperty()Returns a value indicating whether a property can be read.yii\base\BaseObject
canSetProperty()Returns a value indicating whether a property can be set.yii\base\BaseObject
className() staticReturns the fully qualified name of this class.yii\base\BaseObject
createUrl()Creates the URL suitable for pagination with the specified page number.Pagination
getLimit()Pagination
getLinks()Returns a whole set of links for navigating to the first, last, next and previous pages.Pagination
getOffset()Pagination
getPage()Returns the zero-based current page number.Pagination
getPageCount()Pagination
getPageSize()Returns the number of items per page.Pagination
hasMethod()Returns a value indicating whether a method is defined.yii\base\BaseObject
hasProperty()Returns a value indicating whether a property is defined.yii\base\BaseObject
init()Initializes the object.yii\base\BaseObject
setPage()Sets the current page number.Pagination
setPageSize()Pagination
getQueryParam() protectedReturns the value of the specified query parameter.Pagination
resolveRoute() protectedResolves the target route from $route, the active controller, or Application::$requestedRoute.Pagination
ConstantValueDescriptionDefined by
LINK_NEXT'next'Pagination
LINK_PREV'prev'Pagination
LINK_FIRST'first'Pagination
LINK_LAST'last'Pagination
public int $defaultPageSize = 20

The default page size.

This property will be returned by $pageSize when page size cannot be determined by $pageSizeParam from $params.

public bool $forcePageParam = true

Whether to always have the page parameter in the URL created by createUrl().

If false and $page is 0, the page parameter will not be put in the URL.

public int $limit

The limit of the data.

This may be used to set the LIMIT value for a SQL statement for fetching the current page of data. Note that if the page size is infinite, a value -1 will be returned.

Access

Read-only (virtual property).

public array $links

The links for navigational purpose.

The array keys specify the purpose of the links (e.g. LINK_FIRST), and the array values are the corresponding URLs.

Access

Read-only (virtual property).

public int $offset

The offset of the data.

This may be used to set the OFFSET value for a SQL statement for fetching the current page of data.

Access

Read-only (virtual property).

public int $pageSize

The number of items per page.

If it is less than 1, it means the page size is infinite, and thus a single page contains all items.

public array|false $pageSizeLimit = [1, 50]

The page size limits.

The first array element defines the minimum page size, and the second the maximum page size. If this is false, it means $pageSize should always return the value of $defaultPageSize.

public array|null $params = null

Parameters (name => value) that should be used to obtain the current page number and to create new pagination URLs.

If not set, all parameters from $_GET will be used instead.

In order to add hash to all links use array_merge($_GET, ['#' => 'my-hash']).

The array element indexed by $pageParam is considered to be the current page number (defaults to 0); while the element indexed by $pageSizeParam is treated as the page size (defaults to $defaultPageSize).

public string|null $route = null

The route of the controller action for displaying the paged contents.

If not set, it means using the currently requested route.

public yii\web\UrlManager|null $urlManager = null

The URL manager used for creating pagination URLs.

If not set, the “urlManager” application component will be used.

public bool $validatePage = true

Whether to check if $page is within valid range.

When this property is true, the value of $page will always be between 0 and ($pageCount-1). Because $pageCount relies on the correct value of $totalCount which may not be available in some cases (e.g. MongoDB), you may want to set this property to be false to disable the page number validation. By doing so, $page will return the value indexed by $pageParam in $params.

public function createUrl(int $page, int|null $pageSize = null, bool $absolute = false): string

Creates the URL suitable for pagination with the specified page number.

This method is mainly called by pagers when creating URLs used to perform pagination.

The target route is determined by resolveRoute() when $route is not configured.

Parameters

ParameterTypeDescription
$pageintThe zero-based page number that the URL should point to.
$pageSizeint|nullThe number of items on each page. If not set, the value of $pageSize will be used.
$absoluteboolWhether to create an absolute URL. Defaults to false.

Returns

string: The created URL

Throws

See also

public function getLimit(): int

Returns

int: The limit of the data. This may be used to set the LIMIT value for a SQL statement for fetching the current page of data. Note that if the page size is infinite, a value -1 will be returned.

public function getLinks(bool $absolute = false): array

Returns a whole set of links for navigating to the first, last, next and previous pages.

Parameters

ParameterTypeDescription
$absoluteboolWhether the generated URLs should be absolute.

Returns

array: The links for navigational purpose. The array keys specify the purpose of the links (e.g. LINK_FIRST), and the array values are the corresponding URLs.

public function getOffset(): int

Returns

int: The offset of the data. This may be used to set the OFFSET value for a SQL statement for fetching the current page of data.

public function getPage(bool $recalculate = false): int

Returns the zero-based current page number.

Parameters

ParameterTypeDescription
$recalculateboolWhether to recalculate the current page based on the page size and item count.

Returns

int: The zero-based current page number.

public function getPageCount(): int

Returns

int: Number of pages

public function getPageSize(): int

Returns the number of items per page.

By default, this method will try to determine the page size by $pageSizeParam in $params. If the page size cannot be determined this way, $defaultPageSize will be returned.

Returns

int: The number of items per page. If it is less than 1, it means the page size is infinite, and thus a single page contains all items.

See also

public function setPage(int $value, bool $validatePage = false): mixed

Sets the current page number.

Parameters

ParameterTypeDescription
$valueintThe zero-based index of the current page.
$validatePageboolWhether to validate the page number. Note that in order to validate the page number, both $validatePage and this parameter must be true.

Returns

mixed

public function setPageSize(int $value, bool $validatePageSize = false): mixed

Parameters

ParameterTypeDescription
$valueintThe number of items per page.
$validatePageSizeboolWhether to validate page size.

Returns

mixed

protected function getQueryParam(string $name, string|null $defaultValue = null): string|null

Returns the value of the specified query parameter.

This method returns the named parameter value from $params. Null is returned if the value does not exist.

Parameters

ParameterTypeDescription
$namestringThe parameter name
$defaultValuestring|nullThe value to be returned when the specified parameter does not exist in $params.

Returns

string|null: The parameter value

protected function resolveRoute(): string

Resolves the target route from $route, the active controller, or Application::$requestedRoute.

The requestedRoute fallback supports standalone actions where no controller is hosting the request.

Returns

string: The resolved route.

Throws