-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
executable file
·69 lines (57 loc) · 1.77 KB
/
index.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
57
58
59
60
61
62
63
64
65
66
67
68
<?php
//setting Twig template engine
require_once $_SERVER['DOCUMENT_ROOT'].'/lib/Twig/lib/Twig/Autoloader.php';
Twig_Autoloader::register();
$loader = new Twig_Loader_Filesystem($_SERVER['DOCUMENT_ROOT'].'/views');
$twig = new Twig_Environment($loader, array(
'cache' => $_SERVER['DOCUMENT_ROOT'].'/cache',
'debug' => true
));
//routing
/*
The following function will strip the script name from URL i.e. http://www.something.com/search/book/fitzgerald will become /search/book/fitzgerald
*/
function getCurrentUri() {
$basepath = implode('/', array_slice(explode('/', $_SERVER['SCRIPT_NAME']), 0, -1)) . '/';
$uri = substr($_SERVER['REQUEST_URI'], strlen($basepath));
$uri = trim($uri, '/');
return $uri;
}
$base_url = getCurrentUri();
$routes = array();
$burl = explode('/', $base_url);
foreach($burl as $route)
{
if(trim($route) != '')
array_push($routes, $route);
}
/*
Now, $routes will contain all the routes. $routes[0] will correspond to first route. For e.g. in above example $routes[0] is search, $routes[1] is book and $routes[2] is fitzgerald
*/
$allowed_routes = array('projects', 'openlife', 'ideas', 'links');
if(sizeof($routes) === 0) {
$template = $twig->loadTemplate('index.html');
$variables = array();
echo $template->render($variables);
}
elseif(in_array($routes[0], $allowed_routes)) {
$pagename = $routes[0];
$template = $twig->loadTemplate($pagename.'.html');
$variables = array();
if($pagename === 'projects') {
$variables = array();
}
if($pagename === 'ideas') {
$variables = array();
}
if($pagename === 'links') {
$variables = array();
}
echo $template->render($variables);
}
else {
http_response_code(404);
$template = $twig->loadTemplate('404.html');
$variables = array();
echo $template->render($variables);
}