From 68006814cc1610e6feb9c7d67995589f23835347 Mon Sep 17 00:00:00 2001 From: William Date: Wed, 19 Jan 2022 20:16:09 +0100 Subject: [PATCH] Commit --- app/core/App.php | 12 ++++++++++++ public/api/simulator.php | 6 ++++++ public/index.php | 2 +- public/login.php | 2 +- 4 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 public/api/simulator.php diff --git a/app/core/App.php b/app/core/App.php index cf107fe..de92a1e 100644 --- a/app/core/App.php +++ b/app/core/App.php @@ -8,6 +8,7 @@ class App $this->config = $config; } + // Grab model public function model(string $model): object { // Require model file @@ -16,6 +17,7 @@ class App return new $model(); } + // Render given view public function view(string $view, array $data = []): void { // Import variables into the current symbol table from an array @@ -23,4 +25,14 @@ class App // Require view file require __DIR__ . '/../view/' . $view . '.php'; } + + // Turn data array into JSON response + public function api(array $data, int $status_code = 200): void + { + // Set headers + http_response_code($status_code); + header('Content-type: application/json'); + // Convert and respond with data + echo json_encode($data); + } } \ No newline at end of file diff --git a/public/api/simulator.php b/public/api/simulator.php new file mode 100644 index 0000000..6979b3f --- /dev/null +++ b/public/api/simulator.php @@ -0,0 +1,6 @@ +api([ + "msg" => "Hello World!" +]); \ No newline at end of file diff --git a/public/index.php b/public/index.php index 2cbae5c..b78ce92 100644 --- a/public/index.php +++ b/public/index.php @@ -1,5 +1,5 @@ model('Index'); diff --git a/public/login.php b/public/login.php index 88e8ea8..1d87b45 100644 --- a/public/login.php +++ b/public/login.php @@ -1,5 +1,5 @@ view('template/header', ["title" => "Logg inn"]); $app->view('login');