Slim 4 dependencies how to set a container?

I have a Slim 3 REST API I am trying to move to slim 4, but I am confused about the dependencies.php. In slim 3 I use pdo like this:

// MySQL PDO
$container['pdo'] = function ($c) {
	$settings = $c->get('settings')['mysql'];
	$dsn = "mysql:dbname=" . $settings['dbname'] . ";host=". $settings['host'] .";charset=utf8";
	$pdo = new PDO($dsn, $settings['username'], $settings['password'], array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));
	$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
	return $pdo;
};

I tried changing this for slim 4 but without luck. Can someone point me to the right direction?
slim 4 depnedencies.php

// Create Container using PHP-DI
$container = new Container();

// Set container to create App with on AppFactory
AppFactory::setContainer($container);
$app = AppFactory::create();

// MySQL PDO
$container->set('pdo', function ($c) {
    $settings = $c->get('settings')['mysql'];
$dsn = "mysql:dbname=" . $settings['dbname'] . ";host=". $settings['host'] .";charset=utf8";
$pdo = new PDO($dsn, $settings['username'], $settings['password'], array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $pdo;
});

Hi @leossmith

Your container has no definition for ‘settings’.

I would not recommend to call $container->set(...).

You can find more examples here:

Thank you, I ended up with a working copy:

$containerBuilder->addDefinitions([
    
    'pdo' => function (ContainerInterface $c) {
        $settings = $c->get('settings')['mysql'];
        $dsn = "mysql:dbname=" . $settings['dbname'] . ";host=". $settings['host'] .";charset=utf8";
        $pdo = new PDO($dsn, $settings['username'], $settings['password'], array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        return $pdo;
    } 
]);

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 :slight_smile:

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

2 posts were split to a new topic: DI Container and settings