Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Dispatcher has detected a cyclic routing causing stability problems

Hello, I do not understand the problem

my app throws: Dispatcher has detected a cyclic routing causing stability problems

in Security plugin

public function beforeDispatch(Event $event, Dispatcher $dispatcher)
    {
        $auth = $this->session->get('auth-identity');

        if (!$auth) {
            $dispatcher->forward(array(
                'controller' => 'session',
                'action' => 'login'
            ));
            return false;
        }
    }

in services

$di->set('dispatcher', function() use ($di) {
    $eventsManager = $di->getShared('eventsManager');

    $security = new Security($di);

    /**
     * We listen for events in the dispatcher using the Security plugin
     */
    $eventsManager->attach('dispatch', $security);

    $dispatcher = new Dispatcher();
    $dispatcher->setDefaultNamespace('App\Controllers');
    $dispatcher->setEventsManager($eventsManager);
    return $dispatcher;
});


20.3k

It seems that $this->session->get('auth-identity') always returns false or null. So it's cyclic routing to "session/login"



85.0k
Accepted
answer

As @Agent-J said, you need to avoid that the session/login be forwarded itself:

if (!$auth) {
    if ($dispatcher->getControllerName() != 'session') {
        $dispatcher->forward(array(
            'controller' => 'session',
            'action' => 'login'
        ));
        return false;
    }
    return;
}