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

Micro\Collection setHandler lazy loading

$collection = new Phalcon\Mvc\Micro\Collection();

$collection->setHandler(new API\Service\Sample($di));

I need to load the Sample class if it is really needed, for that i tried lazy loading in setHander but the i can't pass the $di object. Below is the code to make it as lazy loading

$collection->setHandler('API\Service\Sample', true);

thanks in advance

edited Mar '16

You might want to make your class extend from Phalcon\Di\Injectable:

<?php

namespace API\Service;

use Phalcon\Di\Injectable;

class Sample extends Injectable
{
   public function something()
   {
       $post = $this->request->getPost();
   }
}
edited Mar '16

In order to access services container from your controller, you can also do the following.

class Sample extends BaseController // or just \Phalcon\Mvc\Controller
{
//access services
 $this->request->getPost();
 $this->db->connect();
 ...
}