我有一个端点来更新给定实体,响应应该是更新后的实体。我因此尝试了以下操作,但是,重定向是另一个 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]");
});
关于php - 使用不同的 HTTP 方法重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57714262/