Phalcon Error: Dispatcher has detected a cyclic routing causing stability problems

Issue

Saat membuat website dengan framework Phalcon, Anda menemukan error berikut saat mengakses halaman.

Dispatcher has detected a cyclic routing causing stability problems
#0 [internal function]: Phalcon\Mvc\Dispatcher->_throwDispatchException('Dispatcher has ...', 1)
#1 [internal function]: Phalcon\Dispatcher->dispatch()
#2 /home/user/public_html/public/index.php(29): Phalcon\Mvc\Application->handle()
#3 {main}

Solution

Saat mengakses halaman yang error, Dispatcher akan menampilkan error tersebut apabila konfigurasi action tidak ditentukan atau konfigurasi pada controller yang belum sesuai.

$di->set(
    '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' => 'Error',
                                'action'     => 'index',
                            )
                        );
                        return false;
                }
            }
        );
        $dispatcher = new PhDispatcher();
        $dispatcher->setEventsManager($evManager);
        return $dispatcher;
    },
    true
);

Untuk itu agar dapat melihat error yang sebenarnya pada halaman yang diakses maka Anda dapat menghapus atau comment (//) bagian kode dispatcher seperti contoh diatas.

Jika sudah maka error yang tampil akan berbeda.

Atau Anda dapat mengubah dispatcher menjadi seperti berikut.

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

    // Attach event handlers, if any

    $dispatcher = new Phalcon\Mvc\Dispatcher();
    $dispatcher->setEventsManager($eventsManager);

    return $dispatcher;
});