How to do you get this dependency when trying to use it in a route? I tried $app->getContainer()->get(‘db’);
but I get this error: “Cannot use object of type App\Application\Settings\Settings as array”
The default “LoggerInterface::class” can be gotten that way, $app->getContainer()->get(‘LoggerInterface::class’) which is why I’m confused that I can’t get it on the ‘db’
('db') => function (ContainerInterface $c) {
$db = $c->get(SettingsInterface::class)['db'];
$pdo = new PDO($db['dsn'].':'.$db['database']);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
return $pdo;
}
$db = $app->getContainer()->get('db');
I know it’s been a long time since you answered your own question but I thought you might be able to help me
Turns out that it wasn’t that I couldn’t get it with $app->getContainer()->get('db');
it was that $db = $c->get(SettingsInterface::class)['db'];
didn’t work and it had to be $c->get(SettingsInterface::class)->get('db')
instead.
Also you can use $this->get('db')
instead of $app->getContainer()->get('db');