Website/public/index.php

35 lines
793 B
PHP

<?php
function route(string $uri, callable $callback) {
if (urldecode(strtok($_SERVER['REQUEST_URI'], '?')) !== $uri)
return;
$callback();
die();
}
/**
* Loads a view from the view directory
*/
function view(string $_view, array $_data = []) {
$_path = __DIR__ . '/../view/' . $_view . '.php';
extract($_data);
require $_path;
}
/**
* Use for prefixing URLs so that they work in nested subdirectories
*/
function url(string $url, bool $full = false): string {
$dir = dirname($_SERVER['SCRIPT_NAME']);
if ($dir === '/') $dir = '';
if (!$full) return $dir . $url;
return isset($_SERVER['HTTPS']) ? 'https' : 'http' . '://' . $_SERVER['HTTP_HOST'] . $dir . $url;
}
route('/', fn() =>
view('home'));
route('/test', fn() =>
view('test'));
http_response_code(404);
view('404');