当前位置:网站首页>laravel8更新之速率限制改进
laravel8更新之速率限制改进
2020-11-08 13:07:00 【神兵小将】
原文地址:https://www.wjcms.net/archives/laravel8%E6%9B%B4%E6%96%B0%E4%B9%8B%E9%80%9F%E7%8E%87%E9%99%90%E5%88%B6%E6%94%B9%E8%BF%9B
Laravel的请求速率限制器功能已增强,具有更大的灵活性和功能,同时仍保持与先前版本的throttle中间件API的向后兼容性。
速率限制器是使用RateLimiter立面的for方法定义的。该for方法接受一个速率限制器名称和一个Closure,该Closure返回应应用于分配了该速率限制器的路由的限制配置:
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Support\Facades\RateLimiter;
RateLimiter::for('global', function (Request $request) {
return Limit::perMinute(1000);
});
由于速率限制器回调接收传入的HTTP请求实例,因此您可以根据传入的请求或经过身份验证的用户动态构建适当的速率限制:
RateLimiter::for('uploads', function (Request $request) {
return $request->user()->vipCustomer()
? Limit::none()
: Limit::perMinute(100);
});
有时您可能希望将速率限制按任意值进行细分。例如,您可能希望允许用户每个IP地址每分钟100次访问给定路由。为此,您可以by在建立速率限制时使用以下方法:
RateLimiter::for('uploads', function (Request $request) {
return $request->user()->vipCustomer()
? Limit::none()
: Limit::perMinute(100)->by($request->ip());
});
可以使用throttle 中间件将速率限制器附加到路由或路由组。油门中间件接受您希望分配给路线的速率限制器的名称:
Route::middleware(['throttle:uploads'])->group(function () {
Route::post('/audio', function () {
//
});
Route::post('/video', function () {
//
});
});
版权声明
本文为[神兵小将]所创,转载请带上原文链接,感谢
https://my.oschina.net/u/3380391/blog/4707811
边栏推荐
- 适合c/c++新手学习的一些项目,别给我错过了!
- BCCOIN告诉您:年底最靠谱的投资项目是什么!
- C语言I博客作业03
- Get PMP certificate at 51CTO College
- 阿里出品!视觉计算开发者系列手册(附网盘链接)
- On monotonous stack
- 分布式文档存储数据库之MongoDB基础入门
- Don't look! Full interpretation of Alibaba cloud's original data lake system! (Internet disk link attached)
- Entry level! Teach you how to develop small programs without asking for help (with internet disk link)
- Why is Schnorr Signature known as the biggest technology update after bitcoin segwit
猜你喜欢
打工人,打工魂,抽终身会员,成为人上人!
Rust: performance test criteria Library
Entry level! Teach you how to develop small programs without asking for help (with internet disk link)
这次,快手终于比抖音'快'了!
BCCOIN告诉您:年底最靠谱的投资项目是什么!
Ali teaches you how to use the Internet of things platform! (Internet disk link attached)
阿里教你深入浅出玩转物联网平台!(附网盘链接)
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
华为云重大变革:Cloud&AI 升至华为第四大 BG ,火力全开
笔试面试题目:盛水最多的容器
随机推荐
Written interview topic: looking for the lost pig
Why is Schnorr Signature known as the biggest technology update after bitcoin segwit
AQS解析
Python Gadgets: code conversion
分布式文档存储数据库之MongoDB基础入门
蘑菇街电商交易平台服务架构及改造优化历程(含PPT)
笔试面试题目:求缺失的最小正整数
Tight supply! Apple's iPhone 12 power chip capacity exposed
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
Ali! Visual computing developer's series of manuals (with internet disk link)
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
Python basic syntax variables
Stm32uberide download and install - GPIO basic configuration operation - debug (based on CMSIS DAP debug)
Win10 terminal + WSL 2 installation and configuration guide, exquisite development experience
供货紧张!苹果被曝 iPhone 12 电源芯片产能不足
打工人,打工魂,抽终身会员,成为人上人!
Tidb performance competition 11.02-11.06
How to write a resume and project
擅长To C的腾讯,如何借腾讯云在这几个行业云市场占有率第一?
Python基础语法