xmigas
xmigas

Reputation: 131

Redirecting with Slim PHP v 3.x

I have one route in my Slim PHP application. It goes like this: gallery.dev/roomPicture/RoomDetails/212 for example. I would like to redirect all this requests to route gallery.dev/login. Here is what I tried:

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

This redirects me to gallery.dev/roomPicture/RoomDetails/login multiple times, and then it throws an error.

Do you have some idea how can I solve it?

Thanks!

Upvotes: 0

Views: 1471

Answers (1)

Max P.
Max P.

Reputation: 5679

Redirection status code is 301. Also change url to absolute /login. Now it is relative to current path, so login is prepended to current path directory.

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

or use special method for it withRedirect($url, $status = null)

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

Upvotes: 4

Related Questions