-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRouter.php
More file actions
84 lines (59 loc) · 1.65 KB
/
Router.php
File metadata and controls
84 lines (59 loc) · 1.65 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
namespace Core;
use App\Config;
class Router {
private $params = [];
private $controller;
private $method;
private function parseUrl(string $url) {
$url = rtrim($url, '/');
$url = filter_var($url, FILTER_SANITIZE_URL);
$urlArr = explode('/', $url);
$urlArr = array_filter($urlArr, function($value) {
return $value !== '';
});
$urlLenght = count($urlArr);
if ($urlLenght < 1) {
$this -> setController(Config::ROUTE_DEFAULT);
return;
}
$this -> setController(ucfirst($urlArr[0]));
if ($urlLenght > 1) {
$this -> setMethod($urlArr[1]);
$this -> setParams(array_slice($urlArr, 2));
}
}
public function dispatch(string $url) {
$this -> parseUrl($url);
if ($this -> controller == null)
throw new \Exception('Contoller Hatası');
$cont = 'App\\Controller\\' . $this -> controller . 'Controller';
if (!class_exists($cont))
throw new \Exception(get_class() . ': Controller bulunamadı.');
$controller = new $cont($this -> params);
if (method_exists($controller, 'initialize'))
$controller -> initialize();
if ($this -> getMethod() === null)
$this -> setMethod($controller::defaultMethod);
if (method_exists($controller, $this -> getMethod()))
$controller -> {$this->method}();
}
public function getParams() {
return $this -> params;
}
public function setParams($params) {
$this -> params = $params;
}
public function getController() {
return $this -> controller;
}
public function setController($controller) {
$this -> controller = $controller;
}
public function getMethod() {
return $this -> method;
}
public function setMethod($method) {
$this -> method = $method;
}
}