How set dynamic database host names


Current below is my settings.php

 return [
     'settings' => [
         'displayErrorDetails' => true, // set to false in production
         'addContentLengthHeader' => false, // Allow the web server to send the content-length header
         // Renderer settings
         'renderer' => [
             'template_path' => __DIR__ . '/../templates/',
         // Monolog settings
         'logger' => [
             'name' => 'slim-app',
             'path' => isset($_ENV['docker']) ? 'php://stdout' : __DIR__ . '/../logs/app.log',
             'level' => \Monolog\Logger::DEBUG,
         "dbShard1" => [
             "host" => "localhost",
             "dbname" => "***",
             "user" => "***",
             "pass" => "***"

With this settings it works perfectly in the route when I called this e.g.

$selectQueryResult1 = $this->db->prepare($selectQuery1)

The issue now I will want to have many different database e.g. db1, db2; when I set new settings in the settings file I always get this error

/var/www/html/apiv1/vendor/slim/slim/Slim/Container.php(172): Slim\Container->get('db1')

How can I set and call those extra database settings?


Just noting I edited the post above to improve formatting.


Hi @newbie14

The question is what your server environment actually looks like and what you really want to achieve?

Depending on the environment and requirements, you could create a separate container entry for each database connection.

Don’t you also have to distinguish between a dev, testing, staging and prod server?

Could you please give us more information?


Hi Odan,
Actually I will have few shard. So depends on the user I need to point him to different shards. Thats why I need different db connection. So based on the shard id I will dynamically connect to different db.


In this case, you could implement a factory class that creates the required connection objects depending on a shared ID.