-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtinyrouter.php
42 lines (33 loc) · 916 Bytes
/
tinyrouter.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
<?php
class route
{
public static function __callStatic($method, $args)
{
if ($_SERVER['REQUEST_METHOD'] == strtoupper($method)) {
$presets = [
':all' => '.*',
':alpha' => '[a-zA-Z]+',
':any' => '[^/]+',
':num' => '\d+|-\d+',
];
$pattern = $args[0];
foreach ($presets as $shortcode => $regex) {
$pattern = strtr($pattern, [$shortcode => '(' . $regex . ')']);
}
$pattern = '~^' . $pattern . '$~';
$uri = '/';
$dir = dirname($_SERVER['SCRIPT_NAME']);
if (isset($_SERVER['REDIRECT_URL']) && $dir != '/') {
$uri = strtr($_SERVER['REDIRECT_URL'], [$dir => '']);
}
preg_match($pattern, $uri, $matches);
if (!$matches) return;
$output = $args[1]($matches);
if (isset($output)) die($output);
}
}
}
function route($pattern, $method)
{
route::get($pattern, $method);
}