Home >Backend Development >PHP Tutorial >How to solve the problem of Laravel's throttle middleware failure

How to solve the problem of Laravel's throttle middleware failure

不言
不言Original
2018-06-13 15:38:291983browse

This article mainly introduces the solution to Laravel's throttle middleware failure problem. It briefly analyzes the reasons for the throttle middleware failure problem and proposes a solution. It has certain reference value. Friends in need can refer to it

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');

This 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 a variety of 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.

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

How to solve the problem of PHP mkdir() not having write permission

The above is the detailed content of How to solve the problem of Laravel's throttle middleware failure. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:About Laravel log usageNext article:About Laravel log usage