aboutsummaryrefslogtreecommitdiff
path: root/bootstrap/Router.php
blob: 34e278cde15db28a279abe6d1d6e5b37fc77e13b (plain)
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
37
38
39
40
41
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.'"');

        die(http_response_code(404));
    }
}