-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBasicRouter.php
47 lines (40 loc) · 1.02 KB
/
BasicRouter.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
<?php
class BasicRouter
{
function __construct()
{
$this->routeTheRoute();
}
private function routeTheRoute()
{
$uri = trim($_SERVER['REQUEST_URI'], '/');
$urray = explode('/', $uri);
if(count($urray) > 0)
{
$class_name = $this->toCamelCase($urray[0]);
if(class_exists($class_name))
{
$routebject = new $class_name($urray[1]);
}
else
{
echo "Bad route or no paramter. It's imperfect routing, I know...";
}
}
else
{
echo 'Hardcoding home page to save time.';
}
}
private function toCamelCase($string)
{
$result = strtolower($string);
preg_match_all('/_[a-z]/', $result, $matches);
foreach($matches[0] as $match)
{
$c = str_replace('_', '', strtoupper($match));
$result = str_replace($match, $c, $result);
}
return $result;
}
}