Welcome Guest, Not a member yet? Register   Sign In
How to extend PagerRenderer and add additional methods
#1

Hello, I'm trying to extend pagination of codeigniter4, so I can be able to use it with ajax.
If I add directly my custom method to CodeIgniter\Pager\PagerRenderer, they works fine. But I want to use them through Libraries directory.
So I can be able to use like this
PHP Code:
<?php 
namespace App\Libraries\Pagination;
use 
CodeIgniter\Pager\PagerRenderer;

class 
Pagination extends PagerRenderer
{
 
   public function __construct()
   {
       // parent::__construct();
   }
    
    
// Get First Page Number 
    public function getFirstPageCounter(): int
    
{
        if ($this->segment === 0){
            $firstPage 1;
        }else{
            $firstPage $this->segment;
        }
    
        
return (int) $firstPage;
    }
    
    
// get Last page Number
    public function getLastPageCounter()
    {
        if ($this->segment === 0){
            $last $this->pageCount;
        }else{
            $last $this->segment;
        }
        return (int)$last;
    }
    
    
// Get previous Page number
    public function getPreviousPageCounter()
    {
        if (! $this->hasPrevious()){
            return null;
        }
    
        
if ($this->segment === 0){
            $previous $this->first 1;
        }else{
            $previous $this->segment;
        }
    
        
return (int) $previous;
    }
    
    
// get Next Page Number
    public function getNextPageCounter()
    {
        if (! $this->hasNext()){
            return null;
        }
    
        
if ($this->segment === 0){
            $next $this->last 1;
        }else{
            $next $this->last 1;
        }
        return (int) $next;
    }
}

//nav templates links 

//html navigation template
PHP Code:
<?php if ($pager->hasNext()) : ?>
<li>
<a href="<?= $pager->getNext()?>" data-page="<?= $pager->getNextPageCounter() ?>">
</a>
</li>
<li>
<a href="<?= $pager->getLast()?>  data-page="<?= $pager->getPreviousPageCounter() ?>">
</a>
</li>
<?php endif ?>

Thanks in advance.
Reply
#2

It seems there is no way to replace the PagerRenderer class.
It is hard coded:
https://github.com/codeigniter4/CodeIgni...r.php#L123
Reply
#3

(This post was last modified: 09-22-2021, 01:07 AM by kenjis.)

You could do it like this:
1. Extend the Pager class to use your PagerRenderer.
2. Replace the Pager class. See https://codeigniter4.github.io/CodeIgnit...re-classes
Reply
#4

(This post was last modified: 09-23-2021, 02:16 AM by paliz.)

i extend HttpIncoing Class you follow patern to  extend pageRender
see my codes
PHP Code:
<?php


namespace Modules\Auth\Interfaces;


use 
CodeIgniter\HTTP\RequestInterface;

/**
 * Expected behavior of a Security.
 */
interface RequestWithUserInterface extends RequestInterface
{
    public function setUser(object $userData);

    public function getUserVar(string $key): string;

    public function getUser(): object;


PHP Code:
<?php

namespace Modules\Auth\Libraries;

use 
CodeIgniter\HTTP\IncomingRequest;
use 
Modules\Auth\Interfaces\RequestWithUserInterface;


class  RequestWithUser extends IncomingRequest implements RequestWithUserInterface
{
    protected object $userData;
    protected array $serviceEvent;

    public function __construct($config$uri$body$userAgent)
    {
        parent::__construct($config$uri$body$userAgent);
        $this->userData = (object)[];
    }

    public function setUser(object $userData)
    {
        $this->userData $userData;
    }

    public function getUser(): object
    
{
        return $this->userData;
    }

    public function getUserVar(string $key): string
    
{
        if (isset($this->userData->$key)) {
            return $this->userData->$key;
        }

        return '';

    }



PHP Code:
<?php namespace Modules\Auth\Config;


use 
CodeIgniter\HTTP\UserAgent;
use 
Config\App;
use 
Config\Services as AppServices;
use 
Config\Services as BaseService;
use 
Modules\Auth\Libraries\RequestWithUser;
use 
Modules\Auth\Services\AuthService;
use 
Modules\Auth\Services\GroupsPermissionService;
use 
Modules\Auth\Services\PermissionService;
use 
Modules\Auth\Services\RoleRouteService;
use 
Modules\Auth\Services\GroupService;
use 
Modules\Auth\Services\UsersPermissionService;

class 
Services extends BaseService
{
    //--------------------------------------------------------------------

    /**
    * The Request class models an HTTP request.
    *
    * @param App|null $config
    * @param boolean $getShared
    *
    * @return RequestWithUser
    */
    public static function requestWithUser(App $config nullbool $getShared true)
    {
        if ($getShared) {
            return static::getSharedInstance('requestWithUser'$config);
        }

        $config $config ?? config('App');;
        return new RequestWithUser(
            $config,
            AppServices::uri(),
            'php://input',
            new UserAgent()
        );
    }

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



PHP Code:
<?php

namespace Modules\Shared\Controllers;

/**
 * Class BaseController
 *
 * BaseController provides a convenient place for loading components
 * and performing functions that are needed by all your controllers.
 * Extend this class in any new controllers:
 *    class Home extends BaseController
 *
 * For security be sure to declare any new methods as protected or private.
 *
 * @package CodeIgniter
 */


use CodeIgniter\HTTP\RequestInterface;
use 
CodeIgniter\HTTP\ResponseInterface;
use 
CodeIgniter\RESTful\ResourceController;
use 
Modules\Auth\Config\Services;
use 
Myth\Auth\AuthTrait;
use 
Psr\Log\LoggerInterface;
use  Modules\Shared\Interfaces\UrlQueryParamInterface;
use  Modules\Shared\Libraries\UrlQueryParam;

class 
ApiController extends ResourceController
{
    use AuthTrait;

    protected $format "";
    public object $userObject;
    public UrlQueryParamInterface $urlQueryParam;

    /**
    * An array of helpers to be loaded automatically upon
    * class instantiation. These helpers will be available
    * to all other controllers that extend BaseController.
    *
    * @var array
    */
    protected $helpers = [
        'cookie',
        'url',
        'from',
        'filesystem',
        'text',
        'shared'
    ];

    /**
    * Constructor.
    *
    * @param RequestInterface $request
    * @param ResponseInterface $response
    * @param LoggerInterface $logger
    */


    /**
    * @var string
    * Holds the session instance
    */
    protected $session;

    public function __construct()
    {

        $this->userObject = (object)[];
    }

    public function initController(RequestInterface $requestResponseInterface $responseLoggerInterface $logger)
    {
        // Do Not Edit This Line
        parent::initController($request$response$logger);


        $this->urlQueryParam = new UrlQueryParam();
        $this->urlQueryParam->initParameters($request);
        $requestWithUser Services::requestWithUser();
        $this->userObject $requestWithUser->getUser();

    }



easy peasy extend ci core
Enlightenment  Is  Freedom
Reply
#5

I use the solution of Kenjis and it works really well.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB