使用 Slim PHP v 3.x 重定向

Redirecting with Slim PHP v 3.x

我的 Slim PHP 应用程序中有一条路线。它是这样的:例如 gallery.dev/roomPicture/RoomDetails/212。我想将所有这些请求重定向到路由画廊。dev/login。这是我尝试过的:

$app->get('/roomPicture/RoomDetails/{id}', function (Request $request, Response $response, $args){
 //some code
 return $this->response->withStatus(200)->withHeader('Location', 'login');

这将我重定向到图库。dev/roomPicture/RoomDetails/login 多次,然后抛出错误。

你知道我该如何解决吗?

谢谢!

重定向状态代码为 301。同时将 url 更改为绝对 /login。现在它是相对于当前路径的,所以 login 被添加到当前路径目录之前。

return $this->response->withStatus(301)->withHeader('Location', '/login');

或使用特殊方法withRedirect($url, $status = null)

return $this->response->withRedirect('/login');