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

app in Lazy Collection

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?

edited Sep '16

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.

edited Sep '16

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...()
edited Sep '16

You can't do $app->something() in class method(controller for instance), it will just tell undefined variable $app

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.



79.0k
Accepted
answer
edited Sep '16

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...()