blob: 6893e6e92eb0666986969771c8b3b70416371183 [file] [log] [blame]
Derek Allard2067d1a2008-11-13 22:59:24 +00001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
7 * @package CodeIgniter
8 * @author ExpressionEngine Dev Team
Greg Aker0711dc82011-01-05 10:49:40 -06009 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
Derek Allard2067d1a2008-11-13 22:59:24 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * Router Class
20 *
21 * Parses URIs and determines routing
22 *
23 * @package CodeIgniter
24 * @subpackage Libraries
25 * @author ExpressionEngine Dev Team
26 * @category Libraries
27 * @link http://codeigniter.com/user_guide/general/routing.html
28 */
29class CI_Router {
30
Barry Mienydd671972010-10-04 16:33:58 +020031 var $config;
32 var $routes = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000033 var $error_routes = array();
34 var $class = '';
35 var $method = 'index';
36 var $directory = '';
Derek Allard2067d1a2008-11-13 22:59:24 +000037 var $default_controller;
Barry Mienydd671972010-10-04 16:33:58 +020038
Derek Allard2067d1a2008-11-13 22:59:24 +000039 /**
40 * Constructor
41 *
42 * Runs the route mapping function.
43 */
Greg Akera9263282010-11-10 15:26:43 -060044 function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000045 {
Derek Jonesc7738402010-03-02 13:55:13 -060046 $this->config =& load_class('Config', 'core');
47 $this->uri =& load_class('URI', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +000048 log_message('debug', "Router Class Initialized");
49 }
Barry Mienydd671972010-10-04 16:33:58 +020050
Derek Allard2067d1a2008-11-13 22:59:24 +000051 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020052
Derek Allard2067d1a2008-11-13 22:59:24 +000053 /**
54 * Set the route mapping
55 *
56 * This function determines what should be served based on the URI request,
57 * as well as any "routes" that have been set in the routing config file.
58 *
59 * @access private
60 * @return void
61 */
62 function _set_routing()
Barry Mienydd671972010-10-04 16:33:58 +020063 {
Derek Jonesc7738402010-03-02 13:55:13 -060064 // Are query strings enabled in the config file? Normally CI doesn't utilize query strings
Barry Mienydd671972010-10-04 16:33:58 +020065 // since URI segments are more search-engine friendly, but they can optionally be used.
Derek Jonesc7738402010-03-02 13:55:13 -060066 // If this feature is enabled, we will gather the directory/class/method a little differently
67 $segments = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000068 if ($this->config->item('enable_query_strings') === TRUE AND isset($_GET[$this->config->item('controller_trigger')]))
69 {
Derek Jonesc7738402010-03-02 13:55:13 -060070 if (isset($_GET[$this->config->item('directory_trigger')]))
71 {
72 $this->set_directory(trim($this->uri->_filter_uri($_GET[$this->config->item('directory_trigger')])));
73 $segments[] = $this->fetch_directory();
74 }
Barry Mienydd671972010-10-04 16:33:58 +020075
Derek Jonesc7738402010-03-02 13:55:13 -060076 if (isset($_GET[$this->config->item('controller_trigger')]))
77 {
78 $this->set_class(trim($this->uri->_filter_uri($_GET[$this->config->item('controller_trigger')])));
79 $segments[] = $this->fetch_class();
80 }
Barry Mienydd671972010-10-04 16:33:58 +020081
Derek Allard2067d1a2008-11-13 22:59:24 +000082 if (isset($_GET[$this->config->item('function_trigger')]))
83 {
84 $this->set_method(trim($this->uri->_filter_uri($_GET[$this->config->item('function_trigger')])));
Derek Jonesc7738402010-03-02 13:55:13 -060085 $segments[] = $this->fetch_method();
Derek Allard2067d1a2008-11-13 22:59:24 +000086 }
Derek Allard2067d1a2008-11-13 22:59:24 +000087 }
Barry Mienydd671972010-10-04 16:33:58 +020088
Derek Allard2067d1a2008-11-13 22:59:24 +000089 // Load the routes.php file.
90 @include(APPPATH.'config/routes'.EXT);
91 $this->routes = ( ! isset($route) OR ! is_array($route)) ? array() : $route;
92 unset($route);
Barry Mienydd671972010-10-04 16:33:58 +020093
Derek Allard2067d1a2008-11-13 22:59:24 +000094 // Set the default controller so we can display it in the event
95 // the URI doesn't correlated to a valid controller.
Barry Mienydd671972010-10-04 16:33:58 +020096 $this->default_controller = ( ! isset($this->routes['default_controller']) OR $this->routes['default_controller'] == '') ? FALSE : strtolower($this->routes['default_controller']);
97
Derek Jonesc7738402010-03-02 13:55:13 -060098 // Were there any query string segments? If so, we'll validate them and bail out since we're done.
99 if (count($segments) > 0)
100 {
101 return $this->_validate_request($segments);
102 }
Barry Mienydd671972010-10-04 16:33:58 +0200103
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 // Fetch the complete URI string
105 $this->uri->_fetch_uri_string();
Barry Mienydd671972010-10-04 16:33:58 +0200106
Derek Allard2067d1a2008-11-13 22:59:24 +0000107 // Is there a URI string? If not, the default controller specified in the "routes" file will be shown.
108 if ($this->uri->uri_string == '')
109 {
Derek Jonesc7738402010-03-02 13:55:13 -0600110 return $this->_set_default_controller();
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 }
Barry Mienydd671972010-10-04 16:33:58 +0200112
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 // Do we need to remove the URL suffix?
114 $this->uri->_remove_url_suffix();
Barry Mienydd671972010-10-04 16:33:58 +0200115
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 // Compile the segments into an array
117 $this->uri->_explode_segments();
Barry Mienydd671972010-10-04 16:33:58 +0200118
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 // Parse any custom routing that may exist
Barry Mienydd671972010-10-04 16:33:58 +0200120 $this->_parse_routes();
121
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 // Re-index the segment array so that it starts with 1 rather than 0
123 $this->uri->_reindex_segments();
124 }
Derek Jonesc7738402010-03-02 13:55:13 -0600125
126 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200127
Derek Jonesc7738402010-03-02 13:55:13 -0600128 /**
129 * Set the default controller
130 *
131 * @access private
132 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200133 */
Derek Jonesc7738402010-03-02 13:55:13 -0600134 function _set_default_controller()
135 {
136 if ($this->default_controller === FALSE)
137 {
138 show_error("Unable to determine what should be displayed. A default route has not been specified in the routing file.");
139 }
140 // Is the method being specified?
141 if (strpos($this->default_controller, '/') !== FALSE)
142 {
143 $x = explode('/', $this->default_controller);
Barry Mienydd671972010-10-04 16:33:58 +0200144
Derek Jonesc7738402010-03-02 13:55:13 -0600145 $this->set_class($x[0]);
146 $this->set_method($x[1]);
Pascal Kriete790ebf32010-12-15 10:53:35 -0500147 $this->_set_request($x);
Barry Mienydd671972010-10-04 16:33:58 +0200148 }
Derek Jonesc7738402010-03-02 13:55:13 -0600149 else
150 {
151 $this->set_class($this->default_controller);
152 $this->set_method('index');
153 $this->_set_request(array($this->default_controller, 'index'));
154 }
Barry Mienydd671972010-10-04 16:33:58 +0200155
Derek Jonesc7738402010-03-02 13:55:13 -0600156 // re-index the routed segments array so it starts with 1 rather than 0
157 $this->uri->_reindex_segments();
Barry Mienydd671972010-10-04 16:33:58 +0200158
Derek Jonesc7738402010-03-02 13:55:13 -0600159 log_message('debug', "No URI present. Default controller set.");
160 }
Barry Mienydd671972010-10-04 16:33:58 +0200161
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200163
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 /**
165 * Set the Route
166 *
167 * This function takes an array of URI segments as
168 * input, and sets the current class/method
169 *
170 * @access private
171 * @param array
172 * @param bool
173 * @return void
174 */
175 function _set_request($segments = array())
Barry Mienydd671972010-10-04 16:33:58 +0200176 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 $segments = $this->_validate_request($segments);
Barry Mienydd671972010-10-04 16:33:58 +0200178
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 if (count($segments) == 0)
180 {
Derek Jonesc7738402010-03-02 13:55:13 -0600181 return $this->_set_default_controller();
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 }
Barry Mienydd671972010-10-04 16:33:58 +0200183
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 $this->set_class($segments[0]);
Barry Mienydd671972010-10-04 16:33:58 +0200185
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 if (isset($segments[1]))
187 {
Derek Jonesc7738402010-03-02 13:55:13 -0600188 // A standard method request
189 $this->set_method($segments[1]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 }
191 else
192 {
193 // This lets the "routed" segment array identify that the default
194 // index method is being used.
195 $segments[1] = 'index';
196 }
Barry Mienydd671972010-10-04 16:33:58 +0200197
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 // Update our "routed" segment array to contain the segments.
199 // Note: If there is no custom routing, this array will be
200 // identical to $this->uri->segments
201 $this->uri->rsegments = $segments;
202 }
Barry Mienydd671972010-10-04 16:33:58 +0200203
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200205
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 /**
207 * Validates the supplied segments. Attempts to determine the path to
208 * the controller.
209 *
210 * @access private
211 * @param array
212 * @return array
Barry Mienydd671972010-10-04 16:33:58 +0200213 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 function _validate_request($segments)
215 {
Derek Jonesc7738402010-03-02 13:55:13 -0600216 if (count($segments) == 0)
217 {
218 return $segments;
219 }
Barry Mienydd671972010-10-04 16:33:58 +0200220
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 // Does the requested controller exist in the root folder?
222 if (file_exists(APPPATH.'controllers/'.$segments[0].EXT))
223 {
224 return $segments;
225 }
Barry Mienydd671972010-10-04 16:33:58 +0200226
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 // Is the controller in a sub-folder?
228 if (is_dir(APPPATH.'controllers/'.$segments[0]))
Derek Jonesc7738402010-03-02 13:55:13 -0600229 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 // Set the directory and remove it from the segment array
231 $this->set_directory($segments[0]);
232 $segments = array_slice($segments, 1);
Barry Mienydd671972010-10-04 16:33:58 +0200233
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 if (count($segments) > 0)
235 {
236 // Does the requested controller exist in the sub-folder?
237 if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].EXT))
238 {
Barry Mienydd671972010-10-04 16:33:58 +0200239 show_404($this->fetch_directory().$segments[0]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 }
241 }
242 else
243 {
Derek Jonesc7738402010-03-02 13:55:13 -0600244 // Is the method being specified in the route?
245 if (strpos($this->default_controller, '/') !== FALSE)
246 {
247 $x = explode('/', $this->default_controller);
Barry Mienydd671972010-10-04 16:33:58 +0200248
Derek Jonesc7738402010-03-02 13:55:13 -0600249 $this->set_class($x[0]);
250 $this->set_method($x[1]);
Barry Mienydd671972010-10-04 16:33:58 +0200251 }
Derek Jonesc7738402010-03-02 13:55:13 -0600252 else
253 {
254 $this->set_class($this->default_controller);
255 $this->set_method('index');
256 }
Barry Mienydd671972010-10-04 16:33:58 +0200257
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 // Does the default controller exist in the sub-folder?
259 if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT))
260 {
261 $this->directory = '';
262 return array();
263 }
Barry Mienydd671972010-10-04 16:33:58 +0200264
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 }
Barry Mienydd671972010-10-04 16:33:58 +0200266
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 return $segments;
268 }
Barry Mienydd671972010-10-04 16:33:58 +0200269
270
Derek Jonesc7738402010-03-02 13:55:13 -0600271 // If we've gotten this far it means that the URI does not correlate to a valid
272 // controller class. We will now see if there is an override
Eric Barnesc5bf6162011-01-30 21:17:11 -0500273 if ( ! empty($this->routes['404_override']))
Derek Jonesc7738402010-03-02 13:55:13 -0600274 {
Phil Sturgeon23174a62010-12-15 15:18:16 +0000275 $x = explode('/', $this->routes['404_override']);
Barry Mienydd671972010-10-04 16:33:58 +0200276
Phil Sturgeon23174a62010-12-15 15:18:16 +0000277 $this->set_class($x[0]);
278 $this->set_method(isset($x[1]) ? $x[1] : 'index');
Barry Mienydd671972010-10-04 16:33:58 +0200279
Phil Sturgeon23174a62010-12-15 15:18:16 +0000280 return $x;
Derek Jonesc7738402010-03-02 13:55:13 -0600281 }
Barry Mienydd671972010-10-04 16:33:58 +0200282
Phil Sturgeon23174a62010-12-15 15:18:16 +0000283
Derek Jonesc7738402010-03-02 13:55:13 -0600284 // Nothing else to do at this point but show a 404
Barry Mienydd671972010-10-04 16:33:58 +0200285 show_404($segments[0]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 }
Barry Mienydd671972010-10-04 16:33:58 +0200287
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200289
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 /**
291 * Parse Routes
292 *
293 * This function matches any routes that may exist in
294 * the config/routes.php file against the URI to
295 * determine if the class/method need to be remapped.
296 *
297 * @access private
298 * @return void
299 */
300 function _parse_routes()
301 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 // Turn the segment array into a URI string
303 $uri = implode('/', $this->uri->segments);
Barry Mienydd671972010-10-04 16:33:58 +0200304
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 // Is there a literal match? If so we're done
306 if (isset($this->routes[$uri]))
307 {
Derek Jonesc7738402010-03-02 13:55:13 -0600308 return $this->_set_request(explode('/', $this->routes[$uri]));
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 }
Barry Mienydd671972010-10-04 16:33:58 +0200310
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 // Loop through the route array looking for wild-cards
312 foreach ($this->routes as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200313 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 // Convert wild-cards to RegEx
315 $key = str_replace(':any', '.+', str_replace(':num', '[0-9]+', $key));
Derek Jonesc7738402010-03-02 13:55:13 -0600316
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 // Does the RegEx match?
318 if (preg_match('#^'.$key.'$#', $uri))
Derek Jonesc7738402010-03-02 13:55:13 -0600319 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 // Do we have a back-reference?
321 if (strpos($val, '$') !== FALSE AND strpos($key, '(') !== FALSE)
322 {
323 $val = preg_replace('#^'.$key.'$#', $val, $uri);
324 }
Barry Mienydd671972010-10-04 16:33:58 +0200325
326 return $this->_set_request(explode('/', $val));
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 }
328 }
329
330 // If we got this far it means we didn't encounter a
331 // matching route so we'll set the site default route
332 $this->_set_request($this->uri->segments);
333 }
334
335 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200336
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 /**
338 * Set the class name
339 *
340 * @access public
341 * @param string
342 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200343 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 function set_class($class)
345 {
Derek Jones2615e412010-10-06 17:51:16 -0500346 $this->class = str_replace(array('/', '.'), '', $class);
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 }
Barry Mienydd671972010-10-04 16:33:58 +0200348
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200350
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 /**
352 * Fetch the current class
353 *
354 * @access public
355 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200356 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 function fetch_class()
358 {
359 return $this->class;
360 }
Barry Mienydd671972010-10-04 16:33:58 +0200361
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200363
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 /**
365 * Set the method name
366 *
367 * @access public
368 * @param string
369 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200370 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 function set_method($method)
372 {
373 $this->method = $method;
374 }
375
376 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200377
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 /**
379 * Fetch the current method
380 *
381 * @access public
382 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200383 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 function fetch_method()
385 {
386 if ($this->method == $this->fetch_class())
387 {
388 return 'index';
389 }
390
391 return $this->method;
392 }
393
394 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200395
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 /**
397 * Set the directory name
398 *
399 * @access public
400 * @param string
401 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200402 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 function set_directory($dir)
404 {
Derek Jones2615e412010-10-06 17:51:16 -0500405 $this->directory = str_replace(array('/', '.'), '', $dir).'/';
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 }
407
408 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200409
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 /**
411 * Fetch the sub-directory (if any) that contains the requested controller class
412 *
413 * @access public
414 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200415 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 function fetch_directory()
417 {
418 return $this->directory;
419 }
420
Derek Jonesc7738402010-03-02 13:55:13 -0600421 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200422
Derek Jonesc7738402010-03-02 13:55:13 -0600423 /**
424 * Set the controller overrides
425 *
426 * @access public
427 * @param array
428 * @return null
Barry Mienydd671972010-10-04 16:33:58 +0200429 */
Derek Jonesc7738402010-03-02 13:55:13 -0600430 function _set_overrides($routing)
431 {
432 if ( ! is_array($routing))
433 {
434 return;
435 }
Barry Mienydd671972010-10-04 16:33:58 +0200436
Derek Jonesc7738402010-03-02 13:55:13 -0600437 if (isset($routing['directory']))
438 {
439 $this->set_directory($routing['directory']);
440 }
Barry Mienydd671972010-10-04 16:33:58 +0200441
Derek Jonesc7738402010-03-02 13:55:13 -0600442 if (isset($routing['controller']) AND $routing['controller'] != '')
443 {
444 $this->set_class($routing['controller']);
445 }
Barry Mienydd671972010-10-04 16:33:58 +0200446
Derek Jonesc7738402010-03-02 13:55:13 -0600447 if (isset($routing['function']))
448 {
449 $routing['function'] = ($routing['function'] == '') ? 'index' : $routing['function'];
450 $this->set_method($routing['function']);
451 }
452 }
453
454
Derek Allard2067d1a2008-11-13 22:59:24 +0000455}
456// END Router Class
457
458/* End of file Router.php */
Derek Jonesc68dfbf2010-03-02 12:59:23 -0600459/* Location: ./system/core/Router.php */