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

DI Parameters

is there a way to pass parameters to DI? may be like this:

$di->set('db', function ($section = 'default') use ($config) {
    return new DbAdapter($config->database->{$section}->toArray());
});
// and call on any controller
$this->db('default')-> //... or
$this->db['default']-> //... or whatever


125.8k
Accepted
answer

Have you tried it? Looking at the documentiation, it would appear that get() and getShared() accept a second parameter, which is an array: https://docs.phalcon.io/en/latest/api/Phalcon_DI.html

My guess is your code would look like:

$DB = $this->di->get('db',['default']);

I don't know how that array of parameters works - so you'll have to write some test code to see how the parameters get passed.



16.3k

hi, thanks for your reply. ofcourse i have tried but i found nothing 'methods' worked so i posted to the forum to find the answer.

your code is my first test, anyway.

$DB = $this->di->get('db',['default']);


16.3k

accepted the answer because that's worked on 1.3.1 version ;-)