Questions about redirects

Hey there,

Still in the process of upgrading my app from Slim 2 to Slim 3 (and then 4).

Two questions:

  • Is there a difference between return $response->withRedirect($url, 301); and return $response->withStatus(301)->withHeader('Location', $url); ?
  • Is there a way to detect if $response contains a redirect? This would be useful inside middleware, where running $response = $next($request, $response); is not wanted if the code that came before it set a redirect on the response.

Thanks for the heads up.

I’ll answer my own questions.

From the source code:

  • withRedirect() includes this note “This method is not part of the PSR-7 standard”, but it works the same way: it also sets withHeader and (optionally) withStatus.
  • $response->isRedirect() is what I was looking for.