I’m new to slim.
Created a new slim app with composer create-project slim/slim-skeleton:dev-master helloSlim
I’m using docker-compose to run the development server and my database is hosted in heroku
I’m trying to setup postgres database with slim and I keep getting this error. Can’t figure out the error.
I followed the blog post. https://odan.github.io/2019/11/05/slim4-tutorial.html
{
"statusCode": 500,
"error": {
"type": "SERVER_ERROR",
"description": "could not find driver"
}
}
Here’s what my settings.php looks like
<?php
declare(strict_types=1);
use App\Application\Settings\Settings;
use App\Application\Settings\SettingsInterface;
use DI\ContainerBuilder;
use Monolog\Logger;
return function (ContainerBuilder $containerBuilder) {
// Global Settings Object
$containerBuilder->addDefinitions([
SettingsInterface::class => function () {
return new Settings([
'displayErrorDetails' => true, // Should be set to false in production
'logError' => false,
'logErrorDetails' => false,
'logger' => [
'name' => 'slim-app',
'path' => isset($_ENV['docker']) ? 'php://stdout' : __DIR__ . '/../logs/app.log',
'level' => Logger::DEBUG,
],
'db' => [
'driver' => $_ENV['DB_DRIVER'],
'host' =>$_ENV['DB_HOST'],
'port' =>$_ENV['DB_PORT'],
'database' =>$_ENV['DB_NAME'],
'username' =>$_ENV['DB_USERNAME'] ,
'password' =>$_ENV['DB_PASSWORD'],
]
]);
}
]);
};
I adedd the PDO::class into the dependency
<?php
// ...
return function (ContainerBuilder $containerBuilder) {
$containerBuilder->addDefinitions([
// ...
PDO::class => function (ContainerInterface $c) {
$settings = $c->get(SettingsInterface::class);
$dbSettings = $settings->get('db');
$host = $dbSettings['host'];
$dbname = $dbSettings['database'];
$username = $dbSettings['username'];
$password = $dbSettings['password'];
$port = $dbSettings['port'];
$driver = $dbSettings['driver'];
$dsn = "$driver:host=$host;port=$port;dbname=$dbname";
return new PDO($dsn, $username, $password);
},
]);
};