PHP PUT 方法 Slim 框架

PHP PUT method Slim framework

请问为什么put里面只有一个空字段?当 post 正常工作时?

$app->post('/books', function (Request $request, Response $response, $args){
    $book = Books::fromResultSet($request->getParsedBody());
    if($book->valid())
        return $response->withStatus(400);

    $book->insert();
    return $response->withStatus(201);
})->add($securityMiddleware);

$app->put('/books/{id}', function (Request $request, Response $response, $args){
    $book = Books::fromResultSet($request->getParsedBody());
    print_r($book);

    /*if($book->valid())
        return $response->withStatus(400);

    $book->update();
    return $response->withStatus(201);*/
})->add($securityMiddleware);

上市后出错print_r($request->getParsedBody())

C:\MAMP\htdocs\vendor\slim\slim\Slim\Handlers\Strategies\RequestResponse.php(43): {closure}(Object(GuzzleHttp\Psr7\ServerRequest), Object(GuzzleHttp\Psr7\Response), Array)
    #1 C:\MAMP\htdocs\vendor\slim\slim\Slim\Routing\Route.php(384): Slim\Handlers\Strategies\RequestResponse->__invoke(Object(Closure), Object(GuzzleHttp\Psr7\ServerRequest), Object(GuzzleHttp\Psr7\Response), Array)
    #2 C:\MAMP\htdocs\public\index.php(27): Slim\Routing\Route->handle(Object(GuzzleHttp\Psr7\ServerRequest))
    #3 C:\MAMP\htdocs\vendor\slim\slim\Slim\MiddlewareDispatcher.php(313): {closure}(Object(GuzzleHttp\Psr7\ServerRequest), Object(Slim\Routing\Route))
    #4 C:\MAMP\htdocs\vendor\slim\slim\Slim\MiddlewareDispatcher.php(81): class@anonymous->handle(Object(GuzzleHttp\Psr7\ServerRequest))
    #5 C:\MAMP\htdocs\vendor\slim\slim\Slim\MiddlewareDispatcher.php(81): Slim\MiddlewareDispatcher->handle(Object(GuzzleHttp\Psr7\ServerRequest))
    #6 C:\MAMP\htdocs\vendor\slim\slim\Slim\Routing\Route.php(341): Slim\MiddlewareDispatcher->handle(Object(GuzzleHttp\Psr7\ServerRequest))
    #7 C:\MAMP\htdocs\vendor\slim\slim\Slim\Routing\RouteRunner.php(84): Slim\Routing\Route->run(Object(GuzzleHttp\Psr7\ServerRequest))
    #8 C:\MAMP\htdocs\vendor\slim\slim\Slim\Middleware\RoutingMiddleware.php(59): Slim\Routing\RouteRunner->handle(Object(GuzzleHttp\Psr7\ServerRequest))
    #9 C:\MAMP\htdocs\vendor\slim\slim\Slim\MiddlewareDispatcher.php(147): Slim\Middleware\RoutingMiddleware->process(Object(GuzzleHttp\Psr7\ServerRequest), Object(Slim\Routing\RouteRunner))
    #10 C:\MAMP\htdocs\vendor\slim\slim\Slim\Middleware\ErrorMiddleware.php(107): class@anonymous->handle(Object(GuzzleHttp\Psr7\ServerRequest))
    #11 C:\MAMP\htdocs\vendor\slim\slim\Slim\MiddlewareDispatcher.php(147): Slim\Middleware\ErrorMiddleware->process(Object(GuzzleHttp\Psr7\ServerRequest), Object(class@anonymous))
    #12 C:\MAMP\htdocs\vendor\slim\slim\Slim\MiddlewareDispatcher.php(81): class@anonymous->handle(Object(GuzzleHttp\Psr7\ServerRequest))
    #13 C:\MAMP\htdocs\vendor\slim\slim\Slim\App.php(215): Slim\MiddlewareDispatcher->handle(Object(GuzzleHttp\Psr7\ServerRequest))
    #14 C:\MAMP\htdocs\vendor\slim\slim\Slim\App.php(199): Slim\App->handle(Object(GuzzleHttp\Psr7\ServerRequest))
    #15 C:\MAMP\htdocs\public\index.php(85): Slim\App->run()
    

参考自document.

It’s very common in web APIs to send data in JSON or XML format. Out of the box, PSR-7 implementations do not support these formats, you have to decode the Request object’s getBody() yourself. As this is a common requirement, Slim 4 provides BodyParsingMiddleware to handle this task.

在您的 $app = AppFactory::create(); 之后添加此代码。

// Parse json, form data and xml
$app->addBodyParsingMiddleware();

在您的应用程序功能中。正常使用 $request->getParsedBody()
示例:

$app->any('/books/{id}', function (Request $request, Response $response, $args){
    print_r($request->getParsedBody());
    return $response;
});

结果:

如果仍然报错,请尝试将Slim 4更新到最新版本再试。