无法使用状态重定向

编程入门 行业动态 更新时间:2024-10-13 04:25:44
本文介绍了无法使用状态重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

使用

调用控制器 $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 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!

更多推荐

无法使用状态重定向

本文发布于:2023-07-14 20:02:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1106697.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重定向   状态

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!