Laravel: There is a RateLimiter and you didn’t know

You have a rate limit, and you have a rate limit, and you have a rate limit!

How it works?

$limiter = app(RateLimiter::class);
/**
* Do something tied to rate limiting
*
* @param \Illuminate\Cache\RateLimiter $limiter
* @return \Illuminate\Http\Response
* @throws \Illuminate\Validation\ValidationException
*/
public function notTooManyTimes(RateLimiter $limiter)
{
if ($limiter->tooManyAttempts('my_action')) {
// .. do something
}
// ...
}
$limiter = app(RateLimiter::class, [
'cache' => Cache::store('memcache')
]);
/**
* Bootstrap any application services.
*
*
@return void
*/

public function boot()
{
// ...

$this->app
->when(\Illuminate\Cache\RateLimiter::class)
->needs(\Illuminate\Contracts\Cache\Repository::class)
->give(function ($app) {
return $app->make('cache')->store('memcached');
});

Basic usage

Can we stop reckless Requests?

Graphic Designer graduate. Full Stack Web Developer. Retired Tech & Gaming Editor.