blob: d451aab68fc54ba969963fedc44e1e95c5693cac [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.
Phil Sturgeon05fa6112011-04-06 22:57:43 +010090 if (defined('ENVIRONMENT') AND is_file(APPPATH.'config/'.ENVIRONMENT.'/routes'.EXT))
bubbafoley0ea04142011-03-17 14:55:41 -050091 {
Eric Barnes92808342011-03-18 09:02:37 -040092 include(APPPATH.'config/'.ENVIRONMENT.'/routes'.EXT);
bubbafoley0ea04142011-03-17 14:55:41 -050093 }
Eric Barnesfdd5b112011-03-21 21:28:58 -040094 elseif (is_file(APPPATH.'config/routes'.EXT))
bubbafoley0ea04142011-03-17 14:55:41 -050095 {
Eric Barnesfdd5b112011-03-21 21:28:58 -040096 include(APPPATH.'config/routes'.EXT);
bubbafoley0ea04142011-03-17 14:55:41 -050097 }
Eric Barnesfdd5b112011-03-21 21:28:58 -040098
Derek Allard2067d1a2008-11-13 22:59:24 +000099 $this->routes = ( ! isset($route) OR ! is_array($route)) ? array() : $route;
100 unset($route);
Barry Mienydd671972010-10-04 16:33:58 +0200101
Derek Allard2067d1a2008-11-13 22:59:24 +0000102 // Set the default controller so we can display it in the event
103 // the URI doesn't correlated to a valid controller.
Barry Mienydd671972010-10-04 16:33:58 +0200104 $this->default_controller = ( ! isset($this->routes['default_controller']) OR $this->routes['default_controller'] == '') ? FALSE : strtolower($this->routes['default_controller']);
105
Derek Jonesc7738402010-03-02 13:55:13 -0600106 // Were there any query string segments? If so, we'll validate them and bail out since we're done.
107 if (count($segments) > 0)
108 {
109 return $this->_validate_request($segments);
110 }
Barry Mienydd671972010-10-04 16:33:58 +0200111
Derek Allard2067d1a2008-11-13 22:59:24 +0000112 // Fetch the complete URI string
113 $this->uri->_fetch_uri_string();
Barry Mienydd671972010-10-04 16:33:58 +0200114
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 // Is there a URI string? If not, the default controller specified in the "routes" file will be shown.
116 if ($this->uri->uri_string == '')
117 {
Derek Jonesc7738402010-03-02 13:55:13 -0600118 return $this->_set_default_controller();
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 }
Barry Mienydd671972010-10-04 16:33:58 +0200120
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 // Do we need to remove the URL suffix?
122 $this->uri->_remove_url_suffix();
Barry Mienydd671972010-10-04 16:33:58 +0200123
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 // Compile the segments into an array
125 $this->uri->_explode_segments();
Barry Mienydd671972010-10-04 16:33:58 +0200126
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 // Parse any custom routing that may exist
Barry Mienydd671972010-10-04 16:33:58 +0200128 $this->_parse_routes();
129
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 // Re-index the segment array so that it starts with 1 rather than 0
131 $this->uri->_reindex_segments();
132 }
Derek Jonesc7738402010-03-02 13:55:13 -0600133
134 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200135
Derek Jonesc7738402010-03-02 13:55:13 -0600136 /**
137 * Set the default controller
138 *
139 * @access private
140 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200141 */
Derek Jonesc7738402010-03-02 13:55:13 -0600142 function _set_default_controller()
143 {
144 if ($this->default_controller === FALSE)
145 {
146 show_error("Unable to determine what should be displayed. A default route has not been specified in the routing file.");
147 }
148 // Is the method being specified?
149 if (strpos($this->default_controller, '/') !== FALSE)
150 {
151 $x = explode('/', $this->default_controller);
Barry Mienydd671972010-10-04 16:33:58 +0200152
Derek Jonesc7738402010-03-02 13:55:13 -0600153 $this->set_class($x[0]);
154 $this->set_method($x[1]);
Pascal Kriete790ebf32010-12-15 10:53:35 -0500155 $this->_set_request($x);
Barry Mienydd671972010-10-04 16:33:58 +0200156 }
Derek Jonesc7738402010-03-02 13:55:13 -0600157 else
158 {
159 $this->set_class($this->default_controller);
160 $this->set_method('index');
161 $this->_set_request(array($this->default_controller, 'index'));
162 }
Barry Mienydd671972010-10-04 16:33:58 +0200163
Derek Jonesc7738402010-03-02 13:55:13 -0600164 // re-index the routed segments array so it starts with 1 rather than 0
165 $this->uri->_reindex_segments();
Barry Mienydd671972010-10-04 16:33:58 +0200166
Derek Jonesc7738402010-03-02 13:55:13 -0600167 log_message('debug', "No URI present. Default controller set.");
168 }
Barry Mienydd671972010-10-04 16:33:58 +0200169
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200171
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 /**
173 * Set the Route
174 *
175 * This function takes an array of URI segments as
176 * input, and sets the current class/method
177 *
178 * @access private
179 * @param array
180 * @param bool
181 * @return void
182 */
183 function _set_request($segments = array())
Barry Mienydd671972010-10-04 16:33:58 +0200184 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 $segments = $this->_validate_request($segments);
Barry Mienydd671972010-10-04 16:33:58 +0200186
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 if (count($segments) == 0)
188 {
Derek Jonesc7738402010-03-02 13:55:13 -0600189 return $this->_set_default_controller();
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 }
Barry Mienydd671972010-10-04 16:33:58 +0200191
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 $this->set_class($segments[0]);
Barry Mienydd671972010-10-04 16:33:58 +0200193
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 if (isset($segments[1]))
195 {
Derek Jonesc7738402010-03-02 13:55:13 -0600196 // A standard method request
197 $this->set_method($segments[1]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 }
199 else
200 {
201 // This lets the "routed" segment array identify that the default
202 // index method is being used.
203 $segments[1] = 'index';
204 }
Barry Mienydd671972010-10-04 16:33:58 +0200205
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 // Update our "routed" segment array to contain the segments.
207 // Note: If there is no custom routing, this array will be
208 // identical to $this->uri->segments
209 $this->uri->rsegments = $segments;
210 }
Barry Mienydd671972010-10-04 16:33:58 +0200211
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200213
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 /**
215 * Validates the supplied segments. Attempts to determine the path to
216 * the controller.
217 *
218 * @access private
219 * @param array
220 * @return array
Barry Mienydd671972010-10-04 16:33:58 +0200221 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 function _validate_request($segments)
223 {
Derek Jonesc7738402010-03-02 13:55:13 -0600224 if (count($segments) == 0)
225 {
226 return $segments;
227 }
Barry Mienydd671972010-10-04 16:33:58 +0200228
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 // Does the requested controller exist in the root folder?
230 if (file_exists(APPPATH.'controllers/'.$segments[0].EXT))
231 {
232 return $segments;
233 }
Barry Mienydd671972010-10-04 16:33:58 +0200234
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 // Is the controller in a sub-folder?
236 if (is_dir(APPPATH.'controllers/'.$segments[0]))
Derek Jonesc7738402010-03-02 13:55:13 -0600237 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 // Set the directory and remove it from the segment array
239 $this->set_directory($segments[0]);
240 $segments = array_slice($segments, 1);
Barry Mienydd671972010-10-04 16:33:58 +0200241
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 if (count($segments) > 0)
243 {
244 // Does the requested controller exist in the sub-folder?
245 if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].EXT))
246 {
Barry Mienydd671972010-10-04 16:33:58 +0200247 show_404($this->fetch_directory().$segments[0]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 }
249 }
250 else
251 {
Derek Jonesc7738402010-03-02 13:55:13 -0600252 // Is the method being specified in the route?
253 if (strpos($this->default_controller, '/') !== FALSE)
254 {
255 $x = explode('/', $this->default_controller);
Barry Mienydd671972010-10-04 16:33:58 +0200256
Derek Jonesc7738402010-03-02 13:55:13 -0600257 $this->set_class($x[0]);
258 $this->set_method($x[1]);
Barry Mienydd671972010-10-04 16:33:58 +0200259 }
Derek Jonesc7738402010-03-02 13:55:13 -0600260 else
261 {
262 $this->set_class($this->default_controller);
263 $this->set_method('index');
264 }
Barry Mienydd671972010-10-04 16:33:58 +0200265
Derek Allard2067d1a2008-11-13 22:59:24 +0000266 // Does the default controller exist in the sub-folder?
267 if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT))
268 {
269 $this->directory = '';
270 return array();
271 }
Barry Mienydd671972010-10-04 16:33:58 +0200272
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 }
Barry Mienydd671972010-10-04 16:33:58 +0200274
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 return $segments;
276 }
Barry Mienydd671972010-10-04 16:33:58 +0200277
278
Derek Jonesc7738402010-03-02 13:55:13 -0600279 // If we've gotten this far it means that the URI does not correlate to a valid
280 // controller class. We will now see if there is an override
Eric Barnesc5bf6162011-01-30 21:17:11 -0500281 if ( ! empty($this->routes['404_override']))
Derek Jonesc7738402010-03-02 13:55:13 -0600282 {
Phil Sturgeon23174a62010-12-15 15:18:16 +0000283 $x = explode('/', $this->routes['404_override']);
Barry Mienydd671972010-10-04 16:33:58 +0200284
Phil Sturgeon23174a62010-12-15 15:18:16 +0000285 $this->set_class($x[0]);
286 $this->set_method(isset($x[1]) ? $x[1] : 'index');
Barry Mienydd671972010-10-04 16:33:58 +0200287
Phil Sturgeon23174a62010-12-15 15:18:16 +0000288 return $x;
Derek Jonesc7738402010-03-02 13:55:13 -0600289 }
Barry Mienydd671972010-10-04 16:33:58 +0200290
Phil Sturgeon23174a62010-12-15 15:18:16 +0000291
Derek Jonesc7738402010-03-02 13:55:13 -0600292 // Nothing else to do at this point but show a 404
Barry Mienydd671972010-10-04 16:33:58 +0200293 show_404($segments[0]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 }
Barry Mienydd671972010-10-04 16:33:58 +0200295
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200297
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 /**
299 * Parse Routes
300 *
301 * This function matches any routes that may exist in
302 * the config/routes.php file against the URI to
303 * determine if the class/method need to be remapped.
304 *
305 * @access private
306 * @return void
307 */
308 function _parse_routes()
309 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 // Turn the segment array into a URI string
311 $uri = implode('/', $this->uri->segments);
Barry Mienydd671972010-10-04 16:33:58 +0200312
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 // Is there a literal match? If so we're done
314 if (isset($this->routes[$uri]))
315 {
Derek Jonesc7738402010-03-02 13:55:13 -0600316 return $this->_set_request(explode('/', $this->routes[$uri]));
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 }
Barry Mienydd671972010-10-04 16:33:58 +0200318
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 // Loop through the route array looking for wild-cards
320 foreach ($this->routes as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200321 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 // Convert wild-cards to RegEx
323 $key = str_replace(':any', '.+', str_replace(':num', '[0-9]+', $key));
Derek Jonesc7738402010-03-02 13:55:13 -0600324
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 // Does the RegEx match?
326 if (preg_match('#^'.$key.'$#', $uri))
Derek Jonesc7738402010-03-02 13:55:13 -0600327 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 // Do we have a back-reference?
329 if (strpos($val, '$') !== FALSE AND strpos($key, '(') !== FALSE)
330 {
331 $val = preg_replace('#^'.$key.'$#', $val, $uri);
332 }
Barry Mienydd671972010-10-04 16:33:58 +0200333
334 return $this->_set_request(explode('/', $val));
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 }
336 }
337
338 // If we got this far it means we didn't encounter a
339 // matching route so we'll set the site default route
340 $this->_set_request($this->uri->segments);
341 }
342
343 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200344
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 /**
346 * Set the class name
347 *
348 * @access public
349 * @param string
350 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200351 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 function set_class($class)
353 {
Derek Jones2615e412010-10-06 17:51:16 -0500354 $this->class = str_replace(array('/', '.'), '', $class);
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 }
Barry Mienydd671972010-10-04 16:33:58 +0200356
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200358
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 /**
360 * Fetch the current class
361 *
362 * @access public
363 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200364 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 function fetch_class()
366 {
367 return $this->class;
368 }
Barry Mienydd671972010-10-04 16:33:58 +0200369
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200371
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 /**
373 * Set the method name
374 *
375 * @access public
376 * @param string
377 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200378 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 function set_method($method)
380 {
381 $this->method = $method;
382 }
383
384 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200385
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 /**
387 * Fetch the current method
388 *
389 * @access public
390 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200391 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 function fetch_method()
393 {
394 if ($this->method == $this->fetch_class())
395 {
396 return 'index';
397 }
398
399 return $this->method;
400 }
401
402 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200403
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 /**
405 * Set the directory name
406 *
407 * @access public
408 * @param string
409 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200410 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 function set_directory($dir)
412 {
Derek Jones2615e412010-10-06 17:51:16 -0500413 $this->directory = str_replace(array('/', '.'), '', $dir).'/';
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 }
415
416 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200417
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 /**
419 * Fetch the sub-directory (if any) that contains the requested controller class
420 *
421 * @access public
422 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200423 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 function fetch_directory()
425 {
426 return $this->directory;
427 }
428
Derek Jonesc7738402010-03-02 13:55:13 -0600429 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200430
Derek Jonesc7738402010-03-02 13:55:13 -0600431 /**
432 * Set the controller overrides
433 *
434 * @access public
435 * @param array
436 * @return null
Barry Mienydd671972010-10-04 16:33:58 +0200437 */
Derek Jonesc7738402010-03-02 13:55:13 -0600438 function _set_overrides($routing)
439 {
440 if ( ! is_array($routing))
441 {
442 return;
443 }
Barry Mienydd671972010-10-04 16:33:58 +0200444
Derek Jonesc7738402010-03-02 13:55:13 -0600445 if (isset($routing['directory']))
446 {
447 $this->set_directory($routing['directory']);
448 }
Barry Mienydd671972010-10-04 16:33:58 +0200449
Derek Jonesc7738402010-03-02 13:55:13 -0600450 if (isset($routing['controller']) AND $routing['controller'] != '')
451 {
452 $this->set_class($routing['controller']);
453 }
Barry Mienydd671972010-10-04 16:33:58 +0200454
Derek Jonesc7738402010-03-02 13:55:13 -0600455 if (isset($routing['function']))
456 {
457 $routing['function'] = ($routing['function'] == '') ? 'index' : $routing['function'];
458 $this->set_method($routing['function']);
459 }
460 }
461
462
Derek Allard2067d1a2008-11-13 22:59:24 +0000463}
464// END Router Class
465
466/* End of file Router.php */
Derek Jonesc68dfbf2010-03-02 12:59:23 -0600467/* Location: ./system/core/Router.php */