I am using api throttle in my routes/api.php
(as you can see in the code) but I am wondering if I can use it in the controller on methods.
Route::resource('/user/{user}/post', 'UserPostController')->middleware(['auth:api', 'throttle:5,1']);
Laravel simplifies the process of limiting the number of requests a client could make for a particular action. Consider the following snippet from the Laravel Docs: Route::middleware('auth:api', 'throttle:60,1')->group(function () { Route::get('/user', function () { // }); });
Laravel incorporates a middleware that confirms whether or not the client of the application is verified. If the client is confirmed, it diverts to the home page otherwise, it diverts to the login page. All controllers in Laravel are created in the Controllers folder, located in App/Http/Controllers.
Laravel includes a simple to use rate limiting abstraction which, in conjunction with your application's cache, provides an easy way to limit any action during a specified window of time. If you are interested in rate limiting incoming HTTP requests, please consult the rate limiter middleware documentation.
RESTful Resource Controllers The Artisan command will generate a controller file at app/Http/Controllers/PhotoController. php . The controller will contain a method for each of the available resource operations.
Created by Graham Campbell, Laravel Throttle is an exclusively built package that perform rate limiting in Laravel 5. It requires PHP 7.x version for its proper functioning and supports optional configuration.
It allows you to quickly make HTTP requests to communicate with external APIs. In this tutorial, you will learn how to use the Laravel HTTP Client, and consume an external API and store the data in a database. Before you start, you would need to have a Laravel application up and running.
It seems that throttle is just used for APIs, but why couldn't be used for other controller stuff, to avoid that people send 100 times the same form through Postman. I tell that, because in the Kernel.php, now, middleware are clearly divided between web and apis: Kernel.php:Laravel 5.2
Laravel includes a simple to use rate limiting abstraction which, in conjunction with your application's cache, provides an easy way to limit any action during a specified window of time. If you are interested in rate limiting incoming HTTP requests, please consult the rate limiter middleware documentation.
Better to use the routes to specify the middleware for the routes. Still you think to use / specify inside your controller you can define __construct()
menthod in your controller like:
public function __construct()
{
$this->middleware('throttle:5,1')->only('index');
}
This will work on the index
action of your controller only.
For more details check the documentation Controller Middlewares
you can override route for example
Route::resource('/user/{user}/post', 'UserPostController')->middleware(['auth:api', 'throttle:5,1']);
//add route after resource
Route::get('/user/create', 'UserPostController@create')->middleware(['auth:api', 'throttle:5,1']);
second way add condition in controller
public function __construct()
{
$this->middleware('auth:api');
$this->middleware('throttle:10,1')->only('create');
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With