Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

clear-y's avatar

Extending Illuminate\Http\Request

Hi,

I need to extend the Illuminate\Http\Request class in order to modify the way it gets the IP address. So far I've got it working everywhere except during unit tests, where exceptions seem to end up with an instance of Illuminate\Http\Request being passed into the exception handler, rather than my class (\App\Http\Request).

/public/index.php

$kernel = $app->make(\Illuminate\Contracts\Http\Kernel::class);

$response = $kernel->handle(
    $request = App\Http\Request::capture();
);

$response->send();

$kernel->terminate($request, $response);

I've seen on Stack Overflow that you need to also include $app->alias('request', 'App\Http\Request');, but this doesn't seem to make a difference.

Has anyone had any luck exdending the Request class so that it works in every situation?

0 likes
4 replies
clear-y's avatar

Sadly not, I've already read that thread. Like I said, it works apart from during unit tests :(

clear-y's avatar

Ah! I've made some progress in debugging the issue!

It looks like Laravel's testing library has a hardcoded reference to the Illuminate class when it create the Request instance:

    /**
     * Call the given URI and return the Response.
     *
     * @param  string  $method
     * @param  string  $uri
     * @param  array   $parameters
     * @param  array   $cookies
     * @param  array   $files
     * @param  array   $server
     * @param  string  $content
     * @return \Illuminate\Http\Response
     */
    public function call($method, $uri, $parameters = [], $cookies = [], $files = [], $server = [], $content = null)
    {
        $kernel = $this->app->make('Illuminate\Contracts\Http\Kernel');

        $this->currentUri = $this->prepareUrlForRequest($uri);

        $this->resetPageContext();

        $request = Request::create(
            $this->currentUri, $method, $parameters,
            $cookies, $files, array_replace($this->serverVariables, $server), $content
        );

        $response = $kernel->handle($request);

        $kernel->terminate($request, $response);

        return $this->response = $response;
    }

Gunna be tricky working out how to nicely extend/fix this, but I'll update this thread for anyone experiencing the issue!

clear-y's avatar
clear-y
OP
Best Answer
Level 1

I realise I was being quite dense and that the fix is actually super easy! Just override the method in the base \TestCase class (note the use of \App\Http\Request::create()):

    /**
     * Call the given URI and return the Response.
     *
     * We're overiding the method provided in the \Illuminate\Foundation\Testing\Concerns\MakesHttpRequests
     * trait in order to make use of our customer \App\Http\Request class.
     *
     * @param string $method
     * @param string $uri
     * @param array $parameters
     * @param array $cookies
     * @param array $files
     * @param array $server
     * @param string $content
     * @return \Illuminate\Http\Response
     */
    public function call($method, $uri, $parameters = [], $cookies = [], $files = [], $server = [], $content = null)
    {
        $kernel = $this->app->make('Illuminate\Contracts\Http\Kernel');

        $this->currentUri = $this->prepareUrlForRequest($uri);

        $this->resetPageContext();

        $request = \App\Http\Request::create(
            $this->currentUri, $method, $parameters,
            $cookies, $files, array_replace($this->serverVariables, $server), $content
        );

        $response = $kernel->handle($request);

        $kernel->terminate($request, $response);

        return $this->response = $response;
    }

Please or to participate in this conversation.