-
Notifications
You must be signed in to change notification settings - Fork 0
/
Router.php
44 lines (37 loc) · 1.05 KB
/
Router.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
<?php
namespace Simpleproject;
class Router
{
protected $routes = [];
protected $view;
public function __construct(View $view)
{
$this->view = $view;
}
public function addRoute($pattern, $handler)
{
$pattern = '/^'.str_replace('/', '\/', $pattern).'$/';
$this->routes[$pattern] = $handler;
}
public function handle($url, $baseUri = "")
{
if ($baseUri) {
$url = str_replace($baseUri, "", $url);
}
/**
* EN: Direct match
* PL: Bezpośrednie powiązanie
*/
if (isset($this->routes[$url])) {
$route = $this->routes[$url];
$handler = new $route[0]($this->view);
return call_user_func_array([$handler, $route[1]], $params);
}
foreach ($this->routes as $pattern => $route) {
if (preg_match($pattern, $url, $params)) {
$handler = new $route[0]($this->view);
return call_user_func_array([$handler, $route[1]], $params);
}
}
}
}