Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to use Laravel api throttle on individual method inside a controller?

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']);
like image 1000
Marian Popescu Avatar asked Jun 27 '19 07:06

Marian Popescu


People also ask

How do I use throttle middleware in Laravel?

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 () { // }); });

Can we use middleware in controller Laravel?

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.

What is rate limiter in Laravel?

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.

What is RESTful resource controller in Laravel?

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.

What is Laravel throttle?

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.

What is Laravel HTTP client and how to use it?

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.

Why can't throttle be used for controller?

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

How do I use rate limiting in Laravel?

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.


2 Answers

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

like image 155
Vikash Pathak Avatar answered Sep 28 '22 10:09

Vikash Pathak


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');
}
like image 24
Jignesh Joisar Avatar answered Sep 28 '22 11:09

Jignesh Joisar