We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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;
});


29.4k

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



98.9k
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;
}