使用不同的 HTTP 方法重定向

Redirecting with a different HTTP method

我有一个端点来更新给定的实体,响应应该是更新后的实体。我 hterefore 尝试了以下,但是,重定向是另一个 PUT 请求。如何使用 GET 请求进行响应?

$app->put('/foo/{id:[0-9]+}', function (Request $request, Response $response, $args) {
    $this->foo->update($args['id'], $request->getParsedBody());
    return $response->withRedirect("/foo/$args[id]");
});

您可以使用 303 重定向:

The response to the request can be found under another URI using the GET method. When received in response to a POST (or PUT/DELETE), the client should presume that the server has received the data and should issue a new GET request to the given URI.

https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#303

$app->put('/foo/{id:[0-9]+}', function (Request $request, Response $response, $args) {
    $this->foo->update($args['id'], $request->getParsedBody());
    return $response->withStatus(303)->withRedirect("/foo/$args[id]");
});