Hello everyone,
I'm trying to extend Laravel's Illuminate\Http\Request class to create a custom request class and use it in a middleware, but I'm having some trouble getting it to work. Here's what I've done so far:
Created a new class called CustomRequest that extends Illuminate\Http\Request. In this class, I've added a new method called apiKey that retrieves the value of the API-KEY header from the request and performs some validation on it.
use Illuminate\Http\Request;
class CustomRequest extends Request {
public function apiKey()
{
$apiKey = $this->header('API-KEY');
/* Some validation on the API KEY */
return $apiKey;
}
}
Created a new service provider called RequestServiceProvider that binds Illuminate\Http\Request to CustomRequest and registered it Config\app.php. This tells Laravel's service container to use CustomRequest whenever an instance of Illuminate\Http\Request is requested.
class RequestServiceProvider extends ServiceProvider {
public function register() {
$this->app->bind('Illuminate\Http\Request', CustomRequest::class);
}
}
Created a new middleware called TestMiddleware and registered it in Kernel.php.
class TestMiddleware {
public function handle(Request $request, Closure $next) {
dd(get_class($request));
}
}
However, when I try to test my middleware, I'm getting an instance of Illuminate\Http\Request instead of CustomRequest. I'm not sure what is causing this issue.
Can anyone help me figure out what I'm doing wrong? I would really appreciate any advice or guidance.