Pagination
API reference › yii › data › Pagination
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 paginationecho LinkPager::widget([ 'pagination' => $pages,]);For more details and usage information on Pagination, see the guide article on pagination.
Properties
Section titled “Properties”| Property | Type | Description | Defined by |
|---|---|---|---|
$defaultPageSize | int | The default page size. Default: 20. | Pagination |
$forcePageParam | bool | Whether to always have the page parameter in the URL created by createUrl(). Default: true. | Pagination |
$limit read-only | int | The limit of the data. | Pagination |
$links read-only | array | The links for navigational purpose. | Pagination |
$offset read-only | int | The offset of the data. | Pagination |
$page | int | The zero-based current page number. | Pagination |
$pageCount read-only | int | Number of pages. | Pagination |
$pageParam | string | Name of the parameter storing the current page index. Default: 'page'. | Pagination |
$pageSize | int | The number of items per page. | Pagination |
$pageSizeLimit | array| | The page size limits. Default: [1,. | Pagination |
$pageSizeParam | string | Name of the parameter storing the page size. Default: 'per-page'. | Pagination |
$params | array| | Parameters (name => value) that should be used to obtain the current page number and to create new pagination URLs. Default: null. | Pagination |
$route | string| | The route of the controller action for displaying the paged contents. Default: null. | Pagination |
$totalCount | int | Total number of items. Default: 0. | Pagination |
$urlManager | yii\| | The URL manager used for creating pagination URLs. Default: null. | Pagination |
$validatePage | bool | Whether to check if $page is within valid range. Default: true. | Pagination |
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
__construct() | Constructor. | yii\ |
__call() | Calls the named method which is not a class method. | yii\ |
__get() | Returns the value of an object property. | yii\ |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\ |
__set() | Sets value of an object property. | yii\ |
__unset() | Sets an object property to null. | yii\ |
canGetProperty() | Returns a value indicating whether a property can be read. | yii\ |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\ |
className() static | Returns the fully qualified name of this class. | yii\ |
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\ |
hasProperty() | Returns a value indicating whether a property is defined. | yii\ |
init() | Initializes the object. | yii\ |
setPage() | Sets the current page number. | Pagination |
setPageSize() | Pagination | |
getQueryParam() protected | Returns the value of the specified query parameter. | Pagination |
resolveRoute() protected | Resolves the target route from $route, the active controller, or Application::$requestedRoute. | Pagination |
Constants
Section titled “Constants”| Constant | Value | Description | Defined by |
|---|---|---|---|
LINK_NEXT | 'next' | Pagination | |
LINK_PREV | 'prev' | Pagination | |
LINK_FIRST | 'first' | Pagination | |
LINK_LAST | 'last' | Pagination |
Property details
Section titled “Property details”$defaultPageSize
Section titled “$defaultPageSize ”public int $defaultPageSize = 20The default page size.
This property will be returned by $pageSize when page size
cannot be determined by $pageSizeParam from $params.
$forcePageParam
Section titled “$forcePageParam ”public bool $forcePageParam = trueWhether 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.
$limit
Section titled “$limit ”public int $limitThe 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).
$links
Section titled “$links ”public array $linksThe 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).
$offset
Section titled “$offset ”public int $offsetThe 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).
$pageSize
Section titled “$pageSize ”public int $pageSizeThe 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.
$pageSizeLimit
Section titled “$pageSizeLimit ”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.
$params
Section titled “$params ”public array|null $params = nullParameters (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).
$route
Section titled “$route ”public string|null $route = nullThe route of the controller action for displaying the paged contents.
If not set, it means using the currently requested route.
$urlManager
Section titled “$urlManager ”public yii\web\UrlManager|null $urlManager = nullThe URL manager used for creating pagination URLs.
If not set, the “urlManager” application component will be used.
$validatePage
Section titled “$validatePage ”public bool $validatePage = trueWhether 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.
Method details
Section titled “Method details”createUrl()
Section titled “createUrl() ”public function createUrl(int $page, int|null $pageSize = null, bool $absolute = false): stringCreates 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
| Parameter | Type | Description |
|---|---|---|
$page | int | The zero-based page number that the URL should point to. |
$pageSize | int| | The number of items on each page. If not set, the value of $pageSize will be used. |
$absolute | bool | Whether to create an absolute URL. Defaults to false. |
Returns
string: The created URL
Throws
yii\base\InvalidConfigException: ifresolveRoute()cannot determine a route.
See also
getLimit()
Section titled “getLimit() ”public function getLimit(): intReturns
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.
getLinks()
Section titled “getLinks() ”public function getLinks(bool $absolute = false): arrayReturns a whole set of links for navigating to the first, last, next and previous pages.
Parameters
| Parameter | Type | Description |
|---|---|---|
$absolute | bool | Whether 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.
getOffset()
Section titled “getOffset() ”public function getOffset(): intReturns
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.
getPage()
Section titled “getPage() ”public function getPage(bool $recalculate = false): intReturns the zero-based current page number.
Parameters
| Parameter | Type | Description |
|---|---|---|
$recalculate | bool | Whether to recalculate the current page based on the page size and item count. |
Returns
int: The zero-based current page number.
getPageCount()
Section titled “getPageCount() ”public function getPageCount(): intReturns
int: Number of pages
getPageSize()
Section titled “getPageSize() ”public function getPageSize(): intReturns 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
setPage()
Section titled “setPage() ”public function setPage(int $value, bool $validatePage = false): mixedSets the current page number.
Parameters
| Parameter | Type | Description |
|---|---|---|
$value | int | The zero-based index of the current page. |
$validatePage | bool | Whether to validate the page number. Note that in order to validate the page number, both $validatePage and this parameter must be true. |
Returns
mixed
setPageSize()
Section titled “setPageSize() ”public function setPageSize(int $value, bool $validatePageSize = false): mixedParameters
| Parameter | Type | Description |
|---|---|---|
$value | int | The number of items per page. |
$validatePageSize | bool | Whether to validate page size. |
Returns
mixed
getQueryParam()
Section titled “getQueryParam() ”protected function getQueryParam(string $name, string|null $defaultValue = null): string|nullReturns 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
| Parameter | Type | Description |
|---|---|---|
$name | string | The parameter name |
$defaultValue | string| | The value to be returned when the specified parameter does not exist in $params. |
Returns
string|null: The parameter value
resolveRoute()
Section titled “resolveRoute() ”protected function resolveRoute(): stringResolves 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
yii\base\InvalidConfigException: if no route can be resolved.