Hi everyone!
I can't find answer in docs how to define dynamic service during runtime. First of all, what I need to achieve:
I need to use the outsite API class that in the constructor accepts parameters, like "personalApiKey", that are read from the database after successful login to the application. So I can create object of this class only after login action. But then I want to use this class as Phalcon service in different places of the application.
So how can I do that?
Now I'm trying to define this service as empty object before "$application->handle()" and then redefine with proper constructor after login but I get some errors like "service not defined" or service is empty object. I tried to use shared service but it did not help. Maybe problem is in geting proper DI() object?
My code: index.php
$di = new FactoryDefault();
$di->setShared('Api', function() // I tried $di->set(); and $di->setService() but did not help {
});
AuthControler.php
$Api = new Api(array('apiKey' => '...')); $this->di->setShared('Api', $Api); // I tried $di = Phalcon\DI::getDefault(); and $di = $this->getDI(); but did not help
OtherControler.php
$Api = $this->di->getService('Api'); // error or empty object, I tried $di = Phalcon\DI::getDefault(); and $di = $this->getDI(); but did not help
I begin to lose my senses... please help :/