Router.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 5.1.6 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author EllisLab Dev Team
  9. * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc.
  10. * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/)
  11. * @license http://codeigniter.com/user_guide/license.html
  12. * @link http://codeigniter.com
  13. * @since Version 1.0
  14. * @filesource
  15. */
  16. // ------------------------------------------------------------------------
  17. /**
  18. * Router Class
  19. *
  20. * Parses URIs and determines routing
  21. *
  22. * @package CodeIgniter
  23. * @subpackage Libraries
  24. * @author EllisLab Dev Team
  25. * @category Libraries
  26. * @link http://codeigniter.com/user_guide/general/routing.html
  27. */
  28. class CI_Router {
  29. /**
  30. * Config class
  31. *
  32. * @var object
  33. * @access public
  34. */
  35. var $config;
  36. /**
  37. * List of routes
  38. *
  39. * @var array
  40. * @access public
  41. */
  42. var $routes = array();
  43. /**
  44. * List of error routes
  45. *
  46. * @var array
  47. * @access public
  48. */
  49. var $error_routes = array();
  50. /**
  51. * Current class name
  52. *
  53. * @var string
  54. * @access public
  55. */
  56. var $class = '';
  57. /**
  58. * Current method name
  59. *
  60. * @var string
  61. * @access public
  62. */
  63. var $method = 'index';
  64. /**
  65. * Sub-directory that contains the requested controller class
  66. *
  67. * @var string
  68. * @access public
  69. */
  70. var $directory = '';
  71. /**
  72. * Default controller (and method if specific)
  73. *
  74. * @var string
  75. * @access public
  76. */
  77. var $default_controller;
  78. /**
  79. * Constructor
  80. *
  81. * Runs the route mapping function.
  82. */
  83. function __construct()
  84. {
  85. $this->config =& load_class('Config', 'core');
  86. $this->uri =& load_class('URI', 'core');
  87. log_message('debug', "Router Class Initialized");
  88. }
  89. // --------------------------------------------------------------------
  90. /**
  91. * Set the route mapping
  92. *
  93. * This function determines what should be served based on the URI request,
  94. * as well as any "routes" that have been set in the routing config file.
  95. *
  96. * @access private
  97. * @return void
  98. */
  99. function _set_routing()
  100. {
  101. // Are query strings enabled in the config file? Normally CI doesn't utilize query strings
  102. // since URI segments are more search-engine friendly, but they can optionally be used.
  103. // If this feature is enabled, we will gather the directory/class/method a little differently
  104. $segments = array();
  105. if ($this->config->item('enable_query_strings') === TRUE AND isset($_GET[$this->config->item('controller_trigger')]))
  106. {
  107. if (isset($_GET[$this->config->item('directory_trigger')]))
  108. {
  109. $this->set_directory(trim($this->uri->_filter_uri($_GET[$this->config->item('directory_trigger')])));
  110. $segments[] = $this->fetch_directory();
  111. }
  112. if (isset($_GET[$this->config->item('controller_trigger')]))
  113. {
  114. $this->set_class(trim($this->uri->_filter_uri($_GET[$this->config->item('controller_trigger')])));
  115. $segments[] = $this->fetch_class();
  116. }
  117. if (isset($_GET[$this->config->item('function_trigger')]))
  118. {
  119. $this->set_method(trim($this->uri->_filter_uri($_GET[$this->config->item('function_trigger')])));
  120. $segments[] = $this->fetch_method();
  121. }
  122. }
  123. array_walk_recursive($segments , 'strtolower') ;
  124. // Load the routes.php file.
  125. if (defined('ENVIRONMENT') AND is_file(APPPATH.'config/'.ENVIRONMENT.'/routes.php'))
  126. {
  127. include(APPPATH.'config/'.ENVIRONMENT.'/routes.php');
  128. }
  129. elseif (is_file(APPPATH.'config/routes.php'))
  130. {
  131. include(APPPATH.'config/routes.php');
  132. }
  133. $this->routes = ( ! isset($route) OR ! is_array($route)) ? array() : $route;
  134. unset($route);
  135. // Set the default controller so we can display it in the event
  136. // the URI doesn't correlated to a valid controller.
  137. $this->default_controller = ( ! isset($this->routes['default_controller']) OR $this->routes['default_controller'] == '') ? FALSE : strtolower($this->routes['default_controller']);
  138. // Were there any query string segments? If so, we'll validate them and bail out since we're done.
  139. if (count($segments) > 0)
  140. {
  141. return $this->_validate_request($segments);
  142. }
  143. // Fetch the complete URI string
  144. $this->uri->_fetch_uri_string();
  145. // Is there a URI string? If not, the default controller specified in the "routes" file will be shown.
  146. if ($this->uri->uri_string == '')
  147. {
  148. return $this->_set_default_controller();
  149. }
  150. // Do we need to remove the URL suffix?
  151. $this->uri->_remove_url_suffix();
  152. // Compile the segments into an array
  153. $this->uri->_explode_segments();
  154. // Parse any custom routing that may exist
  155. $this->_parse_routes();
  156. // Re-index the segment array so that it starts with 1 rather than 0
  157. $this->uri->_reindex_segments();
  158. }
  159. // --------------------------------------------------------------------
  160. /**
  161. * Set the default controller
  162. *
  163. * @access private
  164. * @return void
  165. */
  166. function _set_default_controller()
  167. {
  168. if ($this->default_controller === FALSE)
  169. {
  170. show_error("Unable to determine what should be displayed. A default route has not been specified in the routing file.");
  171. }
  172. // Is the method being specified?
  173. if (strpos($this->default_controller, '/') !== FALSE)
  174. {
  175. $x = explode('/', $this->default_controller);
  176. $this->set_class($x[0]);
  177. $this->set_method($x[1]);
  178. $this->_set_request($x);
  179. }
  180. else
  181. {
  182. $this->set_class($this->default_controller);
  183. $this->set_method('index');
  184. $this->_set_request(array($this->default_controller, 'index'));
  185. }
  186. // re-index the routed segments array so it starts with 1 rather than 0
  187. $this->uri->_reindex_segments();
  188. log_message('debug', "No URI present. Default controller set.");
  189. }
  190. // --------------------------------------------------------------------
  191. /**
  192. * Set the Route
  193. *
  194. * This function takes an array of URI segments as
  195. * input, and sets the current class/method
  196. *
  197. * @access private
  198. * @param array
  199. * @param bool
  200. * @return void
  201. */
  202. function _set_request($segments = array())
  203. {
  204. $segments = $this->_validate_request($segments);
  205. if (count($segments) == 0)
  206. {
  207. return $this->_set_default_controller();
  208. }
  209. $this->set_class($segments[0]);
  210. if (isset($segments[1]))
  211. {
  212. // A standard method request
  213. $this->set_method($segments[1]);
  214. }
  215. else
  216. {
  217. // This lets the "routed" segment array identify that the default
  218. // index method is being used.
  219. $segments[1] = 'index';
  220. }
  221. // Update our "routed" segment array to contain the segments.
  222. // Note: If there is no custom routing, this array will be
  223. // identical to $this->uri->segments
  224. $this->uri->rsegments = $segments;
  225. }
  226. // --------------------------------------------------------------------
  227. /**
  228. * Validates the supplied segments. Attempts to determine the path to
  229. * the controller.
  230. *
  231. * @access private
  232. * @param array
  233. * @return array
  234. */
  235. function _validate_request($segments)
  236. {
  237. if (count($segments) == 0)
  238. {
  239. return $segments;
  240. }
  241. // Does the requested controller exist in the root folder?
  242. if (file_exists(APPPATH.'controllers/'.$segments[0].'.php'))
  243. {
  244. return $segments;
  245. }
  246. // Is the controller in a sub-folder?
  247. if (is_dir(APPPATH.'controllers/'.$segments[0]))
  248. {
  249. // Set the directory and remove it from the segment array
  250. $this->set_directory($segments[0]);
  251. $segments = array_slice($segments, 1);
  252. if (count($segments) > 0)
  253. {
  254. // Does the requested controller exist in the sub-folder?
  255. if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].'.php'))
  256. {
  257. if ( ! empty($this->routes['404_override']))
  258. {
  259. $x = explode('/', $this->routes['404_override']);
  260. $this->set_directory('');
  261. $this->set_class($x[0]);
  262. $this->set_method(isset($x[1]) ? $x[1] : 'index');
  263. return $x;
  264. }
  265. else
  266. {
  267. show_404($this->fetch_directory().$segments[0]);
  268. }
  269. }
  270. }
  271. else
  272. {
  273. // Is the method being specified in the route?
  274. if (strpos($this->default_controller, '/') !== FALSE)
  275. {
  276. $x = explode('/', $this->default_controller);
  277. $this->set_class($x[0]);
  278. $this->set_method($x[1]);
  279. }
  280. else
  281. {
  282. $this->set_class($this->default_controller);
  283. $this->set_method('index');
  284. }
  285. // Does the default controller exist in the sub-folder?
  286. if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.'.php'))
  287. {
  288. $this->directory = '';
  289. return array();
  290. }
  291. }
  292. return $segments;
  293. }
  294. // If we've gotten this far it means that the URI does not correlate to a valid
  295. // controller class. We will now see if there is an override
  296. if ( ! empty($this->routes['404_override']))
  297. {
  298. $x = explode('/', $this->routes['404_override']);
  299. $this->set_class($x[0]);
  300. $this->set_method(isset($x[1]) ? $x[1] : 'index');
  301. return $x;
  302. }
  303. // Nothing else to do at this point but show a 404
  304. show_404($segments[0]);
  305. }
  306. // --------------------------------------------------------------------
  307. /**
  308. * Parse Routes
  309. *
  310. * This function matches any routes that may exist in
  311. * the config/routes.php file against the URI to
  312. * determine if the class/method need to be remapped.
  313. *
  314. * @access private
  315. * @return void
  316. */
  317. function _parse_routes()
  318. {
  319. // Turn the segment array into a URI string
  320. $uri = implode('/', $this->uri->segments);
  321. // Is there a literal match? If so we're done
  322. if (isset($this->routes[$uri]))
  323. {
  324. return $this->_set_request(explode('/', $this->routes[$uri]));
  325. }
  326. // Loop through the route array looking for wild-cards
  327. foreach ($this->routes as $key => $val)
  328. {
  329. // Convert wild-cards to RegEx
  330. $key = str_replace(':any', '.+', str_replace(':num', '[0-9]+', $key));
  331. // Does the RegEx match?
  332. if (preg_match('#^'.$key.'$#', $uri))
  333. {
  334. // Do we have a back-reference?
  335. if (strpos($val, '$') !== FALSE AND strpos($key, '(') !== FALSE)
  336. {
  337. $val = preg_replace('#^'.$key.'$#', $val, $uri);
  338. }
  339. return $this->_set_request(explode('/', $val));
  340. }
  341. }
  342. // If we got this far it means we didn't encounter a
  343. // matching route so we'll set the site default route
  344. $this->_set_request($this->uri->segments);
  345. }
  346. // --------------------------------------------------------------------
  347. /**
  348. * Set the class name
  349. *
  350. * @access public
  351. * @param string
  352. * @return void
  353. */
  354. function set_class($class)
  355. {
  356. $this->class = str_replace(array('/', '.'), '', $class);
  357. }
  358. // --------------------------------------------------------------------
  359. /**
  360. * Fetch the current class
  361. *
  362. * @access public
  363. * @return string
  364. */
  365. function fetch_class()
  366. {
  367. return $this->class;
  368. }
  369. // --------------------------------------------------------------------
  370. /**
  371. * Set the method name
  372. *
  373. * @access public
  374. * @param string
  375. * @return void
  376. */
  377. function set_method($method)
  378. {
  379. $this->method = $method;
  380. }
  381. // --------------------------------------------------------------------
  382. /**
  383. * Fetch the current method
  384. *
  385. * @access public
  386. * @return string
  387. */
  388. function fetch_method()
  389. {
  390. if ($this->method == $this->fetch_class())
  391. {
  392. return 'index';
  393. }
  394. return $this->method;
  395. }
  396. // --------------------------------------------------------------------
  397. /**
  398. * Set the directory name
  399. *
  400. * @access public
  401. * @param string
  402. * @return void
  403. */
  404. function set_directory($dir)
  405. {
  406. $this->directory = str_replace(array('/', '.'), '', $dir).'/';
  407. }
  408. // --------------------------------------------------------------------
  409. /**
  410. * Fetch the sub-directory (if any) that contains the requested controller class
  411. *
  412. * @access public
  413. * @return string
  414. */
  415. function fetch_directory()
  416. {
  417. return $this->directory;
  418. }
  419. // --------------------------------------------------------------------
  420. /**
  421. * Set the controller overrides
  422. *
  423. * @access public
  424. * @param array
  425. * @return null
  426. */
  427. function _set_overrides($routing)
  428. {
  429. if ( ! is_array($routing))
  430. {
  431. return;
  432. }
  433. if (isset($routing['directory']))
  434. {
  435. $this->set_directory($routing['directory']);
  436. }
  437. if (isset($routing['controller']) AND $routing['controller'] != '')
  438. {
  439. $this->set_class($routing['controller']);
  440. }
  441. if (isset($routing['function']))
  442. {
  443. $routing['function'] = ($routing['function'] == '') ? 'index' : $routing['function'];
  444. $this->set_method($routing['function']);
  445. }
  446. }
  447. }
  448. // END Router Class
  449. /* End of file Router.php */
  450. /* Location: ./system/core/Router.php */