Laravel 5.2 中的 API 限制

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34660731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:59:59  来源:igfitidea点击:

API Throttle in Laravel 5.2

phplaravel

提问by Juliatzin

I was seeing this tutorialabout throttle in Laravel 5.2

我在 Laravel 5.2 中看到了有关油门的教程

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.

似乎throttle 只是用于API,但为什么不能用于其他控制器的东西,以避免人们通过Postman 发送100 次相同的表单。

I tell that, because in the Kernel.php, now, middleware are clearly divided between web and apis:Kernel.php:Laravel 5.2

我这么说,因为在 Kernel.php 中,现在中间件在 web 和 apis 之间明确划分:Kernel.php:Laravel 5.2

回答by Josh Janusch

You can apply it to web pages as well. Judging from your comments, you're confused as to the new features of Middleware, primarily Middleware Groups.

您也可以将其应用于网页。从您的评论来看,您对 Middleware 的新功能感到困惑,主要是Middleware Groups

5.2 brought along with it a way to group Middleware like you would with Route groups before. In 5.1 you would do something like:

5.2 带来了一种对中间件进行分组的方法,就像之前对路由组一样。在 5.1 中,您将执行以下操作:

Route::group(['prefix' => 'api', 'middleware'=>'auth,custom_middleware,permission:edit_permissions'], function() {
    Route::post('permissions/{id}/store', ['uses'=>'PermissionController@store']);
});

That is still completely valid, but if you wanted to add another Route group with the same middleware, you had to either juggle organization so they were nested beneath a single Route group that applied those middleware or you had to copy paste the middleware, neither very desirable. With 5.2, all you have to is this:

这仍然是完全有效的,但是如果您想添加另一个具有相同中间件的 Route 组,您必须调整组织,使它们嵌套在应用这些中间件的单个 Route 组下,或者您必须复制粘贴中间件,两者都不是可取。使用 5.2,您所要做的就是:

Kernel.php

protected $middlewareGroups = [
    'permissions_api' => [
         'auth', 
         'custom_middleware',
         'permission:edit_permissions',
     ]
];

routes.php

Route::group(['middleware' => ['permissions_api']], function () {
    Route::post('permissions/{id}/store', ['uses'=>'PermissionController@store']);
});

Route::group(['middleware' => ['permissions_api']], function () {
    Route::post('permissions/{id}/update', ['uses'=>'PermissionController@update']);
});

So you can group those middleware and apply them in those groups. That's what the apiand webyou are seeing is. It's just the default Middleware groups provided by Laravel that you can modify however you want. throttleis available as Middleware where ever you may need it. The below are both perfectly valid

因此,您可以将这些中间件分组并将它们应用到这些组中。这就是在apiweb你看到的是。这只是 Laravel 提供的默认中间件组,您可以根据需要进行修改。throttle可作为中间件在您可能需要的任何地方使用。以下都是完全有效的

Route::group(['middleware' => ['throttle:60,1']], function () {
    Route::post('permissions/{id}/update', ['uses'=>'PermissionController@update']);
});

or

或者

protected $middlewareGroups = [
    'permissions_api' => [
         'auth', 
         'custom_middleware',
         'permission:edit_permissions',
         'throttle:60,1'
     ] 
];

So throttleis just a middleware and can be applied just as any middleware is. It is defined in Kernel.phpas 'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,and the 60,1are just middleware parameters, which were added in 5.1

Sothrottle只是一个中间件,可以像任何中间件一样应用。它被定义Kernel.php'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,60,1只是中间件参数,这是在5.1中加入