php - 使用不同的 HTTP 方法重定向

标签 php httpresponse router slim slim-3

我有一个端点来更新给定实体,响应应该是更新后的实体。我因此尝试了以下操作,但是,重定向是另一个 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/

相关文章:

ruby-on-rails - ruby http 响应脚本发送大量 gzip 压缩内容

json - 为什么我从这个 http 请求中得到空响应

iphone - 如何将外部IP转发到多个本地服务器?

javascript - Vue 中的 Spotify 验证 : getting URL params

angular - Angular 5路由中的queryParams列表为空

javascript - 使用ajax编辑表行数据并保存在数据库中

php - 添加或修改数组的一部分作为 mysql 字符串

php - 使用 mysql/php 从数据库中选择行

php - Symfony-Doctrine 博客中用于嵌套评论的 Twig 循环

java - 创建http响应作为mockito的返回值