2016-09-27 96 views
0

我正在使用https://github.com/GrahamCampbell/Laravel-Throttle,我想向用戶返回一個消息,指出他們需要等待多少分鐘才​​能再次嘗試。我檢查了這個教程:http://bicknoyle.com/blog/2015/10/09/throttling-requests-in-laravel/,它提供了下面的例子:在Laravel使用Graham Campbell的Laravel節流閥調節請求

public function render($request, Exception $e) 
{ 
    if ($e instanceof \Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException) { 
     return response('Too many requests. Slow your roll!'); 
    } 
    return parent::render($request, $e); 
} 

我怎樣才能讓用戶知道他們有多少分鐘要等待?

我試過dd($ e);它會返回

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

但每次刷新頁面時Retry-After停留在120時,它不會倒數。有什麼想法,我可以解決這個問題?

回答

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