Part on routers finished

This commit is contained in:
Patryk Hegenberg 2022-12-12 21:12:05 +01:00
parent a1e1a43d81
commit 8bb5f373f8
8 changed files with 68 additions and 9 deletions

25
router.php Normal file
View file

@ -0,0 +1,25 @@
<?php
$uri = parse_url($_SERVER['REQUEST_URI'])['path'];
$routes = [
'/' => 'controllers/index.php',
'/about' => 'controllers/about.php',
'/contact' => 'controllers/contact.php',
'/mission' => 'controllers/mission.php',
];
function routeToController($uri, $routes) {
if (array_key_exists($uri, $routes)) {
require $routes[$uri];
} else {
abort();
}
}
routeToController($uri, $routes);
function abort($code = 404) {
http_response_code($code);
require "views/{$code}.php";
die();
}