I am developing a project in laravel8 (which is still new) and I want to hide some custom response headers(starting with x-) like:
X-RateLimit-Limit: 15 X-RateLimit-Remaining: 14
I want to customize this behavior and hide these headers, how can I easily find code (in a framework), which set’s these headers
My code:
protected function configureRateLimiting() { RateLimiter::for('api', function (Request $request) { return Limit::perMinute(15); }); }
Advertisement
Answer
Let’s say we can create a new AppHttpMiddlewareThrottleRequests
class which will extend original IlluminateRoutingMiddlewareThrottleRequests
. And override the method getHeaders
to your own that will return empty array.
namespace AppHttpMiddleware; use IlluminateRoutingMiddlewareThrottleRequests as OriginalThrottleRequests; /** ... */ class ThrottleRequests extends OriginalThrottleRequests { /** * @inheritdoc */ protected function getHeaders($maxAttempts, $remainingAttempts, $retryAfter = null) { return []; } }
And then all you need to do is to change middleware definitions inside app/Http/Kernel.php
file. Property $routeMiddleware
where you register ‘throttle’ middleware IlluminateRoutingMiddlewareThrottleRequests::class
you can change to your own implementation AppHttpMiddlewareThrottleRequests::class
.
In my case it is:
/** * The application's route middleware. * * These middleware may be assigned to groups or used individually. * * @var array */ protected $routeMiddleware = [ 'auth' => AppHttpMiddlewareAuthenticate::class, //... 'throttle' => AppHttpMiddlewareThrottleRequests::class, //... ];