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

Lazy Loading object not loading

I'm trying to get the name of the active handler for an incoming request to my Phalcon\Mvc\Micro application so I can get annotations for it. I'm lazy loading the Handler Collections so (to my understanding) this should be initialized as soon as it is first accessed. Therefore, I'm trying to get the name of the controller and the name of the handling function like this:

$annotations = $app->annotations->getMethod(
    $app->getActiveHandler()[0],    //the required controller
    $app->getActiveHandler()[1]     //the required method
);

However, this is not working. When I run:

print_r($app->getActiveHandler());

I get the following output:

Array
(
    [0] => Phalcon\Mvc\Micro\LazyLoader Object
        (
            [_handler:protected] => 
            [_definition:protected] => UsersController
        )

    [1] => getAll
)

The function is correctly loaded ("getAll") but the controller isn't. When I turn off lazy loading for this handler and do the same thing, I get:

Array
(
    [0] => UsersController
    [1] => getAll
)

Am I doing something wrong? Is there some way to manipulate the LazyLoader Object to get the data I want? I tried to figure this out from the documentation but I wasn't able to.

I could just turn off lazy loading, but my API has lots of different handlers that I would prefer not to load when not used.

Any help would be greatly appreciated!

edited Feb '14

I've been trying different approaches, but I still haven't figured it out. For the time being I'm using this (really ugly) work-around to get the controller name from the LazyLoading object:

if(is_object($requestedController)){
    $temp = explode("\n", print_r($requestedController, TRUE));
    $temp = explode("=>", $temp[3]);
    $requestedController = str_replace(' ', '', $temp[1]);
}

I hope someone can help me solve the problem so I don't have to commit this crime against nature to get the name of a lazy loaded controller.

I have the same problem. Does anyone know how to solve this??

edited Aug '16

Reflection is your friend, use object checking where necessary

$handler=$application->getActiveHandler();

$reflector = new \ReflectionClass($handler[0]);

$annotations=$application->annotations->getMethod($reflector->getName(),$handler[1]);