blob: d911eb224fb76c739dcefd98170d260d41dbdb12 [file] [log] [blame]
Derek Allard2067d1a2008-11-13 22:59:24 +00001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
8 * @author ExpressionEngine Dev Team
Derek Jones7f3719f2010-01-05 13:35:37 +00009 * @copyright Copyright (c) 2008 - 2010, EllisLab, Inc.
Derek Allard2067d1a2008-11-13 22:59:24 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
12 * @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
25 * @author ExpressionEngine Dev Team
26 * @category Libraries
27 * @link http://codeigniter.com/user_guide/general/routing.html
28 */
29class CI_Router {
30
31 var $config;
32 var $routes = array();
33 var $error_routes = array();
34 var $class = '';
35 var $method = 'index';
36 var $directory = '';
Derek Allard2067d1a2008-11-13 22:59:24 +000037 var $default_controller;
Derek Allard2067d1a2008-11-13 22:59:24 +000038
39 /**
40 * Constructor
41 *
42 * Runs the route mapping function.
43 */
44 function CI_Router()
45 {
Derek Jonesc7738402010-03-02 13:55:13 -060046 $this->config =& load_class('Config', 'core');
47 $this->uri =& load_class('URI', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +000048 log_message('debug', "Router Class Initialized");
49 }
50
51 // --------------------------------------------------------------------
52
53 /**
54 * Set the route mapping
55 *
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.
58 *
59 * @access private
60 * @return void
61 */
62 function _set_routing()
Derek Jonesc7738402010-03-02 13:55:13 -060063 {
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
67 $segments = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000068 if ($this->config->item('enable_query_strings') === TRUE AND isset($_GET[$this->config->item('controller_trigger')]))
69 {
Derek Jonesc7738402010-03-02 13:55:13 -060070 if (isset($_GET[$this->config->item('directory_trigger')]))
71 {
72 $this->set_directory(trim($this->uri->_filter_uri($_GET[$this->config->item('directory_trigger')])));
73 $segments[] = $this->fetch_directory();
74 }
75
76 if (isset($_GET[$this->config->item('controller_trigger')]))
77 {
78 $this->set_class(trim($this->uri->_filter_uri($_GET[$this->config->item('controller_trigger')])));
79 $segments[] = $this->fetch_class();
80 }
81
Derek Allard2067d1a2008-11-13 22:59:24 +000082 if (isset($_GET[$this->config->item('function_trigger')]))
83 {
84 $this->set_method(trim($this->uri->_filter_uri($_GET[$this->config->item('function_trigger')])));
Derek Jonesc7738402010-03-02 13:55:13 -060085 $segments[] = $this->fetch_method();
Derek Allard2067d1a2008-11-13 22:59:24 +000086 }
Derek Allard2067d1a2008-11-13 22:59:24 +000087 }
88
89 // Load the routes.php file.
90 @include(APPPATH.'config/routes'.EXT);
91 $this->routes = ( ! isset($route) OR ! is_array($route)) ? array() : $route;
92 unset($route);
Derek Jonesc7738402010-03-02 13:55:13 -060093
Derek Allard2067d1a2008-11-13 22:59:24 +000094 // Set the default controller so we can display it in the event
95 // the URI doesn't correlated to a valid controller.
Derek Jonesc7738402010-03-02 13:55:13 -060096 $this->default_controller = ( ! isset($this->routes['default_controller']) OR $this->routes['default_controller'] == '') ? FALSE : strtolower($this->routes['default_controller']);
97
98 // Were there any query string segments? If so, we'll validate them and bail out since we're done.
99 if (count($segments) > 0)
100 {
101 return $this->_validate_request($segments);
102 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000103
104 // Fetch the complete URI string
105 $this->uri->_fetch_uri_string();
Derek Jonesc7738402010-03-02 13:55:13 -0600106
Derek Allard2067d1a2008-11-13 22:59:24 +0000107 // Is there a URI string? If not, the default controller specified in the "routes" file will be shown.
108 if ($this->uri->uri_string == '')
109 {
Derek Jonesc7738402010-03-02 13:55:13 -0600110 return $this->_set_default_controller();
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000112
113 // Do we need to remove the URL suffix?
114 $this->uri->_remove_url_suffix();
115
116 // Compile the segments into an array
117 $this->uri->_explode_segments();
118
119 // Parse any custom routing that may exist
120 $this->_parse_routes();
121
122 // Re-index the segment array so that it starts with 1 rather than 0
123 $this->uri->_reindex_segments();
124 }
Derek Jonesc7738402010-03-02 13:55:13 -0600125
126 // --------------------------------------------------------------------
127
128 /**
129 * Set the default controller
130 *
131 * @access private
132 * @return void
133 */
134 function _set_default_controller()
135 {
136 if ($this->default_controller === FALSE)
137 {
138 show_error("Unable to determine what should be displayed. A default route has not been specified in the routing file.");
139 }
140 // Is the method being specified?
141 if (strpos($this->default_controller, '/') !== FALSE)
142 {
143 $x = explode('/', $this->default_controller);
144
145 $this->set_class($x[0]);
146 $this->set_method($x[1]);
147 $this->_set_request(array($x[0], $x[1]));
148 }
149 else
150 {
151 $this->set_class($this->default_controller);
152 $this->set_method('index');
153 $this->_set_request(array($this->default_controller, 'index'));
154 }
155
156 // re-index the routed segments array so it starts with 1 rather than 0
157 $this->uri->_reindex_segments();
158
159 log_message('debug', "No URI present. Default controller set.");
160 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000161
162 // --------------------------------------------------------------------
163
164 /**
165 * Set the Route
166 *
167 * This function takes an array of URI segments as
168 * input, and sets the current class/method
169 *
170 * @access private
171 * @param array
172 * @param bool
173 * @return void
174 */
175 function _set_request($segments = array())
Derek Jonesc7738402010-03-02 13:55:13 -0600176 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 $segments = $this->_validate_request($segments);
178
179 if (count($segments) == 0)
180 {
Derek Jonesc7738402010-03-02 13:55:13 -0600181 return $this->_set_default_controller();
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 }
183
184 $this->set_class($segments[0]);
185
186 if (isset($segments[1]))
187 {
Derek Jonesc7738402010-03-02 13:55:13 -0600188 // A standard method request
189 $this->set_method($segments[1]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 }
191 else
192 {
193 // This lets the "routed" segment array identify that the default
194 // index method is being used.
195 $segments[1] = 'index';
196 }
197
198 // Update our "routed" segment array to contain the segments.
199 // Note: If there is no custom routing, this array will be
200 // identical to $this->uri->segments
201 $this->uri->rsegments = $segments;
202 }
203
204 // --------------------------------------------------------------------
205
206 /**
207 * Validates the supplied segments. Attempts to determine the path to
208 * the controller.
209 *
210 * @access private
211 * @param array
212 * @return array
213 */
214 function _validate_request($segments)
215 {
Derek Jonesc7738402010-03-02 13:55:13 -0600216 if (count($segments) == 0)
217 {
218 return $segments;
219 }
220
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 // Does the requested controller exist in the root folder?
222 if (file_exists(APPPATH.'controllers/'.$segments[0].EXT))
223 {
224 return $segments;
225 }
Derek Jonesc7738402010-03-02 13:55:13 -0600226
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 // Is the controller in a sub-folder?
228 if (is_dir(APPPATH.'controllers/'.$segments[0]))
Derek Jonesc7738402010-03-02 13:55:13 -0600229 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 // Set the directory and remove it from the segment array
231 $this->set_directory($segments[0]);
232 $segments = array_slice($segments, 1);
233
234 if (count($segments) > 0)
235 {
236 // Does the requested controller exist in the sub-folder?
237 if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].EXT))
238 {
Derek Jonesc7738402010-03-02 13:55:13 -0600239 show_404($this->fetch_directory().$segments[0]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 }
241 }
242 else
243 {
Derek Jonesc7738402010-03-02 13:55:13 -0600244 // Is the method being specified in the route?
245 if (strpos($this->default_controller, '/') !== FALSE)
246 {
247 $x = explode('/', $this->default_controller);
248
249 $this->set_class($x[0]);
250 $this->set_method($x[1]);
251 }
252 else
253 {
254 $this->set_class($this->default_controller);
255 $this->set_method('index');
256 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000257
258 // Does the default controller exist in the sub-folder?
259 if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT))
260 {
261 $this->directory = '';
262 return array();
263 }
264
265 }
Derek Jonesc7738402010-03-02 13:55:13 -0600266
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 return $segments;
268 }
Derek Jonesc7738402010-03-02 13:55:13 -0600269
270
271 // If we've gotten this far it means that the URI does not correlate to a valid
272 // controller class. We will now see if there is an override
273 if (isset($this->routes['404_override']) AND $this->routes['404_override'] != '')
274 {
275 if (strpos($this->routes['404_override'], '/') !== FALSE)
276 {
277 $x = explode('/', $this->routes['404_override']);
278
279 $this->set_class($x[0]);
280 $this->set_method($x[1]);
281
282 return $x;
283 }
284 }
285
286 // Nothing else to do at this point but show a 404
287 show_404($segments[0]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 }
Derek Jonesc7738402010-03-02 13:55:13 -0600289
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 // --------------------------------------------------------------------
Derek Jonesc7738402010-03-02 13:55:13 -0600291
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 /**
293 * Parse Routes
294 *
295 * This function matches any routes that may exist in
296 * the config/routes.php file against the URI to
297 * determine if the class/method need to be remapped.
298 *
299 * @access private
300 * @return void
301 */
302 function _parse_routes()
303 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 // Turn the segment array into a URI string
305 $uri = implode('/', $this->uri->segments);
Derek Jonesc7738402010-03-02 13:55:13 -0600306
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 // Is there a literal match? If so we're done
308 if (isset($this->routes[$uri]))
309 {
Derek Jonesc7738402010-03-02 13:55:13 -0600310 return $this->_set_request(explode('/', $this->routes[$uri]));
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 }
312
313 // Loop through the route array looking for wild-cards
314 foreach ($this->routes as $key => $val)
315 {
316 // Convert wild-cards to RegEx
317 $key = str_replace(':any', '.+', str_replace(':num', '[0-9]+', $key));
Derek Jonesc7738402010-03-02 13:55:13 -0600318
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 // Does the RegEx match?
320 if (preg_match('#^'.$key.'$#', $uri))
Derek Jonesc7738402010-03-02 13:55:13 -0600321 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 // Do we have a back-reference?
323 if (strpos($val, '$') !== FALSE AND strpos($key, '(') !== FALSE)
324 {
325 $val = preg_replace('#^'.$key.'$#', $val, $uri);
326 }
327
Derek Jonesc7738402010-03-02 13:55:13 -0600328 return $this->_set_request(explode('/', $val));
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 }
330 }
331
332 // If we got this far it means we didn't encounter a
333 // matching route so we'll set the site default route
334 $this->_set_request($this->uri->segments);
335 }
336
337 // --------------------------------------------------------------------
338
339 /**
340 * Set the class name
341 *
342 * @access public
343 * @param string
344 * @return void
345 */
346 function set_class($class)
347 {
Derek Jones2615e412010-10-06 17:51:16 -0500348 $this->class = str_replace(array('/', '.'), '', $class);
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 }
350
351 // --------------------------------------------------------------------
352
353 /**
354 * Fetch the current class
355 *
356 * @access public
357 * @return string
358 */
359 function fetch_class()
360 {
361 return $this->class;
362 }
363
364 // --------------------------------------------------------------------
365
366 /**
367 * Set the method name
368 *
369 * @access public
370 * @param string
371 * @return void
372 */
373 function set_method($method)
374 {
375 $this->method = $method;
376 }
377
378 // --------------------------------------------------------------------
379
380 /**
381 * Fetch the current method
382 *
383 * @access public
384 * @return string
385 */
386 function fetch_method()
387 {
388 if ($this->method == $this->fetch_class())
389 {
390 return 'index';
391 }
392
393 return $this->method;
394 }
395
396 // --------------------------------------------------------------------
397
398 /**
399 * Set the directory name
400 *
401 * @access public
402 * @param string
403 * @return void
404 */
405 function set_directory($dir)
406 {
Derek Jones2615e412010-10-06 17:51:16 -0500407 $this->directory = str_replace(array('/', '.'), '', $dir).'/';
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 }
409
410 // --------------------------------------------------------------------
411
412 /**
413 * Fetch the sub-directory (if any) that contains the requested controller class
414 *
415 * @access public
416 * @return string
417 */
418 function fetch_directory()
419 {
420 return $this->directory;
421 }
422
Derek Jonesc7738402010-03-02 13:55:13 -0600423 // --------------------------------------------------------------------
424
425 /**
426 * Set the controller overrides
427 *
428 * @access public
429 * @param array
430 * @return null
431 */
432 function _set_overrides($routing)
433 {
434 if ( ! is_array($routing))
435 {
436 return;
437 }
438
439 if (isset($routing['directory']))
440 {
441 $this->set_directory($routing['directory']);
442 }
443
444 if (isset($routing['controller']) AND $routing['controller'] != '')
445 {
446 $this->set_class($routing['controller']);
447 }
448
449 if (isset($routing['function']))
450 {
451 $routing['function'] = ($routing['function'] == '') ? 'index' : $routing['function'];
452 $this->set_method($routing['function']);
453 }
454 }
455
456
Derek Allard2067d1a2008-11-13 22:59:24 +0000457}
458// END Router Class
459
460/* End of file Router.php */
Derek Jonesc68dfbf2010-03-02 12:59:23 -0600461/* Location: ./system/core/Router.php */