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

Detect controller instance

Hi, i use a micro application and i want detect current calling controller instance. i use before method

$app->before(function () use ($app) {
// HERE. I WANT CHECK HANDLER INSTANCE. HOW?
})

i know that i use

$app->getActiveHandler();
$handler[0]->getDefinition(); 

but getDefinition return path to class, not instance ;/ What can I do?



85.5k

https://olddocs.phalcon.io/en/3.0.0/api/Phalcon_Mvc_Dispatcher.html

basically you can call dispatcher methods in any controller

$this->dispatcher->getActiveController();

i think this is what you need ? i am not 100% sure tho



1.3k
edited Nov '17

thanks for answer but i can not use $this in function pointer in before() ;/

if i use $app->dispatcher->getActiveController() i get NULL

Important attention. i use LazyLoader

There is no dispatcher in micro app.

$app->getActiveHandler(); This doesn't return just controller instance?



1.3k
edited Nov '17

getActiveHandler() return an array. First argument is LazyLoader(instance) and second parameter is the called method.

LazyLoader has getDefinition method but this method return class path(NOT INSTANCE!).

I can do it:

$lazyLoader->getDefinition() === App\Controller\TestController::class

but not

$lazyLoader->getDefinition() instanceof App\Controller\TestController // <- i want like this

I've just test it and that work fine without adding anything to catch ... What version of php / phalcon do you use ? Can you provide more code to check. resume examples for students

edited May '20

Thank you so much for problem solution. Maybe some update for code here? $app->getActiveHandler(); - works with LazyLoader.