From 3fbcfa71592cd2c5d8f4c20f1696b56e5bd65b27 Mon Sep 17 00:00:00 2001 From: bye Date: Fri, 28 Jun 2024 19:28:28 +0100 Subject: [PATCH] Create database connection and add API route --- api.php | 15 +++++++++++++++ index.php | 8 +++++++- 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 api.php diff --git a/api.php b/api.php new file mode 100644 index 0000000..f3bf362 --- /dev/null +++ b/api.php @@ -0,0 +1,15 @@ + [ + "success" => false, + "status_code" => 404 + ], + 'body' => [ + 'message' => 'That endpoint could not be found.' + ] +]; + +echo json_encode($response, JSON_PRETTY_PRINT); diff --git a/index.php b/index.php index 9a8be73..31b5257 100644 --- a/index.php +++ b/index.php @@ -8,7 +8,12 @@ try { exit; } -// TODO: Init PDO. +// Connect to database +try { + $pdo = new PDO(DB_DSN, DB_USERNAME, DB_PASSWORD, PDO_OPTIONS); +} catch (PDOException $e) { + echo "Critical error: " . $e->getMessage() . "
"; +} // Starts the session // TODO: write this to use the database to work across more than one server @@ -29,6 +34,7 @@ if (str_ends_with($path, '/') && $path != '/') { $routes = [ '' => function () { require 'views/home.php'; }, + 'api' => function () { require 'api.php'; /* Handoff further routing to API script. */ } ]; //print_r($path_array);