62 lines
1.5 KiB
PHP
62 lines
1.5 KiB
PHP
<?php
|
|
|
|
class App
|
|
{
|
|
public array $config;
|
|
public Database $database;
|
|
public Session $session;
|
|
public User $user;
|
|
|
|
public function __construct(
|
|
array $config,
|
|
Database $database,
|
|
Session $session,
|
|
User $user
|
|
)
|
|
{
|
|
$this->config = $config;
|
|
$this->database = $database;
|
|
$this->session = $session;
|
|
$this->user = $user;
|
|
}
|
|
|
|
// Grab model
|
|
public function model(string $model, $injection = NULL): object
|
|
{
|
|
// Require model file
|
|
require __DIR__ . '/../model/' . $model . '.php';
|
|
// Instantiate model
|
|
if (!$injection)
|
|
{
|
|
$injection = $this->database;
|
|
}
|
|
return new $model($injection);
|
|
}
|
|
|
|
// 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);
|
|
die();
|
|
}
|
|
|
|
// Redirect to given url
|
|
public function redirect(string $url): void
|
|
{
|
|
header("Location: $url");
|
|
die();
|
|
}
|
|
} |