-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrouter.php
More file actions
36 lines (28 loc) · 731 Bytes
/
Copy pathrouter.php
File metadata and controls
36 lines (28 loc) · 731 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?php
//dieAndDump($_SERVER);
$uri = parse_url($_SERVER['REQUEST_URI'])['path'];
//if ($uri === '/') {
// require "controllers/index.php";
//} else if ($uri === '/about') {
// require "controllers/about.php";
//} else if ($uri === '/contact') {
// require "controllers/contact.php";
//}
$routes = [
'/' => 'controllers/index.php',
'/about' => 'controllers/about.php',
'/contact' => 'controllers/contact.php',
];
function routeToController($uri, $routes) {
if(key_exists($uri, $routes)) {
require $routes[$uri];
} else {
abort();
}
}
function abort($code = 404) {
http_response_code($code);
require "views/${code}.php";
die();
}
routeToController($uri, $routes);