The example in this article describes how to solve the problem of Laravel's throttle middleware failure. Share it with everyone for your reference, the details are as follows:
According to the official explanation, it is very simple to implement access frequency limit:
Route::get('test', function(){ return 'helle world' ; })->middleware('throttle');
It is indeed the case, The cache stores the number of accesses and makes judgments.
I used zizaco/entrust (a role-based permission management package) before, in which CACHE_DRIVER=file in .env was changed to CACHE_DRIVER=array. So the problem arises. Laravel supports multiple cache drivers, including File, Array, Db, Redis, etc., but throttle seems to be effective only when using File type drivers.
My modifications are as follows:
vendor/illuminate/cache/RateLimiter.php file
public function __construct(Cache $cache) { $this->cache = $cache; } public function __construct() { $this->cache = app('cache')->driver('file'); }
Change the above Just do the following. The throttle middleware also works.
For more articles related to solutions to Laravel’s throttle middleware failure problem, please pay attention to the PHP Chinese website!