I'm trying to implement json-schema validator from justinrainbow as middleware in Slim 3.
-
I can't figure out how to get the clients input from GET/POST requests in middleware. tried like this:
$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);
-
There are 2 possible ways of how I need it. what i'm doing wrong ?
validate it in middleware and send some array/json response to the controller, which i will then get as I understood with
$data = $request->getParsedBody();
-
validate it in middleware but final check will be in controller like this:
$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);
Best option for me it do something like here but I don't understand what should i return in container, and how to pass get/post input to container