Pagination-URI in Codeigniter 4

I began writing a webapp in Codigniter 4 and currently, i'm stuck with the pagination.

I created a controller, a model an a view to retrieve database entries für usergroups and used CI's built-in pagination-library.

UsergroupsModel:

<?php namespace App\Models;

use CodeIgniter\Model;

class UsergroupsModel extends Model{

    protected $table = 'roles';
    protected $allowedFields = [];
    protected $beforeInsert = ['beforeInsert'];
    protected $beforeUpdate = ['beforeUpdate'];
    

    public function getGroups(){
        $db = \Config\Database::connect();

        $builder = $db->table('roles');
        $query   = $builder->get();

        $results = $query->getResultArray();
        return $results;
    }

}

Controller (Usergroups):

<?php namespace App\Controllers;
use App\Models\UsergroupsModel;

class Usergroups extends BaseController
{
    public function index()
    {
        //Helper laden
        helper(['form','template','userrights']);
        $data = [];
        $data['template'] = get_template();
        $data['info'] = [
            "active" => "menu_dash",
            "title" => "Dashboard",
            "icon" => "fab fa-fort-awesome fa-lg",
                        "sub" => "Frontend",
        ];
        
        //Check Permissions
        $data['userrights'] = get_userrights(session()->get('id'));
        if($data['userrights'][1] == 1)
        {
            foreach($data['userrights'] as $key => $value){
            $data['userrights'][$key] =  '1';
            }
        }
        else
        {
            $data['userrights'] = $data['userrights'];
        }

        $model = new UsergroupsModel;
        $model->getGroups();
        $pager = \Config\Services::pager();

        $data['usergroups'] = $model->paginate(5); 
        $data['pager'] = $model->pager;

        //Create Views
        echo view($data['template'].'/templates/header', $data);
        echo view($data['template'].'/backend/navigation');
        echo view($data['template'].'/templates/sidebar');
        echo view($data['template'].'/backend/usergroups');
        echo view($data['template'].'/templates/footer');    
    }

       
    //--------------------------------------------------------------------

}

In the view, i got my pagination by using

<?= $pager->links() ?>

The default pagination works fine, but i get an URI like https://DOMAIN.DE/usergroups?page=2

In the official Codeigniter 4 docs for the pagination, you can find the following:

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].:

::

$users = $userModel->paginate(10, ‘group1’, null, 3); Please note: $segment value cannot be greater than the number of URI segments plus 1.

So in my controller i changed

$data['usergroups'] = $model->paginate(5);

to

$data['usergroups'] = $model->paginate(5,'test',0,2);

and in the view i added 'test' as a parameter.

<?= $pager->links('test') ?>

In the Routes i added

$routes->get('usergroups/(:num)', 'Usergroups::index/$1');

and in the Controller i changed the index-function to

public function index($segment = null)

The URIs generated from the pagination now look like this:

https://DOMAIN.DE/usergroups/2

but it does not change anything in the entries and the pagination itself alway sticks to page 1.

I think, i can not use CI's built in library when switching to segment-URIs and thus i need to create a manual pagination.

Can somebody help me to fix this problem?

Upvotes: 1

Views: 3063

Answers (2)

Sebastian Osses
Sebastian Osses

Reputation: 11

I currently had the same problem using segments in my pagination.

After much research I discovered that when you use segments associated with a group name in your case "test" you must assign the segment to the variable $ pager

$pager->setSegment(2, 'nameOfGroup');

You only need to change the first parameter with the segment number that you are using and the second with the name of the group that you are assigning.

Upvotes: 1

It seems like there's a bug in Version 4.0.3. so it can't work out of the box. But i found a way to solve it:

The index-function needs to look like this:

public function index($page = 1)

and

within the Controller, the $data['usergroups'] need to look like this:

$data['usergroups'] = $model->paginate(5, 'test', $page, 2);

with 2 being the segment of the page-number in the URI.

Works like a charm.

Upvotes: 0

Related Questions