Welcome Guest, Not a member yet? Register   Sign In
How to change first pagination link URL in codeigniter 4
#1

I want to make pagination in CodeIgniter 4 like the below:
PHP Code:
http://localhost:8080/admin/all-clients
http://localhost:8080/admin/all-clients/page/2
http://localhost:8080/admin/all-clients/page/3 

So here is my method:

PHP Code:
public function index($page 1){

    
    
/**
    * page redirect to http://localhost:8080/admin/all-clients
    * if the user visits to http://localhost:8080/admin/all-clients/page/1
    */
    $segment null != $this->request->getUri()->getSegment(3);
    if($segment == 'page'){
        if ( $this->request->getUri()->getSegment(4) == 1) {
            return redirect()->tobase_url('admin/all-clients') );
        }
    }
    

    $perPage 
10;
    $offset = ($page 1);
    $total $this->clientsModel->countAll();


    /**
    * get all clients
    */
    $clients $this->clientsModel
            
->select('clients.*, subject.subject_text')
            ->join('subject''clients.client_subject = subject.id')
            ->paginate($perPage''$offset 1);
    
    
/**
    * get pager links
    */
    $pager $this->clientsModel->pager;

    $pager->setPath('admin/all-clients/page''client-group');
    $links $pager->makeLinks($page$perPage$total'bs_custom'4'client-group');
    

    
echo view('admin/allClients', [
        'page_title' => 'All Clients',
        'clients' => $clients,
        'pager_links' => $links,
    ]);


here is my route:
PHP Code:
$routes->get('all-clients''ClientsController::index');
$routes->get('all-clients/page/(:num)''ClientsController::index/$1'); 

In pagination links, the first page URL is http://localhost:8080/admin/all-clients/page/1. But I want http://localhost:8080/admin/all-clients/ and the rest of the pagination links should be http://localhost:8080/admin/all-clients/page/2, http://localhost:8080/admin/all-clients/page/3 ....

So, my question is how to do that.
Reply
#2

Customize the Pager class.
See https://codeigniter4.github.io/CodeIgnit...re-classes
Reply




Theme © iAndrew 2016 - Forum software by © MyBB