For Slim v3.x:
Use $response->withStatus(302)->withHeader('Location', $url);
instead of $app->redirect();
In Slim v2.x one would use the helper function $app->redirect(); to trigger a redirect request. In Slim v3.x one can do the same with using the Response class like so (see the following example)[1].
Use pathFor()
instead of urlFor()
:
urlFor() has been renamed pathFor() and can be found in the router object.
Also, pathFor() is base path aware[2].
Example:
$app->get('/', function ( $request, $response, $args ) use ( $app ) {
$url = $this->router->pathFor('loginRoute');
return $response->withStatus(302)->withHeader('Location', $url);
});
Note: additional parameters can be supplied by passing an associative array of parameter names and values as a second argument of pathFor()
like: $this->router->pathFor('viewPost', ['id' => 1]);
.
The router’s pathFor() method accepts two arguments:
- The route name
- Associative array of route pattern placeholders and replacement values[3]
References:
- Changed Redirect
- urlFor() is now pathFor() in the router
- Route names