Welcome Guest, Not a member yet? Register   Sign In
How to extend PagerRenderer and add additional methods
#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


Messages In This Thread
RE: How to extend PagerRenderer and add additional methods - by paliz - 09-23-2021, 02:14 AM



Theme © iAndrew 2016 - Forum software by © MyBB