I wanna limit the request to two different methods in two different controllers I set it up like that.
Controller 1
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('throttle:5,1')->only('store');
}
Controller 2
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('throttle:2,10')->only('store');
}
So now if the first middleware in the first controller takes effect I cant use the second one either. I thought the route throttles are indepentend from each other?
Ok thanks for that!! You know a way to prevent spam on methods? Lets say you dont want a user to post 20 comments in a minute or something like that? Is there a common way to do it with laravel or I have to create something on my own?
Looking back at the logic in the function I posted it does look like the route and ip are used if your using a stateless api. Therefore it depends on whether you have an authenticated user or not.
If you need per route limiting for you web routes you are going to need to write your own middleware. Not tested but this might work
<?php
namespace App\Http\Middleware;
use RuntimeException;
use Illuminate\Routing\Middleware\ThrottleRequests;
class ThrottleRequestsByRoute extends ThrottleRequests
protected function resolveRequestSignature($request)
{
if ($route = $request->route()) {
return sha1($route->getDomain().'|'.$request->ip());
}
throw new RuntimeException('Unable to generate the request signature. Route unavailable.');
}
}
You can then add to your routes. Not 100% on this working but worth a try. It might also mess with the existing middleware so YMMV
Thanks for you code implemented it but how to limit the route requests now?
How to achieve that it works like the throttle middleware $this->middleware('routeThrottle:2,10')->only('store');?
Registered it already in the kernel.php but did not know that it works already like $this->middleware('routeThrottle:2,10')->only('store'); that. Thanks !