CodeIgniter Forums
Extending core class IncomingRequest - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forum-28.html)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forum-30.html)
+--- Thread: Extending core class IncomingRequest (/thread-75496.html)



Extending core class IncomingRequest - x1250 - 02-12-2020

Hello folks.

Someone has an idea of how making this work?

app/Libraries/IncomingRequest.php
PHP Code:
<?php
namespace App\Libraries;
use 
CodeIgniter\HTTP\IncomingRequest;

class 
IncomingRequest extends IncomingRequest {

    public function __construct(...Same args as in original...) {
       exit('I DONT SEE THIS');
       parent::__construct(...Same args as in original..);
    }


I'm actually trying to make some changes to method getPost(), but this file doesn't seem to be working at all.


RE: Extending core class IncomingRequest - kilishan - 02-12-2020

Did you create a new Services method?

Copy the request() method from system/Config/Services.php[/php] into [b]app/Config/Services.php. Modify to load your new class instead of the CI one. Should work out of the box after that.


RE: Extending core class IncomingRequest - x1250 - 02-14-2020

(02-12-2020, 08:48 PM)kilishan Wrote: Did you create a new Services method?

Copy the request() method from system/Config/Services.php[/php] into [b]app/Config/Services.php. Modify to load your new class instead of the CI one. Should work out of the box after that.
[/b]

Hello Kilishan,

Thank you for your pointer. I did try to play with Config/Services.php, but I think I don't know what to change exactly. If I copy de request function from system/Config/Services.php to app/Config/Services.php, I get not output, no error, nothing. The app becomes dead.

This is what I have:


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 
request(App $config nullbool $getShared true)
    {
        if (
$getShared)
        {
            return static::
getSharedInstance('request'$config);
        }

        if (! 
is_object($config))
        {
            
$config config(App::class);
        }

        return new 
IncomingRequest(
                
$config,
                static::
uri(),
                
'php://input',
                new 
UserAgent()
        );
    }



I tried returning App\Libraries\IncomingRequest instead of just IncomingRequest, but with the same result, not output, not even a message in the logs. This is also not in the userguide Sad

Any help?

Thank you.


RE: Extending core class IncomingRequest - dave friend - 02-14-2020

I think all you need to do is add the proper "use" clause into /app/Config/Services.php
The top part of the file should look like this
PHP Code:
<?php namespace Config;

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

//add the following
use App\Libraries\IncomingRequest;

require_once 
SYSTEMPATH 'Config/Services.php';

class 
Services extends CoreServices 

Everything else in the file remains as you show it above. That all looks good to me.