Initial habit tracker project
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
use PDO;
|
||||
use PDOException;
|
||||
|
||||
class Database
|
||||
{
|
||||
private static ?PDO $connection = null;
|
||||
|
||||
public static function connection(): PDO
|
||||
{
|
||||
if (self::$connection !== null) {
|
||||
return self::$connection;
|
||||
}
|
||||
|
||||
$db = config('database');
|
||||
$dsn = sprintf(
|
||||
'mysql:host=%s;dbname=%s;charset=%s',
|
||||
$db['host'],
|
||||
$db['dbname'],
|
||||
$db['charset']
|
||||
);
|
||||
|
||||
try {
|
||||
self::$connection = new PDO($dsn, $db['user'], $db['password'], [
|
||||
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
||||
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
||||
]);
|
||||
} catch (PDOException $exception) {
|
||||
throw new PDOException('Datenbankverbindung fehlgeschlagen.');
|
||||
}
|
||||
|
||||
return self::$connection;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
class View
|
||||
{
|
||||
public static function render(string $view, array $data = []): void
|
||||
{
|
||||
extract($data, EXTR_SKIP);
|
||||
|
||||
$viewPath = __DIR__ . '/../Views/' . $view . '.php';
|
||||
|
||||
require __DIR__ . '/../Views/layout/header.php';
|
||||
require $viewPath;
|
||||
require __DIR__ . '/../Views/layout/footer.php';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user