How do you handle multi-database configurations in Slim Framework?

How do you handle multi-database configurations in Slim Framework?

To handle multi-database configurations in the Slim Framework, follow these minimal steps:

1. Install Database Library:

Ensure you have a database library installed (e.g., PDO, Eloquent, or Doctrine). For this example, we will use PDO.

2. Configure Database Connections:

Define multiple database configurations in your settings file, typically located at config/settings.php.

Example

<?php
return [
    'db' => [
        'database1' => [
            'driver' => 'mysql',
            'host' => 'localhost',
            'dbname' => 'database1_name',
            'user' => 'db1_user',
            'pass' => 'db1_password',
        ],
        'database2' => [
            'driver' => 'mysql',
            'host' => 'localhost',
            'dbname' => 'database2_name',
            'user' => 'db2_user',
            'pass' => 'db2_password',
        ],
    ],
];
?>

3. Create Database Connection Class:

Create a database connection class that can manage multiple connections. For example, create a Database.php class.

Example

<?php
namespace App;

use PDO;

class Database {
    protected $connections = [];

    public function __construct($settings) {
        foreach ($settings['db'] as $key => $config) {
            $this->connections[$key] = new PDO(
                "{$config['driver']}:host={$config['host']};dbname={$config['dbname']}",
                $config['user'],
                $config['pass']
            );
        }
    }

    public function getConnection($dbName) {
        return $this->connections[$dbName] ?? null;
    }
}
?>

4. Register Database Service in Slim Container:

In your index.php, register the Database class with Slim’s dependency injection container.

Example

<?php
$app = new \Slim\App;

$settings = require __DIR__ . '/config/settings.php';
$container = $app->getContainer();

$container['database'] = function($container) use ($settings) {
    return new \App\Database($settings);
};
?>

5. Use the Database Connections:

In your route handlers or controllers, you can now retrieve the desired database connection from the container and use it.

Example

<?php
$app->get('/data1', function ($request, $response) {
    $db = $this->database->getConnection('database1');
    $stmt = $db->query('SELECT * FROM table1');
    $data = $stmt->fetchAll(PDO::FETCH_ASSOC);
    return $response->withJson($data);
});

$app->get('/data2', function ($request, $response) {
    $db = $this->database->getConnection('database2');
    $stmt = $db->query('SELECT * FROM table2');
    $data = $stmt->fetchAll(PDO::FETCH_ASSOC);
    return $response->withJson($data);
});
?>

6. Testing:

Run your Slim application and access the defined routes (/data1 and /data2) to test the connections to the respective databases.

By following these steps, you can efficiently manage multiple database configurations within your Slim Framework application.

Related Questions & Topics