aboutsummaryrefslogtreecommitdiff
path: root/bootstrap/Router.php
diff options
context:
space:
mode:
Diffstat (limited to 'bootstrap/Router.php')
-rw-r--r--bootstrap/Router.php42
1 files changed, 42 insertions, 0 deletions
diff --git a/bootstrap/Router.php b/bootstrap/Router.php
new file mode 100644
index 0000000..a098c30
--- /dev/null
+++ b/bootstrap/Router.php
@@ -0,0 +1,42 @@
+<?php
+
+class Router
+{
+ protected $routes = [
+ 'GET' => [],
+ 'POST' => [],
+ 'HEAD' => []
+ ];
+
+ public function get($uri, $controller)
+ {
+ $this->routes['GET'][$uri] = $controller;
+ }
+
+ public function post($uri, $controller)
+ {
+ $this->routes['POST'][$uri] = $controller;
+ }
+
+ public function head($uri, $controller)
+ {
+ $this->routes['HEAD'][$uri] = $controller;
+ }
+
+ public static function load($file)
+ {
+ $router = new static;
+ require $file;
+ return $router;
+ }
+
+ public function direct($uri, $requestType)
+ {
+ if (is_array($this->routes[$requestType]) && array_key_exists($uri, $this->routes[$requestType])) {
+ return $this->routes[$requestType][$uri];
+ }
+ // throw new Exception('No route defined for this URI: "'.$uri.'"');
+ http_response_code(404);
+ die(require '../views/404.view.php');
+ }
+}