index.php 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. //setting Twig template engine
  3. // require_once $_SERVER['DOCUMENT_ROOT'].'/lib/Twig/lib/Twig/Autoloader.php';
  4. require __DIR__ . '/vendor/autoload.php';
  5. // Twig_Autoloader::register();
  6. $loader = new Twig_Loader_Filesystem($_SERVER['DOCUMENT_ROOT'].'/views');
  7. $twig = new Twig_Environment($loader, array(
  8. 'cache' => $_SERVER['DOCUMENT_ROOT'].'/cache',
  9. 'debug' => true
  10. ));
  11. //routing
  12. /*
  13. The following function will strip the script name from URL i.e. http://www.something.com/search/book/fitzgerald will become /search/book/fitzgerald
  14. */
  15. function getCurrentUri() {
  16. $basepath = implode('/', array_slice(explode('/', $_SERVER['SCRIPT_NAME']), 0, -1)) . '/';
  17. $uri = substr($_SERVER['REQUEST_URI'], strlen($basepath));
  18. $uri = trim($uri, '/');
  19. return $uri;
  20. }
  21. $base_url = getCurrentUri();
  22. $routes = array();
  23. $burl = explode('/', $base_url);
  24. foreach($burl as $route)
  25. {
  26. if(trim($route) != '')
  27. array_push($routes, $route);
  28. }
  29. /*
  30. 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
  31. */
  32. $allowed_routes = array('projects', 'openlife', 'ideas', 'links');
  33. if(sizeof($routes) === 0) {
  34. $template = $twig->loadTemplate('index.html');
  35. $variables = array();
  36. echo $template->render($variables);
  37. }
  38. elseif(in_array($routes[0], $allowed_routes)) {
  39. $pagename = $routes[0];
  40. $template = $twig->loadTemplate($pagename.'.html');
  41. $variables = array();
  42. if($pagename === 'projects') {
  43. $variables = array();
  44. }
  45. if($pagename === 'ideas') {
  46. $variables = array();
  47. }
  48. if($pagename === 'links') {
  49. $variables = array();
  50. }
  51. echo $template->render($variables);
  52. }
  53. else {
  54. http_response_code(404);
  55. $template = $twig->loadTemplate('404.html');
  56. $variables = array();
  57. echo $template->render($variables);
  58. }