blob: df72cea26b87e577705d406645b21949c84500f6 [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');
103
104 log_message('debug', "No URI present. Default controller set.");
105 return;
106 }
107 unset($this->routes['default_controller']);
108
Rick Ellis30b40152007-07-20 00:01:13 +0000109 // Do we need to remove the URL suffix?
110 $this->uri->_remove_url_suffix();
Derek Allarda72b60d2007-01-31 23:56:11 +0000111
Rick Ellis30b40152007-07-20 00:01:13 +0000112 // Compile the segments into an array
113 $this->uri->_explode_segments();
Derek Allarda72b60d2007-01-31 23:56:11 +0000114
115 // Parse any custom routing that may exist
116 $this->_parse_routes();
117
118 // Re-index the segment array so that it starts with 1 rather than 0
Rick Ellis30b40152007-07-20 00:01:13 +0000119 $this->uri->_reindex_segments();
Derek Allarda72b60d2007-01-31 23:56:11 +0000120 }
121
122 // --------------------------------------------------------------------
123
124 /**
Rick Ellis30b40152007-07-20 00:01:13 +0000125 * Set the Route
Derek Allarda72b60d2007-01-31 23:56:11 +0000126 *
127 * This function takes an array of URI segments as
Rick Ellis30b40152007-07-20 00:01:13 +0000128 * input, and sets the current class/method
Derek Allarda72b60d2007-01-31 23:56:11 +0000129 *
130 * @access private
131 * @param array
132 * @param bool
133 * @return void
134 */
Rick Ellis30b40152007-07-20 00:01:13 +0000135 function _set_request($segments = array())
Derek Allarda72b60d2007-01-31 23:56:11 +0000136 {
Rick Ellis30b40152007-07-20 00:01:13 +0000137 $segments = $this->_validate_request($segments);
Derek Allarda72b60d2007-01-31 23:56:11 +0000138
139 if (count($segments) == 0)
140 {
141 return;
142 }
143
144 $this->set_class($segments[0]);
145
146 if (isset($segments[1]))
147 {
148 // A scaffolding request. No funny business with the URL
149 if ($this->routes['scaffolding_trigger'] == $segments[1] AND $segments[1] != '_ci_scaffolding')
150 {
151 $this->scaffolding_request = TRUE;
152 unset($this->routes['scaffolding_trigger']);
153 }
154 else
155 {
156 // A standard method request
157 $this->set_method($segments[1]);
158 }
159 }
160
161 // Update our "routed" segment array to contain the segments.
162 // Note: If there is no custom routing, this array will be
Rick Ellis30b40152007-07-20 00:01:13 +0000163 // identical to $this->uri->segments
164 $this->uri->rsegments = $segments;
Derek Allarda72b60d2007-01-31 23:56:11 +0000165 }
166
167 // --------------------------------------------------------------------
168
169 /**
170 * Validates the supplied segments. Attempts to determine the path to
171 * the controller.
172 *
173 * @access private
174 * @param array
175 * @return array
176 */
Rick Ellis30b40152007-07-20 00:01:13 +0000177 function _validate_request($segments)
Derek Allarda72b60d2007-01-31 23:56:11 +0000178 {
179 // Does the requested controller exist in the root folder?
180 if (file_exists(APPPATH.'controllers/'.$segments[0].EXT))
181 {
182 return $segments;
183 }
184
185 // Is the controller in a sub-folder?
186 if (is_dir(APPPATH.'controllers/'.$segments[0]))
187 {
188 // Set the directory and remove it from the segment array
189 $this->set_directory($segments[0]);
190 $segments = array_slice($segments, 1);
191
192 if (count($segments) > 0)
193 {
194 // Does the requested controller exist in the sub-folder?
195 if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].EXT))
196 {
197 show_404();
198 }
199 }
200 else
201 {
202 $this->set_class($this->default_controller);
203 $this->set_method('index');
204
205 // Does the default controller exist in the sub-folder?
206 if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT))
207 {
208 $this->directory = '';
209 return array();
210 }
211
212 }
213
214 return $segments;
215 }
216
217 // Can't find the requested controller...
218 show_404();
219 }
Derek Allarda72b60d2007-01-31 23:56:11 +0000220
Derek Allarda72b60d2007-01-31 23:56:11 +0000221 // --------------------------------------------------------------------
222
223 /**
224 * Parse Routes
225 *
226 * This function matches any routes that may exist in
227 * the config/routes.php file against the URI to
228 * determine if the class/method need to be remapped.
229 *
230 * @access private
231 * @return void
232 */
233 function _parse_routes()
234 {
235 // Do we even have any custom routing to deal with?
Derek Allard445b24d2007-04-24 12:48:19 +0000236 // There is a default scaffolding trigger, so we'll look just for 1
237 if (count($this->routes) == 1)
Derek Allarda72b60d2007-01-31 23:56:11 +0000238 {
Rick Ellis30b40152007-07-20 00:01:13 +0000239 $this->_set_request($this->uri->segments);
Derek Allarda72b60d2007-01-31 23:56:11 +0000240 return;
241 }
Derek Allard445b24d2007-04-24 12:48:19 +0000242
Derek Allarda72b60d2007-01-31 23:56:11 +0000243 // Turn the segment array into a URI string
Rick Ellis30b40152007-07-20 00:01:13 +0000244 $uri = implode('/', $this->uri->segments);
245 $num = count($this->uri->segments);
Derek Allarda72b60d2007-01-31 23:56:11 +0000246
247 // Is there a literal match? If so we're done
248 if (isset($this->routes[$uri]))
249 {
Rick Ellis30b40152007-07-20 00:01:13 +0000250 $this->_set_request(explode('/', $this->routes[$uri]));
Derek Allarda72b60d2007-01-31 23:56:11 +0000251 return;
252 }
253
254 // Loop through the route array looking for wild-cards
Rick Ellis183fe842007-07-22 02:09:13 +0000255 foreach ($this->routes as $key => $val)
Derek Allarda72b60d2007-01-31 23:56:11 +0000256 {
257 // Convert wild-cards to RegEx
258 $key = str_replace(':any', '.+', str_replace(':num', '[0-9]+', $key));
259
260 // Does the RegEx match?
261 if (preg_match('#^'.$key.'$#', $uri))
262 {
263 // Do we have a back-reference?
264 if (strpos($val, '$') !== FALSE AND strpos($key, '(') !== FALSE)
265 {
266 $val = preg_replace('#^'.$key.'$#', $val, $uri);
267 }
268
Rick Ellis30b40152007-07-20 00:01:13 +0000269 $this->_set_request(explode('/', $val));
Derek Allarda72b60d2007-01-31 23:56:11 +0000270 return;
271 }
272 }
273
274 // If we got this far it means we didn't encounter a
275 // matching route so we'll set the site default route
Rick Ellis30b40152007-07-20 00:01:13 +0000276 $this->_set_request($this->uri->segments);
Derek Allarda72b60d2007-01-31 23:56:11 +0000277 }
278
279 // --------------------------------------------------------------------
280
281 /**
282 * Set the class name
283 *
284 * @access public
285 * @param string
286 * @return void
287 */
288 function set_class($class)
289 {
paulburdick691010e2007-06-28 00:02:30 +0000290 $this->class = $class;
Derek Allarda72b60d2007-01-31 23:56:11 +0000291 }
292
293 // --------------------------------------------------------------------
294
295 /**
296 * Fetch the current class
297 *
298 * @access public
299 * @return string
300 */
301 function fetch_class()
302 {
303 return $this->class;
304 }
305
306 // --------------------------------------------------------------------
307
308 /**
309 * Set the method name
310 *
311 * @access public
312 * @param string
313 * @return void
314 */
315 function set_method($method)
316 {
paulburdick691010e2007-06-28 00:02:30 +0000317 $this->method = $method;
Derek Allarda72b60d2007-01-31 23:56:11 +0000318 }
319
320 // --------------------------------------------------------------------
321
322 /**
323 * Fetch the current method
324 *
325 * @access public
326 * @return string
327 */
328 function fetch_method()
329 {
330 if ($this->method == $this->fetch_class())
331 {
332 return 'index';
333 }
334
335 return $this->method;
336 }
337
338 // --------------------------------------------------------------------
339
340 /**
341 * Set the directory name
342 *
343 * @access public
344 * @param string
345 * @return void
346 */
347 function set_directory($dir)
348 {
paulburdick691010e2007-06-28 00:02:30 +0000349 $this->directory = $dir.'/';
Derek Allarda72b60d2007-01-31 23:56:11 +0000350 }
351
352 // --------------------------------------------------------------------
353
354 /**
355 * Fetch the sub-directory (if any) that contains the requested controller class
356 *
357 * @access public
358 * @return string
359 */
360 function fetch_directory()
361 {
362 return $this->directory;
363 }
364
365}
366// END Router Class
adminb0dd10f2006-08-25 17:25:49 +0000367?>