Strange Router behavior ("randomy" redirects to another page)

Hello people !

I’ve been using SlimFramework V3 for a new project at work, works ALMOST like a charm so far, I’ve encountered one issue which I struggle debugging :

I have several routes, some of them are attached with a middleware to know if the user is authenticated or not, if not it redirects to the login page /login.
When I have a 404 I redirect to /route-c.

I have a menu with several links redirecting to pages :

/route-a and route-b, sometimes when I navigate to /route-a it “randomly” redirects me to /login even if I am authenticated. I thought the problem was the middleware but it’s actually pretty straighforward so I do not think this is the problem because :
It happened to me again with another route, this time it didn’t redirect me to /login but to another route, let’s say /route-c.

I thought I messed up my routes and try to identify the cause, then I tried on another browser because why not and the behavior is not the same : sometimes the app perfectly works on one browser when it randomly redirects to wrong pages on another. And vice-verca.

So basically at one point my routes redirect me to others routes and I don’t know why, and it randomly occurs.

Here’s the code I use : ( I can give more if needed :slight_smile: )


Container config :

    use Slim\Container as SlimContainer;
    use Slim\Handlers\Strategies\RequestResponseArgs;

    $container = new SlimContainer([
        "settings" => [
            "displayErrorDetails" => true
        "foundHandler" => function () {
            return new RequestResponseArgs();
        "notFoundHandler" => function () {
            return function ($req, $res) {
                return $res->withRedirect('/route-c');

My routes :

    $app->get("/login", "AuthenticateController:onGetLogin");
    $app->post("/login", "AuthenticateController:onPostLogin");

    $app->group("/app", function () {
        $this->get("/route-a", "ControllerA:onGetRouteA");
        $this->get("/route-b", "ControllerB:onGetRouteB");
    })->add(new IsAuthenticatedMiddleware($container->get("SessionService");

    $app->group("/route-c", "ControllerC:onGetRouteC")
    ->add(new IsAuthenticatedMiddleware($container->get("SessionService");

IsAuthenticatedMiddleware.php -> attached to routes where I want to check if an user is logged in.

    namespace App\Middlewares;

    use Psr\Http\Message\RequestInterface as Request;
    use Psr\Http\Message\ResponseInterface as Response;

    class IsAuthenticatedMiddleware
        private $session;

        public function __construct($session)
            $this->session = $session;

        public function __invoke(Request $request, Response $response, callable $next)
             if (!$this->session->get("payload", false)) {
                 return $response->withRedirect('/login');
            return $next($request, $response);

SessionService.php -> injected in the middleware

    namespace App\Services;
    use SlimSession\Helper;

    class SessionService
        public function __invoke()
            return function ($c) {
                return new Helper();

I don’t know if you ever encountered this issue, tried to dig on google but couldn’t find any solution or related topic.

If you need more infos or need clarifications…

Thanks ! :slight_smile:

Why do you return a new Helper in the __invoke method of SessionService, but SessionService does not implement a get method?

Did you add \Slim\Middleware\Session to your apps middleware?

When do you add “payload” to the session?