diff --git a/system/Model.php b/system/Model.php index cc6bee16dc78..5d8aa86298ee 100644 --- a/system/Model.php +++ b/system/Model.php @@ -1141,10 +1141,16 @@ public function chunk(int $size, Closure $userFunc) * * @return array|null */ - public function paginate(int $perPage = null, string $group = 'default', int $page = 0, int $segment = 0) + public function paginate(int $perPage = null, string $group = 'default', int $page = null, int $segment = 0) { $pager = \Config\Services::pager(null, null, false); - $page = $page >= 1 ? $page : $pager->getCurrentPage($group); + + if ($segment) + { + $pager->setSegment($segment); + } + + $page = $page >= 1 ? $page : $pager->getCurrentPage($group); $total = $this->countAllResults(false); diff --git a/system/Pager/Pager.php b/system/Pager/Pager.php index 143505496942..d34fd3d08518 100644 --- a/system/Pager/Pager.php +++ b/system/Pager/Pager.php @@ -202,7 +202,10 @@ protected function displayLinks(string $group, string $template): string */ public function store(string $group, int $page, int $perPage = null, int $total, int $segment = 0) { - $this->segment[$group] = $segment; + if ($segment) + { + $this->setSegment($segment, $group); + } $this->ensureGroup($group, $perPage); @@ -223,6 +226,23 @@ public function store(string $group, int $page, int $perPage = null, int $total, //-------------------------------------------------------------------- + /** + * Sets segment for a group. + * + * @param integer $number + * @param string $group + * + * @return mixed + */ + public function setSegment(int $number, string $group = 'default') + { + $this->segment[$group] = $number; + + return $this; + } + + //-------------------------------------------------------------------- + /** * Sets the path that an aliased group of links will use. * @@ -269,7 +289,7 @@ public function getCurrentPage(string $group = 'default'): int { $this->ensureGroup($group); - return $this->groups[$group]['currentPage']; + return $this->groups[$group]['currentPage'] ?: 1; } //-------------------------------------------------------------------- diff --git a/user_guide_src/source/libraries/pagination.rst b/user_guide_src/source/libraries/pagination.rst index 452c22c13499..d984be66cf5b 100644 --- a/user_guide_src/source/libraries/pagination.rst +++ b/user_guide_src/source/libraries/pagination.rst @@ -112,7 +112,7 @@ Specifying the URI Segment for Page It is also possible to use a URI segment for the page number, instead of the page query parameter. Simply specify the segment number to use as the fourth argument. URIs generated by the pager would then look -like *https://domain.tld/model/[pageNumber]* instead of *https://domain.tld/model?page=[pageNumber]*.:: +like *https://domain.tld/model/[pageNumber]* instead of *https://domain.tld/model?page=[pageNumber]*. ::