2017-04-20 34 views

回答

1

所以,一個簡單的方法就是改變油門中間件。

首先,創建它擴展了基本節氣門中間件這樣一個新的中間件:

namespace App\Http\Middleware; 

use Illuminate\Routing\Middleware\ThrottleRequests as 
BaseThrottleRequests; 

class ThrottleRequests extends BaseThrottleRequests 
{ 
} 

然後更改應用程序的油門中間件/ HTTP/Kernel.php:

'throttle' => \App\Http\Middleware\ThrottleRequests::class 

它將現在使用自己的油門中間件,並且從laravel延伸出來,它具有它的功能並且像以前一樣工作。

然後,查看基類內部的內容,您會發現buildResponse會在出現Attemps過多的情況下構建響應。因此,你只需要重寫它的中間件裏面:

protected function buildResponse($key, $maxAttempts) 
{ 
    $retryAfter = $this->limiter->availableIn($key); // This gives you the number of seconds before the next time it is available 

    return redirect('test')->with('error', '...'); // You can use redirect() and all those stuffs as you would normally do to redirect the user and set a session message 
} 
+0

謝謝你洙多亞歷克斯,它就像一個魅力:) – user2983865

+0

不客氣:) –

+0

亞歷克斯,我有一個問題。你的解決方案效果很好,但我顯示錯誤消息硬編碼像這樣:'返回重定向('測試') - >與('錯誤','嘗試10分鐘後');'有可能顯示重試 - 後動態?我的意思是,如果5分鐘左顯示'嘗試5分鐘後'等。 – user2983865