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
32 var $routes = array();
33 var $error_routes = array();
35 var $method = 'index';
37 var $default_controller;
42 * Runs the route mapping function.
44 function __construct()
46 $this->config =& load_class('Config', 'core');
47 $this->uri =& load_class('URI', 'core');
48 log_message('debug', "Router Class Initialized");
51 // --------------------------------------------------------------------
54 * Set the route mapping
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.
62 function _set_routing()
64 // Are query strings enabled in the config file? Normally CI doesn't utilize query strings
65 // since URI segments are more search-engine friendly, but they can optionally be used.
66 // If this feature is enabled, we will gather the directory/class/method a little differently
68 if ($this->config->item('enable_query_strings') === TRUE AND isset($_GET[$this->config->item('controller_trigger')]))
70 if (isset($_GET[$this->config->item('directory_trigger')]))
72 $this->set_directory(trim($this->uri->_filter_uri($_GET[$this->config->item('directory_trigger')])));
73 $segments[] = $this->fetch_directory();
76 if (isset($_GET[$this->config->item('controller_trigger')]))
78 $this->set_class(trim($this->uri->_filter_uri($_GET[$this->config->item('controller_trigger')])));
79 $segments[] = $this->fetch_class();
82 if (isset($_GET[$this->config->item('function_trigger')]))
84 $this->set_method(trim($this->uri->_filter_uri($_GET[$this->config->item('function_trigger')])));
85 $segments[] = $this->fetch_method();
89 // Load the routes.php file.
90 if (defined('ENVIRONMENT') AND is_file(APPPATH.'config/'.ENVIRONMENT.'/routes'.EXT))
92 include(APPPATH.'config/'.ENVIRONMENT.'/routes'.EXT);
94 elseif (is_file(APPPATH.'config/routes'.EXT))
96 include(APPPATH.'config/routes'.EXT);
99 $this->routes = ( ! isset($route) OR ! is_array($route)) ? array() : $route;
102 // Set the default controller so we can display it in the event
103 // the URI doesn't correlated to a valid controller.
104 $this->default_controller = ( ! isset($this->routes['default_controller']) OR $this->routes['default_controller'] == '') ? FALSE : strtolower($this->routes['default_controller']);
106 // Were there any query string segments? If so, we'll validate them and bail out since we're done.
107 if (count($segments) > 0)
109 return $this->_validate_request($segments);
112 // Fetch the complete URI string
113 $this->uri->_fetch_uri_string();
115 // Is there a URI string? If not, the default controller specified in the "routes" file will be shown.
116 if ($this->uri->uri_string == '')
118 return $this->_set_default_controller();
121 // Do we need to remove the URL suffix?
122 $this->uri->_remove_url_suffix();
124 // Compile the segments into an array
125 $this->uri->_explode_segments();
127 // Parse any custom routing that may exist
128 $this->_parse_routes();
130 // Re-index the segment array so that it starts with 1 rather than 0
131 $this->uri->_reindex_segments();
134 // --------------------------------------------------------------------
137 * Set the default controller
142 function _set_default_controller()
144 if ($this->default_controller === FALSE)
146 show_error("Unable to determine what should be displayed. A default route has not been specified in the routing file.");
148 // Is the method being specified?
149 if (strpos($this->default_controller, '/') !== FALSE)
151 $x = explode('/', $this->default_controller);
153 $this->set_class($x[0]);
154 $this->set_method($x[1]);
155 $this->_set_request($x);
159 $this->set_class($this->default_controller);
160 $this->set_method('index');
161 $this->_set_request(array($this->default_controller, 'index'));
164 // re-index the routed segments array so it starts with 1 rather than 0
165 $this->uri->_reindex_segments();
167 log_message('debug', "No URI present. Default controller set.");
170 // --------------------------------------------------------------------
175 * This function takes an array of URI segments as
176 * input, and sets the current class/method
183 function _set_request($segments = array())
185 $segments = $this->_validate_request($segments);
187 if (count($segments) == 0)
189 return $this->_set_default_controller();
192 $this->set_class($segments[0]);
194 if (isset($segments[1]))
196 // A standard method request
197 $this->set_method($segments[1]);
201 // This lets the "routed" segment array identify that the default
202 // index method is being used.
203 $segments[1] = 'index';
206 // 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;
212 // --------------------------------------------------------------------
215 * Validates the supplied segments. Attempts to determine the path to
222 function _validate_request($segments)
224 if (count($segments) == 0)
229 // Does the requested controller exist in the root folder?
230 if (file_exists(APPPATH.'controllers/'.$segments[0].EXT))
235 // Is the controller in a sub-folder?
236 if (is_dir(APPPATH.'controllers/'.$segments[0]))
238 // Set the directory and remove it from the segment array
239 $this->set_directory($segments[0]);
240 $segments = array_slice($segments, 1);
242 if (count($segments) > 0)
244 // Does the requested controller exist in the sub-folder?
245 if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].EXT))
247 show_404($this->fetch_directory().$segments[0]);
252 // Is the method being specified in the route?
253 if (strpos($this->default_controller, '/') !== FALSE)
255 $x = explode('/', $this->default_controller);
257 $this->set_class($x[0]);
258 $this->set_method($x[1]);
262 $this->set_class($this->default_controller);
263 $this->set_method('index');
266 // Does the default controller exist in the sub-folder?
267 if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT))
269 $this->directory = '';
279 // 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
281 if ( ! empty($this->routes['404_override']))
283 $x = explode('/', $this->routes['404_override']);
285 $this->set_class($x[0]);
286 $this->set_method(isset($x[1]) ? $x[1] : 'index');
292 // Nothing else to do at this point but show a 404
293 show_404($segments[0]);
296 // --------------------------------------------------------------------
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.
308 function _parse_routes()
310 // Turn the segment array into a URI string
311 $uri = implode('/', $this->uri->segments);
313 // Is there a literal match? If so we're done
314 if (isset($this->routes[$uri]))
316 return $this->_set_request(explode('/', $this->routes[$uri]));
319 // Loop through the route array looking for wild-cards
320 foreach ($this->routes as $key => $val)
322 // Convert wild-cards to RegEx
323 $key = str_replace(':any', '.+', str_replace(':num', '[0-9]+', $key));
325 // Does the RegEx match?
326 if (preg_match('#^'.$key.'$#', $uri))
328 // Do we have a back-reference?
329 if (strpos($val, '$') !== FALSE AND strpos($key, '(') !== FALSE)
331 $val = preg_replace('#^'.$key.'$#', $val, $uri);
334 return $this->_set_request(explode('/', $val));
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);
343 // --------------------------------------------------------------------
352 function set_class($class)
354 $this->class = str_replace(array('/', '.'), '', $class);
357 // --------------------------------------------------------------------
360 * Fetch the current class
365 function fetch_class()
370 // --------------------------------------------------------------------
373 * Set the method name
379 function set_method($method)
381 $this->method = $method;
384 // --------------------------------------------------------------------
387 * Fetch the current method
392 function fetch_method()
394 if ($this->method == $this->fetch_class())
399 return $this->method;
402 // --------------------------------------------------------------------
405 * Set the directory name
411 function set_directory($dir)
413 $this->directory = str_replace(array('/', '.'), '', $dir).'/';
416 // --------------------------------------------------------------------
419 * Fetch the sub-directory (if any) that contains the requested controller class
424 function fetch_directory()
426 return $this->directory;
429 // --------------------------------------------------------------------
432 * Set the controller overrides
438 function _set_overrides($routing)
440 if ( ! is_array($routing))
445 if (isset($routing['directory']))
447 $this->set_directory($routing['directory']);
450 if (isset($routing['controller']) AND $routing['controller'] != '')
452 $this->set_class($routing['controller']);
455 if (isset($routing['function']))
457 $routing['function'] = ($routing['function'] == '') ? 'index' : $routing['function'];
458 $this->set_method($routing['function']);
466 /* End of file Router.php */
467 /* Location: ./system/core/Router.php */