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 created a new project and I just created a login page. I can not access it because I get the following error message:

Fatal error: Uncaught Phalcon\Mvc\Dispatcher\Exception: Dispatcher has detected a cyclic routing causing stability problems in D:\workspace\yoni-hbjo\public\index.php:32 Stack trace: #0 [internal function]: Phalcon\Mvc\Dispatcher->_throwDispatchException('Dispatcher has ...', 1) #1 [internal function]: Phalcon\Dispatcher->dispatch() #2 D:\workspace\yoni-hbjo\public\index.php(32): Phalcon\Mvc\Application->handle() #3 {main} thrown in D:\workspace\yoni-hbjo\public\index.php on line 32

In my services.php :

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

$evManager->attach("dispatch:beforeException", function ($event, $dispatcher, $exception) {
    switch ($exception->getCode()) {
        case PhDispatcher::EXCEPTION_HANDLER_NOT_FOUND:
        case PhDispatcher::EXCEPTION_ACTION_NOT_FOUND:
            $dispatcher->forward(array(
                'controller' => 'index',
                'action' => 'show404'
            ));

            return false;
    }
});

$dispatcher = new PhDispatcher();
$dispatcher->setEventsManager($evManager);

return $dispatcher;
});

in my IndexController.php

class IndexController extends ControllerBase
{

public function indexAction()
{
    $this->view->setLayout('login');
}

public function show404Action()
{
    $this->view->setLayout('login');
    $this->response->setStatusCode(404, "Not Found");
    $this->view->pick('index/404');
}
}

In my ControllerBase :

public function beforeExecuteRoute($dispatcher)
{
    $isValid = true;

    // check auth
    if ($dispatcher->getControllerName() !== 'index') {
        $isValid = false;

        $this->view->disable();

        $this->response->setStatusCode(401, 'Vous devez vous identifier pour accéder à votre environnement.');

        if (!$this->request->isAjax()) {
            $this->response->redirect('index');
        }
    }

    return $isValid;
}

and my index.php

<?php

define('APP_PATH', realpath('..'));

/**
 * Define APPLICATION_ENV (DEV,STAGING,PREPROD,PROD)
 *
 * @var APPLICATION_ENV string
 */
defined('APPLICATION_ENV') || define('APPLICATION_ENV', (isset($_SERVER['APPLICATION_ENV']) ?     $_SERVER['APPLICATION_ENV'] : 'PROD'));

/**
 * Read the configuration
 */
$config = include APP_PATH . "/app/config/" . strtolower(APPLICATION_ENV) . ".config.php";

/**
 * Read auto-loader
 */
include APP_PATH . "/app/config/loader.php";

/**
 * Read services
 */
include APP_PATH . "/app/config/services.php";

/**
 * Handle the request
 */
$application = new \Phalcon\Mvc\Application($di);

echo $application->handle()->getContent();

Have you an idea?

Thank you

Vince



32.3k
Accepted
answer

Hi @vleboulanger I think the problem is your ControllerBase. Do something like this


public function beforeExecuteRoute($dispatcher)
{
    // if don't have a session, only can access to index controller
    if ($this->auth->isLoggedIn() === false && $dispatcher->getControllerName() !== 'index') {
        $this->view->disable();

        $this->response->setStatusCode(401, 'Vous devez vous identifier pour accéder à votre environnement.');

        if (!$this->request->isAjax()) {
            $this->response->redirect('/'); // go home or /login or what you want
        }

        return false;
    }
}

Try that and then comment us the result

Good luck