PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/egs/core/core/Router.php

https://bitbucket.org/jnewing/egs
PHP | 298 lines | 233 code | 65 blank | 0 comment | 36 complexity | 926356eef677d5275da21a818508f19e MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. class CI_Router {
  3. var $config;
  4. var $routes = array();
  5. var $error_routes = array();
  6. var $class = '';
  7. var $method = 'index';
  8. var $directory = '';
  9. var $default_controller;
  10. function __construct()
  11. {
  12. $this->config =& load_class('Config', 'core');
  13. $this->uri =& load_class('URI', 'core');
  14. log_message('debug', "Router Class Initialized");
  15. }
  16. function _set_routing()
  17. {
  18. $segments = array();
  19. if ($this->config->item('enable_query_strings') === TRUE AND isset($_GET[$this->config->item('controller_trigger')]))
  20. {
  21. if (isset($_GET[$this->config->item('directory_trigger')]))
  22. {
  23. $this->set_directory(trim($this->uri->_filter_uri($_GET[$this->config->item('directory_trigger')])));
  24. $segments[] = $this->fetch_directory();
  25. }
  26. if (isset($_GET[$this->config->item('controller_trigger')]))
  27. {
  28. $this->set_class(trim($this->uri->_filter_uri($_GET[$this->config->item('controller_trigger')])));
  29. $segments[] = $this->fetch_class();
  30. }
  31. if (isset($_GET[$this->config->item('function_trigger')]))
  32. {
  33. $this->set_method(trim($this->uri->_filter_uri($_GET[$this->config->item('function_trigger')])));
  34. $segments[] = $this->fetch_method();
  35. }
  36. }
  37. if (defined('ENVIRONMENT') AND is_file(APPPATH.'config/'.ENVIRONMENT.'/routes.php'))
  38. {
  39. include(APPPATH.'config/'.ENVIRONMENT.'/routes.php');
  40. }
  41. elseif (is_file(APPPATH.'config/routes.php'))
  42. {
  43. include(APPPATH.'config/routes.php');
  44. }
  45. $this->routes = ( ! isset($route) OR ! is_array($route)) ? array() : $route;
  46. unset($route);
  47. $this->default_controller = ( ! isset($this->routes['default_controller']) OR $this->routes['default_controller'] == '') ? FALSE : strtolower($this->routes['default_controller']);
  48. if (count($segments) > 0)
  49. {
  50. return $this->_validate_request($segments);
  51. }
  52. $this->uri->_fetch_uri_string();
  53. if ($this->uri->uri_string == '')
  54. {
  55. return $this->_set_default_controller();
  56. }
  57. $this->uri->_remove_url_suffix();
  58. $this->uri->_explode_segments();
  59. $this->_parse_routes();
  60. $this->uri->_reindex_segments();
  61. }
  62. function _set_default_controller()
  63. {
  64. if ($this->default_controller === FALSE)
  65. {
  66. show_error("Unable to determine what should be displayed. A default route has not been specified in the routing file.");
  67. }
  68. if (strpos($this->default_controller, '/') !== FALSE)
  69. {
  70. $x = explode('/', $this->default_controller);
  71. $this->set_class($x[0]);
  72. $this->set_method($x[1]);
  73. $this->_set_request($x);
  74. }
  75. else
  76. {
  77. $this->set_class($this->default_controller);
  78. $this->set_method('index');
  79. $this->_set_request(array($this->default_controller, 'index'));
  80. }
  81. $this->uri->_reindex_segments();
  82. log_message('debug', "No URI present. Default controller set.");
  83. }
  84. function _set_request($segments = array())
  85. {
  86. $segments = $this->_validate_request($segments);
  87. if (count($segments) == 0)
  88. {
  89. return $this->_set_default_controller();
  90. }
  91. $this->set_class($segments[0]);
  92. if (isset($segments[1]))
  93. {
  94. $this->set_method($segments[1]);
  95. }
  96. else
  97. {
  98. $segments[1] = 'index';
  99. }
  100. $this->uri->rsegments = $segments;
  101. }
  102. function _validate_request($segments)
  103. {
  104. if (count($segments) == 0)
  105. {
  106. return $segments;
  107. }
  108. if (file_exists(APPPATH.'controllers/'.$segments[0].'.php'))
  109. {
  110. return $segments;
  111. }
  112. if (is_dir(APPPATH.'controllers/'.$segments[0]))
  113. {
  114. $this->set_directory($segments[0]);
  115. $segments = array_slice($segments, 1);
  116. if (count($segments) > 0)
  117. {
  118. if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].'.php'))
  119. {
  120. if ( ! empty($this->routes['404_override']))
  121. {
  122. $x = explode('/', $this->routes['404_override']);
  123. $this->set_directory('');
  124. $this->set_class($x[0]);
  125. $this->set_method(isset($x[1]) ? $x[1] : 'index');
  126. return $x;
  127. }
  128. else
  129. {
  130. show_404($this->fetch_directory().$segments[0]);
  131. }
  132. }
  133. }
  134. else
  135. {
  136. if (strpos($this->default_controller, '/') !== FALSE)
  137. {
  138. $x = explode('/', $this->default_controller);
  139. $this->set_class($x[0]);
  140. $this->set_method($x[1]);
  141. }
  142. else
  143. {
  144. $this->set_class($this->default_controller);
  145. $this->set_method('index');
  146. }
  147. if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.'.php'))
  148. {
  149. $this->directory = '';
  150. return array();
  151. }
  152. }
  153. return $segments;
  154. }
  155. if ( ! empty($this->routes['404_override']))
  156. {
  157. $x = explode('/', $this->routes['404_override']);
  158. $this->set_class($x[0]);
  159. $this->set_method(isset($x[1]) ? $x[1] : 'index');
  160. return $x;
  161. }
  162. show_404($segments[0]);
  163. }
  164. function _parse_routes()
  165. {
  166. $uri = implode('/', $this->uri->segments);
  167. if (isset($this->routes[$uri]))
  168. {
  169. return $this->_set_request(explode('/', $this->routes[$uri]));
  170. }
  171. foreach ($this->routes as $key => $val)
  172. {
  173. $key = str_replace(':any', '.+', str_replace(':num', '[0-9]+', $key));
  174. if (preg_match('#^'.$key.'$#', $uri))
  175. {
  176. if (strpos($val, '$') !== FALSE AND strpos($key, '(') !== FALSE)
  177. {
  178. $val = preg_replace('#^'.$key.'$#', $val, $uri);
  179. }
  180. return $this->_set_request(explode('/', $val));
  181. }
  182. }
  183. $this->_set_request($this->uri->segments);
  184. }
  185. function set_class($class)
  186. {
  187. $this->class = str_replace(array('/', '.'), '', $class);
  188. }
  189. function fetch_class()
  190. {
  191. return $this->class;
  192. }
  193. function set_method($method)
  194. {
  195. $this->method = $method;
  196. }
  197. function fetch_method()
  198. {
  199. if ($this->method == $this->fetch_class())
  200. {
  201. return 'index';
  202. }
  203. return $this->method;
  204. }
  205. function set_directory($dir)
  206. {
  207. $this->directory = str_replace(array('/', '.'), '', $dir).'/';
  208. }
  209. function fetch_directory()
  210. {
  211. return $this->directory;
  212. }
  213. function _set_overrides($routing)
  214. {
  215. if ( ! is_array($routing))
  216. {
  217. return;
  218. }
  219. if (isset($routing['directory']))
  220. {
  221. $this->set_directory($routing['directory']);
  222. }
  223. if (isset($routing['controller']) AND $routing['controller'] != '')
  224. {
  225. $this->set_class($routing['controller']);
  226. }
  227. if (isset($routing['function']))
  228. {
  229. $routing['function'] = ($routing['function'] == '') ? 'index' : $routing['function'];
  230. $this->set_method($routing['function']);
  231. }
  232. }
  233. }