How To Set API Rate Limiting In Laravel

Submitted by azazqadir - 9 months ago

Rate limiting is the control of the number of requests per unit time. It can be applied to ports, IPs, routes, etc. when used correctly, it can efficiently block out malicious bots. In the case of our API, it can mitigate DOS attacks , thus, making our API accessible without downtime for legitimate users. Source: http://www.cloudways.com/blog/laravel-and-api-rate-limiting/

Route::group(['prefix' => 'api/v1'], function () {
 
Route::get('/getTasks', function () {
 
return Task::all();
 
});
 
Route::post('/addTask', function (Request $request) {
 
$validator = Validator::make($request->all(), [
 
'names' => 'required|max:255',
 
]);
 
if ($validator->fails()) {
 
return response()->json(['error' => $validator->messages()],
 
200);
 
}
 
$task = new Task;
 
$task->names = $request->names;
 
$task->save();
 
return response()->json(['response' => "Added {$request->names} to tasks."],
 
200);
 
});
 
});
comments powered by Disqus

Ads cover the costs of maintaining Laravel-tricks