1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
5 * An open source application development framework for PHP 4.3.2 or newer
8 * @author ExpressionEngine Dev Team
9 * @copyright Copyright (c) 2008 - 2009, 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 $uri_protocol = 'auto';
38 var $default_controller;
39 var $scaffolding_request = FALSE; // Must be set to FALSE
44 * Runs the route mapping function.
48 $this->config =& load_class('Config');
49 $this->uri =& load_class('URI');
50 $this->_set_routing();
51 log_message('debug', "Router Class Initialized");
54 // --------------------------------------------------------------------
57 * Set the route mapping
59 * This function determines what should be served based on the URI request,
60 * as well as any "routes" that have been set in the routing config file.
65 function _set_routing()
67 // Are query strings enabled in the config file?
68 // If so, we're done since segment based URIs are not used with query strings.
69 if ($this->config->item('enable_query_strings') === TRUE AND isset($_GET[$this->config->item('controller_trigger')]))
71 $this->set_class(trim($this->uri->_filter_uri($_GET[$this->config->item('controller_trigger')])));
73 if (isset($_GET[$this->config->item('function_trigger')]))
75 $this->set_method(trim($this->uri->_filter_uri($_GET[$this->config->item('function_trigger')])));
81 // Load the routes.php file.
82 @include(APPPATH.'config/routes'.EXT);
83 $this->routes = ( ! isset($route) OR ! is_array($route)) ? array() : $route;
86 // Set the default controller so we can display it in the event
87 // the URI doesn't correlated to a valid controller.
88 $this->default_controller = ( ! isset($this->routes['default_controller']) OR $this->routes['default_controller'] == '') ? FALSE : strtolower($this->routes['default_controller']);
90 // Fetch the complete URI string
91 $this->uri->_fetch_uri_string();
93 // Is there a URI string? If not, the default controller specified in the "routes" file will be shown.
94 if ($this->uri->uri_string == '')
96 if ($this->default_controller === FALSE)
98 show_error("Unable to determine what should be displayed. A default route has not been specified in the routing file.");
101 if (strpos($this->default_controller, '/') !== FALSE)
103 $x = explode('/', $this->default_controller);
105 $this->set_class(end($x));
106 $this->set_method('index');
107 $this->_set_request($x);
111 $this->set_class($this->default_controller);
112 $this->set_method('index');
113 $this->_set_request(array($this->default_controller, 'index'));
116 // re-index the routed segments array so it starts with 1 rather than 0
117 $this->uri->_reindex_segments();
119 log_message('debug', "No URI present. Default controller set.");
122 unset($this->routes['default_controller']);
124 // Do we need to remove the URL suffix?
125 $this->uri->_remove_url_suffix();
127 // Compile the segments into an array
128 $this->uri->_explode_segments();
130 // Parse any custom routing that may exist
131 $this->_parse_routes();
133 // Re-index the segment array so that it starts with 1 rather than 0
134 $this->uri->_reindex_segments();
137 // --------------------------------------------------------------------
142 * This function takes an array of URI segments as
143 * input, and sets the current class/method
150 function _set_request($segments = array())
152 $segments = $this->_validate_request($segments);
154 if (count($segments) == 0)
159 $this->set_class($segments[0]);
161 if (isset($segments[1]))
163 // A scaffolding request. No funny business with the URL
164 if ($this->routes['scaffolding_trigger'] == $segments[1] AND $segments[1] != '_ci_scaffolding')
166 $this->scaffolding_request = TRUE;
167 unset($this->routes['scaffolding_trigger']);
171 // A standard method request
172 $this->set_method($segments[1]);
177 // This lets the "routed" segment array identify that the default
178 // index method is being used.
179 $segments[1] = 'index';
182 // Update our "routed" segment array to contain the segments.
183 // Note: If there is no custom routing, this array will be
184 // identical to $this->uri->segments
185 $this->uri->rsegments = $segments;
188 // --------------------------------------------------------------------
191 * Validates the supplied segments. Attempts to determine the path to
198 function _validate_request($segments)
200 // Does the requested controller exist in the root folder?
201 if (file_exists(APPPATH.'controllers/'.$segments[0].EXT))
206 // Is the controller in a sub-folder?
207 if (is_dir(APPPATH.'controllers/'.$segments[0]))
209 // Set the directory and remove it from the segment array
210 $this->set_directory($segments[0]);
211 $segments = array_slice($segments, 1);
213 if (count($segments) > 0)
215 // Does the requested controller exist in the sub-folder?
216 if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].EXT))
218 show_404($this->fetch_directory().$segments[0]);
223 $this->set_class($this->default_controller);
224 $this->set_method('index');
226 // Does the default controller exist in the sub-folder?
227 if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT))
229 $this->directory = '';
238 // Can't find the requested controller...
239 show_404($segments[0]);
242 // --------------------------------------------------------------------
247 * This function matches any routes that may exist in
248 * the config/routes.php file against the URI to
249 * determine if the class/method need to be remapped.
254 function _parse_routes()
256 // Do we even have any custom routing to deal with?
257 // There is a default scaffolding trigger, so we'll look just for 1
258 if (count($this->routes) == 1)
260 $this->_set_request($this->uri->segments);
264 // Turn the segment array into a URI string
265 $uri = implode('/', $this->uri->segments);
267 // Is there a literal match? If so we're done
268 if (isset($this->routes[$uri]))
270 $this->_set_request(explode('/', $this->routes[$uri]));
274 // Loop through the route array looking for wild-cards
275 foreach ($this->routes as $key => $val)
277 // Convert wild-cards to RegEx
278 $key = str_replace(':any', '.+', str_replace(':num', '[0-9]+', $key));
280 // Does the RegEx match?
281 if (preg_match('#^'.$key.'$#', $uri))
283 // Do we have a back-reference?
284 if (strpos($val, '$') !== FALSE AND strpos($key, '(') !== FALSE)
286 $val = preg_replace('#^'.$key.'$#', $val, $uri);
289 $this->_set_request(explode('/', $val));
294 // If we got this far it means we didn't encounter a
295 // matching route so we'll set the site default route
296 $this->_set_request($this->uri->segments);
299 // --------------------------------------------------------------------
308 function set_class($class)
310 $this->class = $class;
313 // --------------------------------------------------------------------
316 * Fetch the current class
321 function fetch_class()
326 // --------------------------------------------------------------------
329 * Set the method name
335 function set_method($method)
337 $this->method = $method;
340 // --------------------------------------------------------------------
343 * Fetch the current method
348 function fetch_method()
350 if ($this->method == $this->fetch_class())
355 return $this->method;
358 // --------------------------------------------------------------------
361 * Set the directory name
367 function set_directory($dir)
369 $this->directory = $dir.'/';
372 // --------------------------------------------------------------------
375 * Fetch the sub-directory (if any) that contains the requested controller class
380 function fetch_directory()
382 return $this->directory;
388 /* End of file Router.php */
389 /* Location: ./system/libraries/Router.php */