This repository has been archived on 2023-01-06. You can view files and clone it, but cannot push or open issues or pull requests.
web/app/core/App.php
2022-01-20 22:23:21 +01:00

48 lines
1.2 KiB
PHP

<?php
class App
{
public array $config;
public object $database;
public object $session;
public function __construct()
{
// Require configuration file
$this->config = require __DIR__ . '/../config.php';
// Connect to database
$this->database = new Database($this->config['database']);
// Instantiate new session
$this->session = new Session;
}
// Grab model
public function model(string $model): object
{
// Require model file
require __DIR__ . '/../model/' . $model . '.php';
// Instantiate model
return new $model();
}
// Render given view
public function view(string $view, array $data = []): void
{
// Import variables into the current symbol table from an array
extract($data);
// 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);
}
}