有没有办法在控制器内的单个方法上使用 Laravel api 节流阀?

Mar*_*scu 5 php laravel laravel-routing

我在我的routes/api.php(如您在代码中看到的)中使用了 api 节流阀,但我想知道我是否可以在方法的控制器中使用它。

Route::resource('/user/{user}/post', 'UserPostController')->middleware(['auth:api', 'throttle:5,1']);
Run Code Online (Sandbox Code Playgroud)

Vik*_*hak 5

最好使用路由来指定路由的中间件。您仍然认为在控制器中使用 / 指定,您可以__construct()在控制器中定义方法,例如:

public function __construct()
{
    $this->middleware('throttle:5,1')->only('index');
}
Run Code Online (Sandbox Code Playgroud)

index仅适用于您的控制器的操作。

有关更多详细信息,请查看文档控制器中间件