我正在尝试将justinrainbow的 json-schema 验证器实现为 Slim 3 中的中间件。
我不知道如何从中间件中的 GET/POST 请求中获取客户端输入。像这样尝试:
$mw = function ($request, $response, $next) { $data = $request->getParsedBody(); print_r($data); // prints nothing $id = $request->getAttribute('loan_id'); print_r($id); // prints nothing // here I need to validate the user input from GET/POST requests with json-schema library and send the result to controller $response = $next($request, $response); return $response; }; $app->get('/loan/{loan_id}', function (Request $request, Response $response) use ($app, $model) { $loanId = $request->getAttribute('loan_id'); // here it works $data = $model->getLoan($loanId); $newResponse = $response->withJson($data, 201); return $newResponse; })->add($mw);
我需要它有两种可能的方式。我做错了什么?
在中间件中验证它并向控制器发送一些数组/json响应,然后我会得到我所理解的
$data = $request->getParsedBody();
在中间件中验证它,但最终检查将在控制器中进行,如下所示:
$app->get('/loan/{loan_id}', function (Request $request, Response $response) use ($app, $model) { if($validator->isValid()){ // } $loanId = $request->getAttribute('loan_id'); // here it works $data = $model->getLoan($loanId); $newResponse = $response->withJson($data, 201); return $newResponse; })->add($mw);
对我来说最好的选择它做类似这里的事情, 但我不明白我应该在容器中返回什么,以及如何将 get/post 输入传递给容器