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, 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 // Turn the default route into an array. We explode it in the event that
102 // the controller is located in a subfolder
103 $segments = $this->_validate_request(explode('/', $this->default_controller));
105 // Set the class and method
106 $this->set_class($segments[0]);
107 $this->set_method('index');
109 // Assign the segments to the URI class
110 $this->uri->rsegments = $segments;
112 // re-index the routed segments array so it starts with 1 rather than 0
113 $this->uri->_reindex_segments();
115 log_message('debug', "No URI present. Default controller set.");
118 unset($this->routes['default_controller']);
120 // Do we need to remove the URL suffix?
121 $this->uri->_remove_url_suffix();
123 // Compile the segments into an array
124 $this->uri->_explode_segments();
126 // Parse any custom routing that may exist
127 $this->_parse_routes();
129 // Re-index the segment array so that it starts with 1 rather than 0
130 $this->uri->_reindex_segments();
133 // --------------------------------------------------------------------
138 * This function takes an array of URI segments as
139 * input, and sets the current class/method
146 function _set_request($segments = array())
148 $segments = $this->_validate_request($segments);
150 if (count($segments) == 0)
155 $this->set_class($segments[0]);
157 if (isset($segments[1]))
159 // A scaffolding request. No funny business with the URL
160 if ($this->routes['scaffolding_trigger'] == $segments[1] AND $segments[1] != '_ci_scaffolding')
162 $this->scaffolding_request = TRUE;
163 unset($this->routes['scaffolding_trigger']);
167 // A standard method request
168 $this->set_method($segments[1]);
173 // This lets the "routed" segment array identify that the default
174 // index method is being used.
175 $segments[1] = 'index';
178 // Update our "routed" segment array to contain the segments.
179 // Note: If there is no custom routing, this array will be
180 // identical to $this->uri->segments
181 $this->uri->rsegments = $segments;
184 // --------------------------------------------------------------------
187 * Validates the supplied segments. Attempts to determine the path to
194 function _validate_request($segments)
196 // Does the requested controller exist in the root folder?
197 if (file_exists(APPPATH.'controllers/'.$segments[0].EXT))
202 // Is the controller in a sub-folder?
203 if (is_dir(APPPATH.'controllers/'.$segments[0]))
205 // Set the directory and remove it from the segment array
206 $this->set_directory($segments[0]);
207 $segments = array_slice($segments, 1);
209 if (count($segments) > 0)
211 // Does the requested controller exist in the sub-folder?
212 if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].EXT))
214 show_404($this->fetch_directory().$segments[0]);
219 $this->set_class($this->default_controller);
220 $this->set_method('index');
222 // Does the default controller exist in the sub-folder?
223 if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT))
225 $this->directory = '';
234 // Can't find the requested controller...
235 show_404($segments[0]);
238 // --------------------------------------------------------------------
243 * This function matches any routes that may exist in
244 * the config/routes.php file against the URI to
245 * determine if the class/method need to be remapped.
250 function _parse_routes()
252 // Do we even have any custom routing to deal with?
253 // There is a default scaffolding trigger, so we'll look just for 1
254 if (count($this->routes) == 1)
256 $this->_set_request($this->uri->segments);
260 // Turn the segment array into a URI string
261 $uri = implode('/', $this->uri->segments);
263 // Is there a literal match? If so we're done
264 if (isset($this->routes[$uri]))
266 $this->_set_request(explode('/', $this->routes[$uri]));
270 // Loop through the route array looking for wild-cards
271 foreach ($this->routes as $key => $val)
273 // Convert wild-cards to RegEx
274 $key = str_replace(':any', '.+', str_replace(':num', '[0-9]+', $key));
276 // Does the RegEx match?
277 if (preg_match('#^'.$key.'$#', $uri))
279 // Do we have a back-reference?
280 if (strpos($val, '$') !== FALSE AND strpos($key, '(') !== FALSE)
282 $val = preg_replace('#^'.$key.'$#', $val, $uri);
285 $this->_set_request(explode('/', $val));
290 // If we got this far it means we didn't encounter a
291 // matching route so we'll set the site default route
292 $this->_set_request($this->uri->segments);
295 // --------------------------------------------------------------------
304 function set_class($class)
306 $this->class = $class;
309 // --------------------------------------------------------------------
312 * Fetch the current class
317 function fetch_class()
322 // --------------------------------------------------------------------
325 * Set the method name
331 function set_method($method)
333 $this->method = $method;
336 // --------------------------------------------------------------------
339 * Fetch the current method
344 function fetch_method()
346 if ($this->method == $this->fetch_class())
351 return $this->method;
354 // --------------------------------------------------------------------
357 * Set the directory name
363 function set_directory($dir)
365 $this->directory = $dir.'/';
368 // --------------------------------------------------------------------
371 * Fetch the sub-directory (if any) that contains the requested controller class
376 function fetch_directory()
378 return $this->directory;
384 /* End of file Router.php */
385 /* Location: ./system/libraries/Router.php */