【发布时间】:2017-12-02 16:14:51
【问题描述】:
我想让用户每分钟最多只能发表 1 条评论。
我尝试过简单地使用 throttle 中间件,但它不起作用。我仍然可以每秒发布 cmets。
路线代码:
Route::post('comment/{id}', 'HomeController@comment')->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 条评论,我该如何设置它以闪烁错误而不是保存?
【问题讨论】:
-
throttle 中间件是 Laravel 的核心功能。
标签: php laravel throttling