Skip to content

Commit

Permalink
支持路由向中间件传参
Browse files Browse the repository at this point in the history
  • Loading branch information
yuanzhihai committed Sep 13, 2022
1 parent e5caf63 commit 66c6908
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
17 changes: 8 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,20 +138,19 @@ Route::any('/api/driver-ocr', [ app\api\controller\Ocr::class, 'driver'])->middl
]);
//错误使用例子(不支持路由中间件默认传参
//正确使用向中间件传参例子 (Webman-framework >= 1.3.16
Route::group('/path', function() {
//路由注册
...
//路由注册
...
})->middleware(\app\middleware\Throttle::class, [
'visit_rate' => '20/m',
...
...
]);
})->setParams(['visit_rate' => '20/m',
...
...
])->middleware(\app\middleware\Throttle::class);
```

## 注意:
1、截至(Webman 1.2.5, Webman-framework v1.2.7) 不支持路由中间件默认传参
1、Webman-framework >= 1.3.16 支持路由向中间件传参 使用 $route->param() 方法
2、禁止访问时,throttle 默认是抛出 HttpResponseException, 当前插件场景下将正常响应一个httpResponse(即不会主动:Throw Exception),特殊需求请在 "visit_fail_response" 匿名函数中配置

## 申明
Expand Down
7 changes: 5 additions & 2 deletions src/middleware.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ use Webman\Http\Request;
*/
class Throttle implements MiddlewareInterface
{
public function process(Request $request, callable $next,array $params = []):Response
public function process(Request $request, callable $next):Response
{
return (new \yzh52521\middleware\Throttle())->handle($request, $next, $params);
if ( $route = $request->route ) {
$params = $route->param();
}
return (new \yzh52521\middleware\Throttle())->handle($request, $next, $params??[]);
}
}

0 comments on commit 66c6908

Please sign in to comment.