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 and beforeExecuteRoute - extraordinary effects

Hi again. According to that: https://olddocs.phalcon.io/en/3.0.0/reference/tutorial-invo-2.html and that: https://olddocs.phalcon.io/en/3.0.0/api/Phalcon_Mvc_Dispatcher.html I added to services.php:

    $di->set(
        "dispatcher",
        function () {
            $dispatcher = new \Phalcon\Mvc\Dispatcher();
                $eventsManager = new \Phalcon\Events\Manager();
                $eventsManager->attach(
                    'dispatch:beforeExecuteRoute',
                    new SecurityService()
                );
                $dispatcher->setEventsManager($eventsManager);

            return $dispatcher;
        }
    );

And of course my implementation in service:

   public function beforeExecuteRoute(\Phalcon\Events\Event $event, \Phalcon\Dispatcher $dispatcher)
    {
        $router = new \Phalcon\Mvc\Router();
        $router->add('/index/test')->setName('test');
        $router->handle();

        if ($router->wasMatched()) {
            //it performs well, according to my expectations
             file_put_contents('test1.txt', 'Well done!');
        }else{
        //this is sth extraterrestrial !!!
            file_put_contents('test2.txt', 'Phalcon secret parser here');

            echo 'I am in (|)';
            exit;
        }
}

Of course I call /index/test action by URL (to ensure that effect occurs at least 5 times, sometimes works in first time, I don't know why)

Could someone tell me, why both files apear when I call /index/test? How can I code when basic control structures don't work?



145.0k
Accepted
answer

Favicon request?



8.3k

Good point!

Browser always sends a request like /favicon.ico even if I don't have anything in head.