2017-06-29 107 views
1

我想让用户最多只能发布每分钟1条评论。限制评论

我试过简单地使用throttle中间件,它不工作。我仍然可以每秒发表评论。

路线代码:

Route::post('comment/{id}', '[email protected]')->name('comment')->middleware('throttle'); 

控制器代码:

public function comment($id) 
{ 
    $this->validate(request(), [ 
     "body" => "required", 
    ]); 

    $jersey = Jersey::findOrFail($id); 
    $comment = new Comment; 
    $comment->user_id = auth()->user()->id; 
    $comment->jersey_id = $jersey->id; 
    $comment->body = request()->input('body'); 
    $comment->save(); 
    activity()->by(auth()->user())->withProperties($comment)->log('Commented'); 
    request()->session()->flash('status', 'Comment submitted!'); 

    return redirect()->route('concept', $id); 
} 

如何让这个它会闪烁而不是保存如果用户尝试发布超过1个评论的错误每分钟?

+0

油门中间件是Laravel的核心功能。 – kjdion84

回答

1

通常我使用油门航线组这样的:

Route::group(['middleware' => 'throttle:1'], function() { 
    // Your routes here 
    Route::get('/', '[email protected]')->name('comment'); 
    // ... 
)} 

但在你的情况,你可以指定油门参数一样,修改代码:

Route::post('comment/{id}', '[email protected]')->name('comment')->middleware('throttle:1'); 

不要忘记清除缓存以应用更改。