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

IndexController passes twice

My project has a serious bug.

my phalcon project operate with docker for mac.

so, If you go to https://{my_host_name}/ ,IndexController passes twice .For example in the following cases, two new users will be added in DB. What's happen!?

and, sometimes IndexController passes three times as soon as docker-compose start completes.

Is this due to the dispatcher or phalcon's version or docker's version?

My controller was described below.

class IndexController extends Controller
{
    /**
     * GET /
     */
    public function indexAction()
    {
        //  it's test
        $user = new User();
        $user->create();
    }

    public function afterExecuteRoute()
    {
        if (!$this->dispatcher->isFinished()) {
            return;
        }

       // css and js roading
        $cssHeaderCollection = $this->assets->collection("cssHeader");
        foreach (self::CSS_LIST as $css) {
            $cssHeaderCollection->addCss($css . "?" . self::VERSION);
        }
        $jsHeaderCollection = $this->assets->collection("jsHeader");
        foreach (self::JS_LIST as $js) {
            $jsHeaderCollection->addJs($js . "?" . self::VERSION);
        }
    }

    public function beforeExecuteRoute(){

        // user_setting
        $this->user = Sentinel::check();
        if ($this->user) {
            $this->role = Services::getService('User')->getRole($this->user->id);
        }
        $this->view->user = $this->user;
        $this->view->role = $this->role;
    }
}

And, My di's was described below.

$di['router'] = function () {
    $router = new Phalcon\Mvc\Router();
    $router->setDefaultModule('frontend');
    $router->setDefaultNamespace('Application\Frontend\Controllers');
    $router->setUriSource(\Phalcon\Mvc\Router::URI_SOURCE_SERVER_REQUEST_URI);
    $router->removeExtraSlashes(true);
}

$di['dispatcher'] = function () {

        $eventsManager = new Manager();
        $eventsManager->attach('dispatch:beforeException', function ($event, $dispatcher, $exception) {
            if ($exception->getMessage() && (getenv("MODE") == "development" || getenv("MODE") == "staging")) {
                var_dump($exception->getMessage());
            }
            switch ($exception->getCode()) {
                case Dispatcher::EXCEPTION_HANDLER_NOT_FOUND:
                case Dispatcher::EXCEPTION_ACTION_NOT_FOUND:
                    $dispatcher->forward([
                        'namespace' => 'Application\Frontend\Controllers',
                        'module' => 'frontend',
                        'controller' => 'errors',
                        'action' => 'show404'
                    ]);
                    return false;
                default:
                    if (getenv("MODE") == "production") {
                        $dispatcher->forward([
                            'namespace' => 'Application\Frontend\Controllers',
                            'module' => 'frontend',
                            'controller' => 'errors',
                            'action' => 'show500'
                        ]);
                    }
                    return false;
            }
        });
        $dispatcher = new \Phalcon\Mvc\Dispatcher();
        $dispatcher->setEventsManager($eventsManager);
        $dispatcher->setDefaultNamespace('Application\Frontend\Controllers');

        return $dispatcher;
    };


77.7k
Accepted
answer
edited Oct '17

Create your favicon.ico file, so when a browser requests it you actually serve that instead of the index page.

Or write more strict rewrite rules in your web server



12.1k

I think the matter is not in docker neither in the dispatcher, but in your bootstrap. You are calling something twice



2.5k

Create your favicon.ico file, so when a browser requests it you actually serve that instead of the index page.

Or write more strict rewrite rules in your web server

Sorry for replying so late.

I realized that What you say is right.

Thank you a lottttttttttttttttt!!!!