Welcome Guest, Not a member yet? Register   Sign In
CI4 - Extending Core System Class - HTTP/URI
#1
Question 

Hello, i upgraded my project from CI3 to CI4. In Upgraded project, i tried to extend the System HTTP\URI class with the reference of CI4 user guide. But extended class not loaded properly.

Code as follows:

1.app/libraries/URI.php

Code:
<?php namespace App\Libraries;

use CodeIgniter\HTTP\Exceptions\HTTPException;

class URI extends \CodeIgniter\HTTP\URI
{

     public function setURI(string $uri = null)
    {
        if (! is_null($uri))
        {
            $parts = parse_url($uri);

                        //need to do some stuff here..

            if ($parts === false)
            {
                throw HTTPException::forUnableToParseURI($uri);
            }
            $this->applyParts($parts);
        }
                 
        return $this;
    }

}

2.app/config/services.php

Code:
<?php namespace Config;

use CodeIgniter\Config\Services as CoreServices;
use CodeIgniter\Config\BaseConfig;

require_once SYSTEMPATH . 'Config/Services.php';

class Services extends CoreServices
{

    public static function uri(string $uri = NULL, bool $getShared = false)
    {
            if ($getShared)
            {
                    return static::getSharedInstance('uri',$uri);
            }

            return new \App\Libraries\URI($uri);
    }
}

Note: Extended URI class method($this->setURI) loaded when accessed like Service::uri, else it loads from Codeigniter\HTTP\URI.php if accessed like "new URI()". I want to override the [font=Tahoma, Verdana, Arial, sans-serif]method($this->setURI) completely. [/font]
Reply
#2

When using new you have to be explicit about the namespace which can be done with use

PHP Code:
use App\Libraries\URI;

$uri = new URI(); 

Or it can be done with a fully qualified class name the way you did in app/config/services.php

PHP Code:
$uri = new \App\Libraries\URI(); 

Note the leading backslash in the above line of code.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB