CodeIgniter Forums
Pagination Library with URI segment - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Feature Requests (https://forum.codeigniter.com/forumdisplay.php?fid=29)
+--- Thread: Pagination Library with URI segment (/showthread.php?tid=74218)



Pagination Library with URI segment - paul - 08-28-2019

Hello
I want use the Pagination Library with URI segment instead of the page query parameter
www.mysite.com/controller/function/1      instead of
www.mysite.com/controller/function?page=1

it seems to be possible with CI4
Quote: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 fifth parameter to
Code:
makeLinks()
. URIs generated by the pager would then look like https://domain.tld/model/[pageNumber] instead of https://domain.tld/model?page=[pageNumber].:


this option does not seem to be possible in 4.0.0-beta.4

i think that the paginate function is not good in model.php

Code:
public function paginate(int $perPage = 20, string $group = 'default', int $page = 0)
    {
        // Get the necessary parts.
        $page = $page >= 1 ? $page : (ctype_digit($_GET['page'] ?? '') && $_GET['page'] > 1 ? $_GET['page'] : 1);

        $total = $this->countAllResults(false);

        // Store it in the Pager library so it can be
        // paginated in the views.
        $pager       = \Config\Services::pager();
        $this->pager = $pager->store($group, $page, $perPage, $total);

        $offset = ($page - 1) * $perPage;

        return $this->findAll($perPage, $offset);
    }


I tried this paginate()
Code:
/**
* if 0 pager like ?page=x
* if >=1 pagerSegment is the URI segment number
*
* @var int
*/
protected $pagerSegment = 0;

public function paginate(int $perPage = 20, string $group = 'default', int $page = 0)
    {
                if($this->pagerSegment >= 1)
                {
                    $uri= Services::request()->uri;
                    $page = (ctype_digit($uri->getSegment($this->pagerSegment) ?? '') && $uri->getSegment($this->pagerSegment) > 1 ? $uri->getSegment($this->pagerSegment) : 1);
                }else
                {
                    // Get the necessary parts.
                    $page = $page >= 1 ? $page : (ctype_digit($_GET['page'] ?? '') && $_GET['page'] > 1 ? $_GET['page'] : 1);
                }
        

        $total = $this->countAllResults(false);

        // Store it in the Pager library so it can be
        // paginated in the views.
        $pager       = \Config\Services::pager();
        $this->pager = $pager->store($group, $page, $perPage, $total,$this->pagerSegment);

        $offset = ($page - 1) * $perPage;

        return $this->findAll($perPage, $offset);
    }

It works

maybe the paginate function should be updated to work with URI