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.

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


53.4k
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: http://docs.phalconphp.com/en/latest/api/Phalcon%5DI.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.



10.2k

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']);


10.2k

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