-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRouting.php
56 lines (44 loc) · 1.54 KB
/
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
55
56
<?php
require_once __DIR__ . '/src/controllers/UserDataController.php';
require_once __DIR__ . '/src/controllers/SecurityController.php';
require_once __DIR__ . '/src/controllers/DefaultController.php';
require_once __DIR__ . '/src/controllers/CardController.php';
require_once __DIR__ . '/src/controllers/AdminController.php';
class Routing
{
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 run($url)
{
// Rozdzielenie ścieżki na akcję
$action = explode('/', $url)[0];
// Sprawdzenie, czy akcja istnieje w routingu
if (!array_key_exists($action, self::$routes)) {
die('Wrong URL');
}
// Pobranie nazwy kontrolera z routing
$controller = self::$routes[$action];
// Dodanie przestrzeni nazw
$controllerNamespace = "controllers\\" . $controller;
// Sprawdzenie, czy klasa istnieje
if (!class_exists($controllerNamespace)) {
die("Class $controllerNamespace not found!");
}
// Utworzenie obiektu klasy kontrolera
$object = new $controllerNamespace;
// Wywołanie metody w kontrolerze
$method = $action ?: 'index';
if (!method_exists($object, $method)) {
die("Method $method not found in class $controllerNamespace!");
}
// Wywołanie metody
$object->$method();
}
}