有没有一种方法可以在控制器内部的各个方法上使用Laravel api节流阀?

时间:2019-06-27 07:21:38

标签: php laravel laravel-routing

我正在db.collection.aggregate([ { "$unwind": "$blocks" }, { "$unwind": "$blocks.port" }, { "$match": { "blocks.port.portType.function": "input" } } ]) 中使用api油门(如代码中所示),但我想知道是否可以在方法的控制器中使用它。

[
  {
    "_id": "1",
    "blocks": {
      "port": {
        "portType": {
          "function": "input"
        }
      }
    }
  }
]

2 个答案:

答案 0 :(得分:2)

最好使用路由为路由指定中间件。仍然您认为可以在控制器内部使用/指定,您可以在控制器中定义IList<string> test = new List<string>(); test.Add(s1); test.Add(s2); test.Add(s3); test.Add(s4); test.Add(s5); 方法,例如:

foreach (var item in test)
{
    Console.WriteLine(item);
}

这仅对您控制器的__construct()操作有效。

有关更多详细信息,请查看文档Controller Middlewares

答案 1 :(得分:1)

例如,您可以覆盖路线

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

//在资源后添加路由

Route::get('/user/create', 'UserPostController@create')->middleware(['auth:api', 'throttle:5,1']);

控制器中的第二种添加条件

public function __construct()
{
   $this->middleware('auth:api');
   $this->middleware('throttle:10,1')->only('create');
}