-
Notifications
You must be signed in to change notification settings - Fork 0
/
Routing.php
54 lines (42 loc) · 1001 Bytes
/
Routing.php
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
43
44
45
46
47
48
49
50
51
52
53
54
<?php
require_once 'src/controllers/AuthController.php';
require_once 'src/controllers/BookController.php';
require_once 'src/controllers/UserController.php';
class Router
{
public static $routes = [];
public static function get($url, $view)
{
self::$routes[$url] = $view;
}
public static function post($url, $view)
{
self::$routes[$url] = $view;
}
public static function put($url, $view)
{
self::$routes[$url] = $view;
}
public static function patch($url, $view)
{
self::$routes[$url] = $view;
}
public static function delete($url, $view)
{
self::$routes[$url] = $view;
}
public static function run($url)
{
$urlParts = explode("/", $url);
$action = $urlParts[0];
if (!isset(self::$routes[$action])) {
header("Location: /");
exit();
}
$controller = self::$routes[$action];
$object = new $controller;
$action = $action ?: 'index';
$id = $urlParts[1] ?? '';
$object->$action($id);
}
}