Advertisement
chrisenoch

EntryPoint.php

Mar 29th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. <?php
  2. namespace Ninja;
  3. class EntryPoint {
  4.  
  5. private $route;
  6. private $method;
  7. private $routes;
  8.  
  9. public function __construct(string $route, string $method, \Ninja\Routes $routes) {
  10. $this->route = $route;
  11. $this->method = $method;
  12. $this->routes = $routes;
  13. $this->checkUrl();
  14. }
  15.  
  16. private function checkUrl() {
  17. if ($this->route !== strtolower($this->route)) {
  18. http_response_code(301);
  19. header('location: ' . strtolower($this->route));
  20. }
  21. }
  22.  
  23. private function loadTemplate($templateFileName, $variables = []){
  24. extract($variables);
  25. ob_start();
  26. include __DIR__ . '/../../templates/' . $templateFileName;
  27.  
  28. return ob_get_clean();
  29. }
  30.  
  31.  
  32. public function run() {
  33. $routes = $this->routes->getRoutes();
  34. //I added the line below as the code didn't work originally.
  35. $authentication = $this->routes->getAuthentication();
  36. //My addition: authentification object got
  37.  
  38. if (isset($routes[$this->route]['login']) && // I think one of these is repeated. Delete one of these.
  39. !$authentication->isLoggedIn()) {
  40. header('location: /login/error');
  41. }
  42. else {
  43. $controller = $routes[$this->route]
  44. [$this->method]['controller'];
  45. $action = $routes[$this->route][$this->method]
  46. ['action'];
  47. $page = $controller->$action();
  48. $title = $page['title'];
  49. if (isset($page['variables'])) {
  50. $output = $this->loadTemplate($page['template'],
  51. $page['variables']);
  52. }
  53. else {
  54. $output = $this->loadTemplate($page['template']);
  55. }
  56.  
  57. echo $this->loadTemplate('layout.html.php', ['loggedIn' => $authentication->isLoggedIn(),
  58. 'output' => $output, 'title' => $title]);
  59.  
  60. }
  61. }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement