在 Slim 3 中,我有一组具有相同操作的组,这取决于$args
:
$this->group('{id}/', function () {
$this->get('first/', function (Request $req, Response $res, $args) {
$myData = operations($args['id']);
...
});
$this->post('second/', function (Request $req, Response $res, $args) {
$myData = operations($args['id']);
...
});
});
我可以将那些常见的操作转移到更高的层次。正如我所读到的,它可能是中间件,但在中间件中我不能(或不知道如何)访问$args
.
->add(function (ServerRequestInterface $request, ResponseInterface $response, callable $next) {
//how to get arguments?
$request = $request->withAttribute('myData', operations($id);
$response = $next($request, $response);
return $response;
});