Advertisement
Guest User

Untitled

a guest
Jul 28th, 2015
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. <?php
  2.  
  3. class router {
  4. /*
  5. * @the registry
  6. */
  7. private $registry;
  8. /*
  9. * @the controller path
  10. */
  11. private $path;
  12. private $args = array();
  13. public $file;
  14. public $controller;
  15. public $action;
  16.  
  17. function __construct($registry) {
  18. $this->registry = $registry;
  19. }
  20.  
  21. function setPath($path) {
  22.  
  23. /*** check if path i sa directory ***/
  24. if (is_dir($path) == false)
  25. {
  26. throw new Exception ('Invalid controller path: `' . $path . '`');
  27. }
  28. /*** set the path ***/
  29. $this->path = $path;
  30. }
  31.  
  32. public function loader()
  33. {
  34. /*** check the route ***/
  35. $this->getController();
  36.  
  37.  
  38.  
  39. /*** if the file is not there diaf ***/
  40. if (is_readable($this->file) == false)
  41. {
  42. // TODO ? //
  43. // Route and Display a 404 Error Page when system is ready //
  44. echo "404: Not Found";
  45. } else {
  46.  
  47. /*** include the controller ***/
  48. include $this->file;
  49.  
  50. /*** a new controller class instance ***/
  51. $class = $this->controller . 'Controller';
  52. $controller = new $class($this->registry);
  53.  
  54. /*** check if the action is callable ***/
  55. if (is_callable(array($controller, $this->action)) == false)
  56. {
  57. $action = 'news';
  58. }
  59. else
  60. {
  61. $action = $this->action;
  62. }
  63. /*** run the action ***/
  64. $controller->$action();
  65. }
  66. }
  67.  
  68. private function getController() {
  69.  
  70. /*** get the route from the url ***/
  71. $route = (empty($_GET['task'])) ? '' : $_GET['task'];
  72.  
  73. if (empty($route))
  74. {
  75. $route = 'news';
  76. }
  77. else
  78. {
  79. /*** get the parts of the route ***/
  80. $parts = explode('/', $route);
  81. $this->controller = $parts[0];
  82. if(isset( $parts[1]))
  83. {
  84. $this->action = $parts[1];
  85. }
  86. }
  87.  
  88. if (empty($this->controller))
  89. {
  90. $this->controller = 'news';
  91. }
  92.  
  93. /*** Get action ***/
  94. if (empty($this->action))
  95. {
  96. $this->action = 'index';
  97. }
  98.  
  99. /*** set the file path ***/
  100. $this->file = $this->path .'/'. $this->controller . 'Controller.php';
  101. }
  102. }
  103.  
  104. <?php
  105.  
  106. class template {
  107.  
  108. /*
  109. * @the registry
  110. * @access private
  111. */
  112. private $registry;
  113.  
  114. /*
  115. * @Variables array
  116. * @access private
  117. */
  118. private $vars = array();
  119.  
  120. /**
  121. *
  122. * @constructor
  123. *
  124. * @access public
  125. *
  126. * @return void
  127. *
  128. */
  129. function __construct($registry) {
  130. $this->registry = $registry;
  131.  
  132. }
  133. /**
  134. *
  135. * @set undefined vars
  136. *
  137. * @param string $index
  138. *
  139. * @param mixed $value
  140. *
  141. * @return void
  142. *
  143. */
  144. public function __set($index, $value)
  145. {
  146. $this->vars[$index] = $value;
  147. }
  148.  
  149. public function show($name) {
  150.  
  151. $path = SITE_PATH . '/system/views/' . $name . '.php';
  152.  
  153. if (file_exists($path) == false)
  154. {
  155. throw new Exception('Template not found in '. $path);
  156. return false;
  157. }
  158.  
  159. // Load variables
  160. foreach ($this->vars as $key => $value)
  161. {
  162. $key = $value;
  163. }
  164.  
  165. include($path);
  166.  
  167. }
  168. }
  169.  
  170. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement