laravel - Laravel 油门消息
问题描述
我正在使用 ThrottleRequest 来限制登录尝试。在 Kendler.php 我有
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
和我在 web.php 中的路线
Route::post('login', ['middleware' => 'throttle:3,1', 'uses' => 'Auth\LoginController@authenticate']);
当我第四次登录时,它返回状态 429 并带有消息“请求过多”。(默认情况下我猜)
但我只想返回错误消息,例如:
return redirect('/login')
->withErrors(['errors' => 'xxxxxxx']);
任何人帮助我!谢谢你!
解决方案
您可以扩展中间件并覆盖该buildException()
方法以更改它在抛出 a 时传递的消息,ThrottleRequestsException
或者您可以使用异常处理程序来捕获ThrottleRequestsException
并执行您想要的任何操作。
所以Exceptions/Handler.php
你可以做类似的事情
use Illuminate\Http\Exceptions\ThrottleRequestsException;
public function render($request, Exception $exception)
{
if ($exception instanceof ThrottleRequestsException) {
//Do whatever you want here.
}
return parent::render($request, $exception);
}
推荐阅读
- java - ListIterator.PreviousIndex 返回一个单元素整数数组而不是 Int?
- laravel - SQLSTATE [42000]:语法错误或访问冲突:1064 您的 SQL 语法有错误 - Laravel
- nhibernate - NHibernate Criteria Query - 分离查询和子查询:System.FormatException:输入字符串格式不正确
- javascript - 如何在开发和生产中深度链接 electronjs 应用程序?
- laravel - v-for 在 Laravel Vuejs 中检索唯一值
- neo4j - Neo4j:如何将变量传递给 Neo4j 查询
- c - 是否可以使用 EPROCESS 结构获取要执行的下一条指令的地址?
- java - Spark 集群分区中的数据库连接对象
- amazon-web-services - AWS Keyspace 的 Spring Data Cassandra 连接问题
- docker - 如何忽略 dockerfile 非零返回码?