2017-03-18 101 views
0

重定向我在Slim PHP應用程序中有一條路由。它是這樣的:例如,gallery.dev/roomPicture/RoomDetails/212。我想重定向所有這些請求路由gallery.dev/login。這裏是我的嘗試:使用Slim PHP v 3.x

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

這重定向我gallery.dev/roomPicture/RoomDetails/login多次,然後拋出一個錯誤。

你有一些想法,我該如何解決它?

謝謝!

回答

2

重定向狀態碼是301.另請將url更改爲絕對/login。現在它是相對於當前路徑,所以login被預置到當前路徑目錄。

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

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

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