1. Go to this page and download the library: Download apigopro/slim-database library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use Dotenv\Dotenv;
if (file_exists('/etc/myapp/.env')) {
Dotenv::createImmutable('/etc/myapp')->load();
}
use DI\Container;
use SlimDatabase\DatabaseConfiguration;
use SlimDatabase\AuthDatabaseConnection;
use SlimDatabase\AppDatabaseConnection;
use SlimDatabase\Middleware\CloseDatabaseConnectionsMiddleware;
$container = new Container();
$container->set(DatabaseConfiguration::class, function () {
return new DatabaseConfiguration();
});
$container->set(AuthDatabaseConnection::class, function (Container $c) {
return new AuthDatabaseConnection($c->get(DatabaseConfiguration::class)->authDb);
});
$container->set(AppDatabaseConnection::class, function (Container $c) {
return new AppDatabaseConnection($c->get(DatabaseConfiguration::class)->appDb);
});
use SlimDatabase\AppDatabaseConnection;
final class ListUsersAction
{
public function __construct(private readonly AppDatabaseConnection $db) {}
public function __invoke($request, $response, array $args)
{
$stmt = $this->db->get()->query('SELECT id, name FROM users');
// ...
}
}
use SlimDatabase\AuthDatabaseConnection;
final class LoginAction
{
public function __construct(private readonly AuthDatabaseConnection $db) {}
// Only this class (and anything else that legitimately needs it)
// should ever type-hint AuthDatabaseConnection.
}
bash
sudo apt install php8.3-pgsql # adjust for your installed PHP version
sql
-- PostgreSQL example
CREATE ROLE auth_user WITH LOGIN PASSWORD '...';
GRANT SELECT ON auth_credentials TO auth_user;
CREATE ROLE app_user WITH LOGIN PASSWORD '...';
GRANT SELECT, INSERT, UPDATE, DELETE ON users TO app_user;
-- deliberately no grants on auth_credentials for app_user
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.