When using closures in routes, they are bound to the $app. Is there a way to access the $app when using a Lazy Collection instead?
|
Sep '16 |
5 |
514 |
0 |
Hmmm i guessing no. But maybe you need just di in micro ? Micro is using still di to store all services. So for example if you want session from $app. Then you can just do Di::getDefault()->get("session")
i think or even $this->session
should work.
Well though it's possibe to somehow bind method from object of one class to another using reflectionmethod but this is really bad idea.
Sure it is. How do you define your \Phalcon\Mvc\Micro\Collection
?
Certainly you need to:
//mount group handler to main app
$app->mount($MyCollection);
Let's say you have routes.php
file where you define all your routes / collections.
//instantiate new Micro app
$app = new \Phalcon\Mvc\Micro(self::getDI()); //di is being injected from services definition into this class, which extends \Phalcon\Di\Injectable
// Include Application definition
require APP_PATH . '/app.php';
$clips = new MicroCollection();
$clips->setHandler('ClipsController'); //lazy loading is set as default
$clips->setPrefix('/get');
$clips->post('/clip/{num}', 'getClips', 'getClipsRoute');
$app->mount($clips);
//Access app here
//$app->something...()
Thank you for your answer. What I meant, based on your example, would be how to access the $app in the getClips of the ClipsController because I have a custom Di that I would like to use.
Sure it is. How do you define your
\Phalcon\Mvc\Micro\Collection
?Certainly you need to:
//mount group handler to main app $app->mount($MyCollection);
Let's say you have
routes.php
file where you define all your routes / collections.//instantiate new Micro app $app = new \Phalcon\Mvc\Micro(self::getDI()); //di is being injected from services definition into this class, which extends \Phalcon\Di\Injectable // Include Application definition require APP_PATH . '/app.php'; $clips = new MicroCollection(); $clips->setHandler('ClipsController'); //lazy loading is set as default $clips->setPrefix('/get'); $clips->post('/clip/{num}', 'getClips', 'getClipsRoute'); $app->mount($clips); //Access app here //$app->something...()
Since controllers are extending Injectable you can access any service using $this->session
for example in method controller. No need for accessing $app
there.
Simple as:
$this->db...
$this->router...
$this->yourCustomService->methodCall();
Thank you for your answer. What I meant, based on your example, would be how to access the $app in the getClips of the ClipsController because I have a custom Di that I would like to use.
Sure it is. How do you define your
\Phalcon\Mvc\Micro\Collection
?Certainly you need to:
//mount group handler to main app $app->mount($MyCollection);
Let's say you have
routes.php
file where you define all your routes / collections.//instantiate new Micro app $app = new \Phalcon\Mvc\Micro(self::getDI()); //di is being injected from services definition into this class, which extends \Phalcon\Di\Injectable // Include Application definition require APP_PATH . '/app.php'; $clips = new MicroCollection(); $clips->setHandler('ClipsController'); //lazy loading is set as default $clips->setPrefix('/get'); $clips->post('/clip/{num}', 'getClips', 'getClipsRoute'); $app->mount($clips); //Access app here //$app->something...()