Skip to content

How to connect to multiple Databases – Slim Framework.

Last updated on December 2, 2022

In the earlier post, I wrote a tutorial on how to connect to the database in a slim framework. In this post, I would like to show you, how you can connect to multiple databases in the same slim framework application.
For a single database connection, we have created an instance of the database and injected the database object into the container using dependency injection. We gonna follow the same principle to use multiple database connections, the only difference is, we will use the different keys in the container array.

We are going to modify the following files –
src/settings.php – It holds application-level configuration details, it’s just an associative array.
src/dependencies.php – It holds application-level dependencies

How to connect to multiple Database

Database configuration settings

Open you settings.php and update with your preferred database configurations,

 [
        'displayErrorDetails' => true, // set to false in production
 
        // Renderer settings
        ....
        ....    
 
        // Monolog settings
        ....
        ....
 
        // Database connection settings
        "db" => [
            "host" => "localhost",
            "dbname" => "slim3",
            "user" => "root",
            "pass" => ""
        ],

       // second database connection settings
        "blog_db" => [
            "host" => "localhost",
            "dbname" => "blog",
            "user" => "root",
            "pass" => ""
        ],
    ],
];

Database configuration

There are many database libraries available for PHP, but this example uses PDO. Now open your src/dependencies.php file and configure the database library as shown below. you can use your own libraries by adapting the example.

// DIC configuration
$container = $app->getContainer();
 
...
...
...
 
// PDO database library
$container['db'] = function ($c) {
    $settings = $c->get('settings')['db'];
    $pdo = new PDO("mysql:host=" . $settings['host'] . ";dbname=" . $settings['dbname'],
        $settings['user'], $settings['pass']);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
    return $pdo;
};

// second Database connection
$container['blog_db'] = function ($c) {
    $settings = $c->get('settings')['blog_db'];
    $pdo = new PDO("mysql:host=" . $settings['host'] . ";dbname=" . $settings['dbname'],
        $settings['user'], $settings['pass']);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
    return $pdo;
};

In the above code, we are injecting database objects into the container using dependency injection, called DB and blog_db. So now you can access your database objects with $this->db and $this->blog_db in your application, and using this object you can perform operations like create, update, delete, find..etc on the database.

0 0 votes
Article Rating
Subscribe
Notify of
guest

3 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments