Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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:

```php //mount group handler to main app $app->mount($MyCollection); ```

Let's say you have routes.php file where you define all your routes / collections.

```php //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.



42.2k
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:

```php //mount group handler to main app $app->mount($MyCollection); ```

Let's say you have routes.php file where you define all your routes / collections.

```php //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...()

```