diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 8dc16b6761..796f0cb347 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -145,6 +145,7 @@ Yii Framework 2 Change Log - Enh: Added `yii\web\Response::clearOutputBuffers()` (qiangxue) - Enh: Improved `QueryBuilder::buildLimit()` to support big numbers (qiangxue) - Enh: Added support for building SQLs with sub-queries (qiangxue) +- Enh: Added `Pagination::getLinks()` (qiangxue) - Chg #1186: Changed `Sort` to use comma to separate multiple sort fields and use negative sign to indicate descending sort (qiangxue) - Chg #1519: `yii\web\User::loginRequired()` now returns the `Response` object instead of exiting the application (qiangxue) - Chg #1586: `QueryBuilder::buildLikeCondition()` will now escape special characters and use percentage characters by default (qiangxue) diff --git a/framework/data/Pagination.php b/framework/data/Pagination.php index 9e9359c455..c390f50888 100644 --- a/framework/data/Pagination.php +++ b/framework/data/Pagination.php @@ -67,6 +67,12 @@ use yii\web\Request; */ class Pagination extends Object { + const LINK_SELF = 'self'; + const LINK_NEXT = 'next'; + const LINK_PREV = 'prev'; + const LINK_FIRST = 'first'; + const LINK_LAST = 'last'; + /** * @var string name of the parameter storing the current page index. Defaults to 'page'. * @see params @@ -217,4 +223,28 @@ class Pagination extends Object { return $this->pageSize < 1 ? -1 : $this->pageSize; } + + /** + * Returns a whole set of links for navigating to the first, last, next and previous pages. + * @param boolean $absolute whether the generated URLs should be absolute. + * @return 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 getLinks($absolute = false) + { + $currentPage = $this->getPage(); + $pageCount = $this->getPageCount(); + $links = [ + self::LINK_SELF => $this->createUrl($currentPage, $absolute), + ]; + if ($currentPage > 0) { + $links[self::LINK_FIRST] = $this->createUrl(0, $absolute); + $links[self::LINK_PREV] = $this->createUrl($currentPage - 1, $absolute); + } + if ($currentPage < $pageCount - 1) { + $links[self::LINK_NEXT] = $this->createUrl($currentPage + 1, $absolute); + $links[self::LINK_LAST] = $this->createUrl($pageCount - 1, $absolute); + } + return $links; + } }