无法使用状态重定向 [英] Can't redirect with status

查看:15
本文介绍了无法使用状态重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用

调用控制器
$this->get( '/read/{slug}', RibSrcAppsBlogBlogControllersIndexController::class . ':index' );

我在里面试过了:

return $response->withStatus( 404 )->withRedirect( '/message' );

return $response->withRedirect( '/message', 404 );

但返回的响应总是代码为200。 如何执行404?

推荐答案

您不能使用404状态代码重定向。仅3xx对重定向有效。当浏览器收到Location:头时,它会向给定的URL发出新的请求。这意味着您可以重定向到返回404的路由。

$app->get("/test", function ($request, $response, $arguments) {
    return $response->withRedirect("/message");
});

$app->get("/message", function ($request, $response, $arguments) {
    return $response->write("Oh noes!")->withStatus(404);
});

上面的代码将重定向到带有404状态代码的响应。

$ curl --include --location http://0.0.0.0:8080/test

HTTP/1.1 302 Found
Host: 0.0.0.0:8080
Date: Sun, 26 Mar 2017 04:53:05 +0000
Connection: close
X-Powered-By: PHP/7.1.2
Content-Type: text/html; charset=UTF-8
Location: /message

HTTP/1.1 404 Not Found
Host: 0.0.0.0:8080
Date: Sun, 26 Mar 2017 04:53:05 +0000
Connection: close
X-Powered-By: PHP/7.1.2
Content-Type: text/html; charset=UTF-8

Oh noes!

这篇关于无法使用状态重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆