Laravel使用Graham Campbell的Laravel Throttle进行节流请求

时间:2016-09-27 17:39:03

标签: laravel

我正在使用https://github.com/GrahamCampbell/Laravel-Throttle,我想向用户返回一条消息,告诉他们在再次尝试之前需要等待多少分钟。我已经查看过本教程:http://bicknoyle.com/blog/2015/10/09/throttling-requests-in-laravel/,它提供了以下示例:

-statusCode: 429
  -headers: array:1 [▼
    "Retry-After" => 120
  ]
  #message: "Rate limit exceeded."

如何让用户知道他们需要等待多少分钟?

我试过dd($ e);然后它返回

[oracle@myserver ~]$ lsnrctl start rane

LSNRCTL for Linux: Version 11.2.0.1.0 - Production on 27-SEP-2016 23:04:58

Copyright (c) 1991, 2009, Oracle.  All rights reserved.

Starting /oraeng/app/oracle/product/11.2.0/bin/tnslsnr: please wait...

TNSLSNR for Linux: Version 11.2.0.1.0 - Production
System parameter file is /home/oracle/listener.ora
Log messages written to /oraeng/app/oracle/product/11.2.0/log/diag/tnslsnr/myserver/rane/alert/log.xml
TNS-01150: The address of the specified listener name is incorrect

Listener failed to start. See the error message(s) above...

但每次刷新页面Retry-After保持在120时,它都不会倒计时。有什么想法我怎么能解决这个问题?

1 个答案:

答案 0 :(得分:1)

如果您使用Laravel 5.2或更高版本,您唯一应该做的就是将其添加到您的路线中:

'middleware' => 'throttle:5,10'
像这样:

Route::group(['prefix' => 'api', 'middleware' => 'throttle:5,10'], function () {
    Route::get('people', function () {
        return Person::all();
    });
});

这将增加再次发出请求的时间,看看这篇文章:

https://mattstauffer.co/blog/api-rate-limiting-in-laravel-5-2