Website/public/index.php

35 lines
793 B
PHP
Raw Normal View History

2023-03-21 17:23:14 +00:00
<?php
2023-05-02 00:13:29 +00:00
function route(string $uri, callable $callback) {
if (urldecode(strtok($_SERVER['REQUEST_URI'], '?')) !== $uri)
return;
$callback();
die();
}
2023-03-21 17:23:14 +00:00
2023-05-02 00:13:29 +00:00
/**
* Loads a view from the view directory
*/
function view(string $_view, array $_data = []) {
$_path = __DIR__ . '/../view/' . $_view . '.php';
extract($_data);
require $_path;
}
2023-03-21 17:23:14 +00:00
2023-05-02 00:13:29 +00:00
/**
* 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;
}
2023-03-21 17:23:14 +00:00
2023-05-02 00:13:29 +00:00
route('/', fn() =>
view('home'));
2023-03-21 17:23:14 +00:00
2023-05-02 00:13:29 +00:00
route('/test', fn() =>
view('test'));
2023-03-21 17:23:14 +00:00
http_response_code(404);
view('404');