2016-07-05 6 views
0

私はスリム3のミドルウェアとしてjustinrainbowからJSONスキーマバリデータを実装するためにスリム3ミドルウェアの検証

  1. をしようとしている私はでGET/POSTリクエストからクライアントの入力を取得する方法を見つけ出すことはできませんミドルウェア。 は次のように試してみました:私はそれを必要とする方法の2つの可能な方法があります

    $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); 
    
  2. 。私は間違っているの?

    1. ミドルウェアでそれを検証し、コントローラにいくつかの配列/ JSON応答を送信、私は私は$data = $request->getParsedBody();

    2. で理解されるようにミドルウェアでそれを検証してしまいますが、最終チェックは、このようなコントローラにされます:それは何かする私のために

      $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); 
      

ベストオプションgのようなhere しかし、私はコンテナに戻って、どのように取得する必要があります/投稿の入力をコンテナに取得/投稿入力を理解していません

+0

ミドルウェアでは 'print_r()'だけではできません。代わりに '$ response-> write(print_r($ data)、true))のようなものを試してみてください。' ' –

答えて

0

あなたのコードは問題なく、 。この時点では、ルートはまだ解決されていないため、パラメータはURLから解析されません。

これは既知の使用例であり、Slim's documentationに記載されています。あなたのコードの作業を取得するアプリの設定に次の設定を追加します - それは長くはありません、それは本当に説明

レスポンスオブジェクトを操作する方法ミドルウェア作品や方法を理解するために
$app = new App([ 
    'settings' => [ 
     // Only set this if you need access to route within middleware 
     'determineRouteBeforeAppMiddleware' => true 
    ] 
]); 

は、私はあなたがUser Guide読むことをお勧めよく

関連する問題