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

**URL:** <https://discourse.slimframework.com/t/strange-router-behavior-randomy-redirects-to-another-page/2129>\
**Category:** Questions\
**Created:** [January 2, 2018, 10:39am UTC](https://discourse.slimframework.com/t/strange-router-behavior-randomy-redirects-to-another-page/2129 "2018-01-02T10:39:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![d.gauthier](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/d.gauthier/32/584_2.png) [@d.gauthier](https://discourse.slimframework.com/u/d.gauthier)\
**Post date:** [January 2, 2018, 10:39am UTC](https://discourse.slimframework.com/t/strange-router-behavior-randomy-redirects-to-another-page/2129/1 "2018-01-02T10:39:40Z")

</div>

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 🙂 )

`index.php`

Container config :

```auto
    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 :

```auto
    $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.

```auto
    <?php
    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

```auto
    <?php
    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 ! 🙂

---

<div class="post-metadata">

**Author:** ![tstadler](https://avatars.discourse-cdn.com/v4/letter/t/aca169/32.png) [@tstadler](https://discourse.slimframework.com/u/tstadler)\
**Post date:** [January 2, 2018, 7:06pm UTC](https://discourse.slimframework.com/t/strange-router-behavior-randomy-redirects-to-another-page/2129/2 "2018-01-02T19:06:06Z")

</div>

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?
