In Slim 3, I could do:
$app->group(’’’, function () use ($app) {
$app->get(’/login’,‘LoginController:get’)->setName(‘login’);
$app->post(’/login’,‘LoginController:post’);
})->add(new Guest($container));
But now with Slim 4 I have migrated everything, especially Guest:
$response = $handler->handle($request);
if (Sessions::isAuthenticated()) {
return $response->withRedirect("URL");
}
return $response;
And I get an error message when I try to make a POST request.
Call to undefined method Slim\Psr7\Response::withRedirect()
But the middleware works perfectly with a GET request
That’s because Slim-Psr7 does not implement that method anymore. You’ll need to use the Slim-Http decorators on top of Slim-Psr7.
Also, don’t use $app
to add routes within a group. You need to use the RouteCollectorProxy
like so:
$app->group(function (RouteCollectorProxy $group) {
$group->get(...);
});
The problem is that I already use Slim-Http, $request->withRedirect() works very well with a GET request but not with a POST request
I need to see more code in order to be able to give you an answer. If you could gist something up with your index.php, GET/POST routes, etc.
If I am logged in and go to /login it redirects and no errors.
If I am disconnected and I go to /profile it redirects and no errors.
If I go to login and make a POST request, then I will get the error “Call to undefined method Slim\Psr7\Response::withRedirect()”
If I remove $group->post(’/login’,‘LoginController:post’); from the group, everything works perfectly but even players already connected can access this page with a POST request.