Reputation: 186
I am having a redirect issue with slim v3.11.0. When I call for a redirect in a route or from middleware it works as expected. However, when I call it from my controller it doesn't redirect nor error out. Any help would be greatly appreciated! Thanks!
*Ref https://www.slimframework.com/docs/v3/start/upgrade.html#changes-to-redirect
route example (works)
$app->get('/login/', function ($request, $response, $args) use($app) {
return $response->withRedirect('/new-url');
});
middleware example (works)
$auth = function ($request, $response, $next) {
if (!isset($_SESSION['Account'])) {
return $response->withStatus(302)->withHeader('Location', '/new-url/');
}
};
controller example (no worky)
$app->get('/login/', function ($request, $response, $args) use($app) {
return (new Login($app))->TestLoginRedirect();
});
....
class Login {
protected $App;
public function __construct($app){
$this->App = $app;
}
public function TestLoginRedirect(){
return $this->App->getContainer()->response->withRedirect('/new-url');
}
}
Other redirect snippet tries
return $this->App->getContainer()->response->withStatus(301)->withHeader('Location', '/new-url/');
return $this->App->redirect('/', '/new-url/');
Upvotes: 0
Views: 491
Reputation: 3409
In your controller example the route callback must return the response object, but it's not returning anything. It should be changed to:
$app->get('/login/', function ($request, $response, $args) use($app) {
return (new Login($app))->TestLoginRedirect();
});
Upvotes: 2