Laravel 5.5 : Dynamically change throttle time

半城伤御伤魂 提交于 2021-01-29 11:10:42

问题


Currently i am working on login throttle, I have to change the throttle time on 2nd throttle dynamically.

How will i be able to do that ?


回答1:


Middleware (such as throttle) can be defined inside controllers as well.

A solution would be to conditionally set the middleware in the controllers' constructor, something along the lines of:

if (true) {
    $this->middleware('throttle:60,1');
} else {
    $this->middleware('throttle:30,1');
}

In Laravel 5.6, here’s how you specify a User model attribute used to determine the number of requests a user can make within the provided timeframe (in minutes):

Route::middleware('throttle:rate_limit,1');

See https://laravel-news.com/laravel-5-6-dynamic-rate-limiting and the docs for more information.

Good luck!



来源:https://stackoverflow.com/questions/51856113/laravel-5-5-dynamically-change-throttle-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!