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

Router::wasMatched() always return false

Hey, guys!

I created an abstract class with some static methods. In one of these, I get the DI calling \Phalcon\DI::getDefault(). But when I get the router and call wasMatched(), it always returns false.

Like this:

abstract class Utils
{
    public static function isMenuAtivo($routeName, $params = array())
    {          
        $di = \Phalcon\DI::getDefault();

        if ($di->getRouter()->wasMatched()) { // always returns false
            ...
        }

        return false;
    }
}

In any controller, the method returns the expected result. Anyone knows what I'm doing wrong? I'm using phalcon 1.3.2.

Thanks.



2.1k

Router was matched is used to detect if the route provided match the controller

https://docs.phalcon.io/en/latest/api/Phalcon_Mvc_RouterInterface.html

If you called the util class from any controller, it returns true because it matched the controller. Its a chicken and egg issue.

Router can only match something when you call upon the route. Eg: localhost/controller/action // this will be a valid route. And thus when it triggers your util class, because the controller is extends it, it becomes valid. because it was matched my your /controller/action

however if you attempt to call it from anywhere that has no route, or not part of the http string, den it is not matched. because it isnt matched. =d