Does this help
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?
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.