Guest User

Untitled

a guest
May 26th, 2018
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. <?php
  2. require("config.php");
  3.  
  4. /**
  5. * Router Class - Framework
  6. * @author Erwin Okken <erwtje@gmail.com>
  7. * @version 1.0
  8. */
  9. class Router
  10. {
  11. /**
  12. * @var string path
  13. */
  14. var $path;
  15.  
  16. /**
  17. * @var string page
  18. */
  19. var $page;
  20.  
  21. /**
  22. * @var string action
  23. */
  24. var $action;
  25.  
  26. /**
  27. * @var array optional parameters
  28. */
  29. var $parameters = array();
  30.  
  31. /**
  32. * Constructor gets the current page, action and optional parameters.
  33. * Redirects to dispatch function.
  34. */
  35. public function __construct()
  36. {
  37. $requestURI = explode('/', $_SERVER['REQUEST_URI']);
  38. $scriptName = explode('/',$_SERVER['SCRIPT_NAME']);
  39. for($i=0;$i < sizeof($scriptName);$i++)
  40. {
  41. if(strtolower($requestURI[$i]) == strtolower($scriptName[$i]))
  42. {
  43. $this->path .= $requestURI[$i] . "/";
  44. unset($requestURI[$i]);
  45. }
  46. }
  47.  
  48. // dit kan later weg:
  49. define("PATH",$this->path);
  50.  
  51. $url = array_values($requestURI);
  52. for($i=0;$i < sizeof($url);$i++){
  53. if($i==0){
  54. $this->page = $url[$i];
  55. }
  56. elseif($i==1){
  57. $this->action = $url[$i];
  58. }
  59. else{
  60. $this->parameters[] = $url[$i];
  61. }
  62. }
  63.  
  64. $this->page = (empty($this->page)) ? "home" : $this->page;
  65. $this->action = (empty($this->action)) ? "news" : $this->action;
  66. switch($this->page){
  67. case "home":
  68. case "guestbook":
  69. try
  70. {
  71. $this->dispatch();
  72. }
  73. catch(Exception $e)
  74. {
  75. echo $e->getMessage();
  76. }
  77. break;
  78.  
  79. default:
  80. $this->page = "home";
  81. $this->action = "404";
  82. $this->dispatch();
  83. break;
  84. }
  85. }
  86.  
  87. /**
  88. * Dispatch method checks the called controller and method and redirects it.
  89. * @throw Exception When controller doesn't exist
  90. */
  91. public function dispatch()
  92. {
  93. if(file_exists("controllers/" . $this->page . ".class.php"))
  94. {
  95. require_once("controllers/" . $this->page . ".class.php");
  96. }
  97. else
  98. {
  99. throw new Exception("Cannot find the controller file: " . $this->page);
  100. }
  101.  
  102. //$controllername = ucfirst($this->page) . "_Controller";
  103. //$controller = new $controllername();
  104.  
  105. if(class_exists(ucfirst($this->page) . "_Controller"))
  106. {
  107. // Onderstaande methode werkt, maar STATIC.
  108. // call_user_func_array(array(ucfirst($this->page) . '_Controller', $this->action),array());
  109. $controllername = $this->page . "_Controller";
  110. $action = $this->action;
  111.  
  112. $curcontroller = new $controllername;
  113. if(method_exists($controllername, $this->action))
  114. {
  115. $curcontroller->$action();
  116. }
  117. else
  118. {
  119. throw new Exception("Methode niet gevonden: " . $action);
  120. }
  121. }
  122. else
  123. {
  124. throw new Exception("Cannot find controller class: " . $this->page);
  125. }
  126. }
  127. }
  128.  
  129. $Router = new Router();
  130. ?>
Add Comment
Please, Sign In to add comment