blob: d24192c1847fc61473ed56d8f1e4d2414edd0b65 [file] [log] [blame]
Derek Allarda72b60d2007-01-31 23:56:11 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
Derek Allardd2df9bc2007-04-15 17:41:17 +00003 * CodeIgniter
Derek Allarda72b60d2007-01-31 23:56:11 +00004 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
Derek Allard3d879d52008-01-18 19:41:32 +00008 * @author ExpressionEngine Dev Team
Derek Allardd2df9bc2007-04-15 17:41:17 +00009 * @copyright Copyright (c) 2006, EllisLab, Inc.
Derek Jones7a9193a2008-01-21 18:39:20 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
Derek Allarda72b60d2007-01-31 23:56:11 +000012 * @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
Derek Allard3d879d52008-01-18 19:41:32 +000025 * @author ExpressionEngine Dev Team
Derek Allarda72b60d2007-01-31 23:56:11 +000026 * @category Libraries
Derek Jones7a9193a2008-01-21 18:39:20 +000027 * @link http://codeigniter.com/user_guide/general/routing.html
Derek Allarda72b60d2007-01-31 23:56:11 +000028 */
29class CI_Router {
30
Rick Ellis30b40152007-07-20 00:01:13 +000031 var $config;
Derek Allarda72b60d2007-01-31 23:56:11 +000032 var $routes = array();
33 var $error_routes = array();
34 var $class = '';
35 var $method = 'index';
36 var $directory = '';
37 var $uri_protocol = 'auto';
38 var $default_controller;
39 var $scaffolding_request = FALSE; // Must be set to FALSE
40
41 /**
42 * Constructor
43 *
44 * Runs the route mapping function.
45 */
46 function CI_Router()
47 {
48 $this->config =& load_class('Config');
Rick Ellis30b40152007-07-20 00:01:13 +000049 $this->uri =& load_class('URI');
50 $this->_set_routing();
Derek Allarda72b60d2007-01-31 23:56:11 +000051 log_message('debug', "Router Class Initialized");
52 }
53
54 // --------------------------------------------------------------------
55
56 /**
57 * Set the route mapping
58 *
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.
61 *
62 * @access private
63 * @return void
64 */
Rick Ellis30b40152007-07-20 00:01:13 +000065 function _set_routing()
Derek Allarda72b60d2007-01-31 23:56:11 +000066 {
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')]))
70 {
Rick Ellis30b40152007-07-20 00:01:13 +000071 $this->set_class(trim($this->uri->_filter_uri($_GET[$this->config->item('controller_trigger')])));
Derek Allarda72b60d2007-01-31 23:56:11 +000072
73 if (isset($_GET[$this->config->item('function_trigger')]))
74 {
Rick Ellis30b40152007-07-20 00:01:13 +000075 $this->set_method(trim($this->uri->_filter_uri($_GET[$this->config->item('function_trigger')])));
Derek Allarda72b60d2007-01-31 23:56:11 +000076 }
77
78 return;
79 }
80
81 // Load the routes.php file.
82 @include(APPPATH.'config/routes'.EXT);
83 $this->routes = ( ! isset($route) OR ! is_array($route)) ? array() : $route;
84 unset($route);
85
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']);
89
90 // Fetch the complete URI string
Rick Ellis30b40152007-07-20 00:01:13 +000091 $this->uri->_fetch_uri_string();
Derek Allarda72b60d2007-01-31 23:56:11 +000092
93 // Is there a URI string? If not, the default controller specified in the "routes" file will be shown.
Rick Ellis30b40152007-07-20 00:01:13 +000094 if ($this->uri->uri_string == '')
Derek Allarda72b60d2007-01-31 23:56:11 +000095 {
96 if ($this->default_controller === FALSE)
97 {
98 show_error("Unable to determine what should be displayed. A default route has not been specified in the routing file.");
99 }
100
101 $this->set_class($this->default_controller);
102 $this->set_method('index');
Derek Jonesd45379e2008-01-30 03:47:03 +0000103 $this->_set_request(array($this->default_controller, 'index'));
104
Derek Allarda72b60d2007-01-31 23:56:11 +0000105 log_message('debug', "No URI present. Default controller set.");
106 return;
107 }
108 unset($this->routes['default_controller']);
109
Rick Ellis30b40152007-07-20 00:01:13 +0000110 // Do we need to remove the URL suffix?
111 $this->uri->_remove_url_suffix();
Derek Allarda72b60d2007-01-31 23:56:11 +0000112
Rick Ellis30b40152007-07-20 00:01:13 +0000113 // Compile the segments into an array
114 $this->uri->_explode_segments();
Derek Allarda72b60d2007-01-31 23:56:11 +0000115
116 // Parse any custom routing that may exist
117 $this->_parse_routes();
118
119 // Re-index the segment array so that it starts with 1 rather than 0
Rick Ellis30b40152007-07-20 00:01:13 +0000120 $this->uri->_reindex_segments();
Derek Allarda72b60d2007-01-31 23:56:11 +0000121 }
122
123 // --------------------------------------------------------------------
124
125 /**
Rick Ellis30b40152007-07-20 00:01:13 +0000126 * Set the Route
Derek Allarda72b60d2007-01-31 23:56:11 +0000127 *
128 * This function takes an array of URI segments as
Rick Ellis30b40152007-07-20 00:01:13 +0000129 * input, and sets the current class/method
Derek Allarda72b60d2007-01-31 23:56:11 +0000130 *
131 * @access private
132 * @param array
133 * @param bool
134 * @return void
135 */
Rick Ellis30b40152007-07-20 00:01:13 +0000136 function _set_request($segments = array())
Derek Allarda72b60d2007-01-31 23:56:11 +0000137 {
Rick Ellis30b40152007-07-20 00:01:13 +0000138 $segments = $this->_validate_request($segments);
Derek Allarda72b60d2007-01-31 23:56:11 +0000139
140 if (count($segments) == 0)
141 {
142 return;
143 }
144
145 $this->set_class($segments[0]);
146
147 if (isset($segments[1]))
148 {
149 // A scaffolding request. No funny business with the URL
150 if ($this->routes['scaffolding_trigger'] == $segments[1] AND $segments[1] != '_ci_scaffolding')
151 {
152 $this->scaffolding_request = TRUE;
153 unset($this->routes['scaffolding_trigger']);
154 }
155 else
156 {
157 // A standard method request
158 $this->set_method($segments[1]);
159 }
160 }
Derek Jonesc38c7032008-01-30 15:38:56 +0000161 else
162 {
163 // This lets the "routed" segment array identify that the default
164 // index method is being used.
165 $segments[1] = 'index';
166 }
Derek Allarda72b60d2007-01-31 23:56:11 +0000167
168 // Update our "routed" segment array to contain the segments.
169 // Note: If there is no custom routing, this array will be
Rick Ellis30b40152007-07-20 00:01:13 +0000170 // identical to $this->uri->segments
171 $this->uri->rsegments = $segments;
Derek Allarda72b60d2007-01-31 23:56:11 +0000172 }
173
174 // --------------------------------------------------------------------
175
176 /**
177 * Validates the supplied segments. Attempts to determine the path to
178 * the controller.
179 *
180 * @access private
181 * @param array
182 * @return array
183 */
Rick Ellis30b40152007-07-20 00:01:13 +0000184 function _validate_request($segments)
Derek Allarda72b60d2007-01-31 23:56:11 +0000185 {
186 // Does the requested controller exist in the root folder?
187 if (file_exists(APPPATH.'controllers/'.$segments[0].EXT))
188 {
189 return $segments;
190 }
191
192 // Is the controller in a sub-folder?
193 if (is_dir(APPPATH.'controllers/'.$segments[0]))
194 {
195 // Set the directory and remove it from the segment array
196 $this->set_directory($segments[0]);
197 $segments = array_slice($segments, 1);
198
199 if (count($segments) > 0)
200 {
201 // Does the requested controller exist in the sub-folder?
202 if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].EXT))
203 {
204 show_404();
205 }
206 }
207 else
208 {
209 $this->set_class($this->default_controller);
210 $this->set_method('index');
211
212 // Does the default controller exist in the sub-folder?
213 if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT))
214 {
215 $this->directory = '';
216 return array();
217 }
218
219 }
220
221 return $segments;
222 }
223
224 // Can't find the requested controller...
225 show_404();
226 }
Derek Allarda72b60d2007-01-31 23:56:11 +0000227
Derek Allarda72b60d2007-01-31 23:56:11 +0000228 // --------------------------------------------------------------------
229
230 /**
231 * Parse Routes
232 *
233 * This function matches any routes that may exist in
234 * the config/routes.php file against the URI to
235 * determine if the class/method need to be remapped.
236 *
237 * @access private
238 * @return void
239 */
240 function _parse_routes()
241 {
242 // Do we even have any custom routing to deal with?
Derek Allard445b24d2007-04-24 12:48:19 +0000243 // There is a default scaffolding trigger, so we'll look just for 1
244 if (count($this->routes) == 1)
Derek Allarda72b60d2007-01-31 23:56:11 +0000245 {
Rick Ellis30b40152007-07-20 00:01:13 +0000246 $this->_set_request($this->uri->segments);
Derek Allarda72b60d2007-01-31 23:56:11 +0000247 return;
248 }
Derek Allard445b24d2007-04-24 12:48:19 +0000249
Derek Allarda72b60d2007-01-31 23:56:11 +0000250 // Turn the segment array into a URI string
Rick Ellis30b40152007-07-20 00:01:13 +0000251 $uri = implode('/', $this->uri->segments);
252 $num = count($this->uri->segments);
Derek Allarda72b60d2007-01-31 23:56:11 +0000253
254 // Is there a literal match? If so we're done
255 if (isset($this->routes[$uri]))
256 {
Rick Ellis30b40152007-07-20 00:01:13 +0000257 $this->_set_request(explode('/', $this->routes[$uri]));
Derek Allarda72b60d2007-01-31 23:56:11 +0000258 return;
259 }
260
261 // Loop through the route array looking for wild-cards
Rick Ellis183fe842007-07-22 02:09:13 +0000262 foreach ($this->routes as $key => $val)
Derek Allarda72b60d2007-01-31 23:56:11 +0000263 {
264 // Convert wild-cards to RegEx
265 $key = str_replace(':any', '.+', str_replace(':num', '[0-9]+', $key));
266
267 // Does the RegEx match?
268 if (preg_match('#^'.$key.'$#', $uri))
269 {
270 // Do we have a back-reference?
271 if (strpos($val, '$') !== FALSE AND strpos($key, '(') !== FALSE)
272 {
273 $val = preg_replace('#^'.$key.'$#', $val, $uri);
274 }
275
Rick Ellis30b40152007-07-20 00:01:13 +0000276 $this->_set_request(explode('/', $val));
Derek Allarda72b60d2007-01-31 23:56:11 +0000277 return;
278 }
279 }
Derek Jonesc38c7032008-01-30 15:38:56 +0000280
Derek Allarda72b60d2007-01-31 23:56:11 +0000281 // If we got this far it means we didn't encounter a
282 // matching route so we'll set the site default route
Rick Ellis30b40152007-07-20 00:01:13 +0000283 $this->_set_request($this->uri->segments);
Derek Allarda72b60d2007-01-31 23:56:11 +0000284 }
285
286 // --------------------------------------------------------------------
287
288 /**
289 * Set the class name
290 *
291 * @access public
292 * @param string
293 * @return void
294 */
295 function set_class($class)
296 {
paulburdick691010e2007-06-28 00:02:30 +0000297 $this->class = $class;
Derek Allarda72b60d2007-01-31 23:56:11 +0000298 }
299
300 // --------------------------------------------------------------------
301
302 /**
303 * Fetch the current class
304 *
305 * @access public
306 * @return string
307 */
308 function fetch_class()
309 {
310 return $this->class;
311 }
312
313 // --------------------------------------------------------------------
314
315 /**
316 * Set the method name
317 *
318 * @access public
319 * @param string
320 * @return void
321 */
322 function set_method($method)
323 {
paulburdick691010e2007-06-28 00:02:30 +0000324 $this->method = $method;
Derek Allarda72b60d2007-01-31 23:56:11 +0000325 }
326
327 // --------------------------------------------------------------------
328
329 /**
330 * Fetch the current method
331 *
332 * @access public
333 * @return string
334 */
335 function fetch_method()
336 {
337 if ($this->method == $this->fetch_class())
338 {
339 return 'index';
340 }
341
342 return $this->method;
343 }
344
345 // --------------------------------------------------------------------
346
347 /**
348 * Set the directory name
349 *
350 * @access public
351 * @param string
352 * @return void
353 */
354 function set_directory($dir)
355 {
paulburdick691010e2007-06-28 00:02:30 +0000356 $this->directory = $dir.'/';
Derek Allarda72b60d2007-01-31 23:56:11 +0000357 }
358
359 // --------------------------------------------------------------------
360
361 /**
362 * Fetch the sub-directory (if any) that contains the requested controller class
363 *
364 * @access public
365 * @return string
366 */
367 function fetch_directory()
368 {
369 return $this->directory;
370 }
371
372}
373// END Router Class
adminb0dd10f2006-08-25 17:25:49 +0000374?>