dt888999 2016-07-05 08:31
浏览 28
已采纳

Slim 3中间件验证

I'm trying to implement json-schema validator from justinrainbow as middleware in Slim 3.

  1. 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);
    
  2. There are 2 possible ways of how I need it. what i'm doing wrong ?

    1. 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();

    2. 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

  • 写回答

1条回答 默认 最新

  • dougang1967 2016-07-09 22:48
    关注

    Your code in the first point seems alright, you only try to access route parameter from within middleware. At that point the route is not yet resolved and therefore parameters are not parsed from the URL.

    This is a known use case and is described in Slim's documentation. Add the following setting to your app configuration to get your code working:

    $app = new App([
        'settings' => [
            // Only set this if you need access to route within middleware
            'determineRouteBeforeAppMiddleware' => true
        ]
    ]);
    

    In order to understand how middleware works and how to manipulate response object, I suggest you read the User Guide - it's not that long and explains it really well.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?