Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Dispatcher and beforeExecuteRoute - extraordinary effects

Hi again. According to that: https://olddocs.phalconphp.com/en/3.0.0/reference/tutorial-invo-2.html and that: https://olddocs.phalconphp.com/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?



129.1k
Accepted
answer

Favicon request?



3.0k

Good point!

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