1
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
5
* An open source application development framework for PHP 5.1.6 or newer
8
* @author ExpressionEngine Dev Team
9
* @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
10
* @license http://codeigniter.com/user_guide/license.html
11
* @link http://codeigniter.com
16
// ------------------------------------------------------------------------
21
* Parses URIs and determines routing
23
* @package CodeIgniter
24
* @subpackage Libraries
25
* @author ExpressionEngine Dev Team
27
* @link http://codeigniter.com/user_guide/general/routing.html
44
var $routes = array();
46
* List of error routes
51
var $error_routes = array();
65
var $method = 'index';
67
* Sub-directory that contains the requested controller class
74
* Default controller (and method if specific)
79
var $default_controller;
84
* Runs the route mapping function.
86
function __construct()
88
$this->config =& load_class('Config', 'core');
89
$this->uri =& load_class('URI', 'core');
90
log_message('debug', "Router Class Initialized");
93
// --------------------------------------------------------------------
96
* Set the route mapping
98
* This function determines what should be served based on the URI request,
99
* as well as any "routes" that have been set in the routing config file.
104
function _set_routing()
106
// Are query strings enabled in the config file? Normally CI doesn't utilize query strings
107
// since URI segments are more search-engine friendly, but they can optionally be used.
108
// If this feature is enabled, we will gather the directory/class/method a little differently
110
if ($this->config->item('enable_query_strings') === TRUE AND isset($_GET[$this->config->item('controller_trigger')]))
112
if (isset($_GET[$this->config->item('directory_trigger')]))
114
$this->set_directory(trim($this->uri->_filter_uri($_GET[$this->config->item('directory_trigger')])));
115
$segments[] = $this->fetch_directory();
118
if (isset($_GET[$this->config->item('controller_trigger')]))
120
$this->set_class(trim($this->uri->_filter_uri($_GET[$this->config->item('controller_trigger')])));
121
$segments[] = $this->fetch_class();
124
if (isset($_GET[$this->config->item('function_trigger')]))
126
$this->set_method(trim($this->uri->_filter_uri($_GET[$this->config->item('function_trigger')])));
127
$segments[] = $this->fetch_method();
131
// Load the routes.php file.
132
if (defined('ENVIRONMENT') AND is_file(APPPATH.'config/'.ENVIRONMENT.'/routes.php'))
134
include(APPPATH.'config/'.ENVIRONMENT.'/routes.php');
136
elseif (is_file(APPPATH.'config/routes.php'))
138
include(APPPATH.'config/routes.php');
141
$this->routes = ( ! isset($route) OR ! is_array($route)) ? array() : $route;
144
// Set the default controller so we can display it in the event
145
// the URI doesn't correlated to a valid controller.
146
$this->default_controller = ( ! isset($this->routes['default_controller']) OR $this->routes['default_controller'] == '') ? FALSE : strtolower($this->routes['default_controller']);
148
// Were there any query string segments? If so, we'll validate them and bail out since we're done.
149
if (count($segments) > 0)
151
return $this->_validate_request($segments);
154
// Fetch the complete URI string
155
$this->uri->_fetch_uri_string();
157
// Is there a URI string? If not, the default controller specified in the "routes" file will be shown.
158
if ($this->uri->uri_string == '')
160
return $this->_set_default_controller();
163
// Do we need to remove the URL suffix?
164
$this->uri->_remove_url_suffix();
166
// Compile the segments into an array
167
$this->uri->_explode_segments();
169
// Parse any custom routing that may exist
170
$this->_parse_routes();
172
// Re-index the segment array so that it starts with 1 rather than 0
173
$this->uri->_reindex_segments();
176
// --------------------------------------------------------------------
179
* Set the default controller
184
function _set_default_controller()
186
if ($this->default_controller === FALSE)
188
show_error("Unable to determine what should be displayed. A default route has not been specified in the routing file.");
190
// Is the method being specified?
191
if (strpos($this->default_controller, '/') !== FALSE)
193
$x = explode('/', $this->default_controller);
195
$this->set_class($x[0]);
196
$this->set_method($x[1]);
197
$this->_set_request($x);
201
$this->set_class($this->default_controller);
202
$this->set_method('index');
203
$this->_set_request(array($this->default_controller, 'index'));
206
// re-index the routed segments array so it starts with 1 rather than 0
207
$this->uri->_reindex_segments();
209
log_message('debug', "No URI present. Default controller set.");
212
// --------------------------------------------------------------------
217
* This function takes an array of URI segments as
218
* input, and sets the current class/method
225
function _set_request($segments = array())
227
$segments = $this->_validate_request($segments);
229
if (count($segments) == 0)
231
return $this->_set_default_controller();
234
$this->set_class($segments[0]);
236
if (isset($segments[1]))
238
// A standard method request
239
$this->set_method($segments[1]);
243
// This lets the "routed" segment array identify that the default
244
// index method is being used.
245
$segments[1] = 'index';
248
// Update our "routed" segment array to contain the segments.
249
// Note: If there is no custom routing, this array will be
250
// identical to $this->uri->segments
251
$this->uri->rsegments = $segments;
254
// --------------------------------------------------------------------
257
* Validates the supplied segments. Attempts to determine the path to
264
function _validate_request($segments)
266
if (count($segments) == 0)
271
// Does the requested controller exist in the root folder?
272
if (file_exists(APPPATH.'controllers/'.$segments[0].'.php'))
277
// Is the controller in a sub-folder?
278
if (is_dir(APPPATH.'controllers/'.$segments[0]))
280
// Set the directory and remove it from the segment array
281
$this->set_directory($segments[0]);
282
$segments = array_slice($segments, 1);
284
if (count($segments) > 0)
286
// Does the requested controller exist in the sub-folder?
287
if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].'.php'))
289
if ( ! empty($this->routes['404_override']))
291
$x = explode('/', $this->routes['404_override']);
293
$this->set_directory('');
294
$this->set_class($x[0]);
295
$this->set_method(isset($x[1]) ? $x[1] : 'index');
301
show_404($this->fetch_directory().$segments[0]);
307
// Is the method being specified in the route?
308
if (strpos($this->default_controller, '/') !== FALSE)
310
$x = explode('/', $this->default_controller);
312
$this->set_class($x[0]);
313
$this->set_method($x[1]);
317
$this->set_class($this->default_controller);
318
$this->set_method('index');
321
// Does the default controller exist in the sub-folder?
322
if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.'.php'))
324
$this->directory = '';
334
// If we've gotten this far it means that the URI does not correlate to a valid
335
// controller class. We will now see if there is an override
336
if ( ! empty($this->routes['404_override']))
338
$x = explode('/', $this->routes['404_override']);
340
$this->set_class($x[0]);
341
$this->set_method(isset($x[1]) ? $x[1] : 'index');
347
// Nothing else to do at this point but show a 404
348
show_404($segments[0]);
351
// --------------------------------------------------------------------
356
* This function matches any routes that may exist in
357
* the config/routes.php file against the URI to
358
* determine if the class/method need to be remapped.
363
function _parse_routes()
365
// Turn the segment array into a URI string
366
$uri = implode('/', $this->uri->segments);
368
// Is there a literal match? If so we're done
369
if (isset($this->routes[$uri]))
371
return $this->_set_request(explode('/', $this->routes[$uri]));
374
// Loop through the route array looking for wild-cards
375
foreach ($this->routes as $key => $val)
377
// Convert wild-cards to RegEx
378
$key = str_replace(':any', '.+', str_replace(':num', '[0-9]+', $key));
380
// Does the RegEx match?
381
if (preg_match('#^'.$key.'$#', $uri))
383
// Do we have a back-reference?
384
if (strpos($val, '$') !== FALSE AND strpos($key, '(') !== FALSE)
386
$val = preg_replace('#^'.$key.'$#', $val, $uri);
389
return $this->_set_request(explode('/', $val));
393
// If we got this far it means we didn't encounter a
394
// matching route so we'll set the site default route
395
$this->_set_request($this->uri->segments);
398
// --------------------------------------------------------------------
407
function set_class($class)
409
$this->class = str_replace(array('/', '.'), '', $class);
412
// --------------------------------------------------------------------
415
* Fetch the current class
420
function fetch_class()
425
// --------------------------------------------------------------------
428
* Set the method name
434
function set_method($method)
436
$this->method = $method;
439
// --------------------------------------------------------------------
442
* Fetch the current method
447
function fetch_method()
449
if ($this->method == $this->fetch_class())
454
return $this->method;
457
// --------------------------------------------------------------------
460
* Set the directory name
466
function set_directory($dir)
468
$this->directory = str_replace(array('/', '.'), '', $dir).'/';
471
// --------------------------------------------------------------------
474
* Fetch the sub-directory (if any) that contains the requested controller class
479
function fetch_directory()
481
return $this->directory;
484
// --------------------------------------------------------------------
487
* Set the controller overrides
493
function _set_overrides($routing)
495
if ( ! is_array($routing))
500
if (isset($routing['directory']))
502
$this->set_directory($routing['directory']);
505
if (isset($routing['controller']) AND $routing['controller'] != '')
507
$this->set_class($routing['controller']);
510
if (isset($routing['function']))
512
$routing['function'] = ($routing['function'] == '') ? 'index' : $routing['function'];
513
$this->set_method($routing['function']);
521
/* End of file Router.php */
522
/* Location: ./system/core/Router.php */
b'\\ No newline at end of file'