Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 1 | <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
|
| 2 | /**
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 3 | * CodeIgniter
|
Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 4 | *
|
| 5 | * An open source application development framework for PHP 4.3.2 or newer
|
| 6 | *
|
| 7 | * @package CodeIgniter
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 8 | * @author ExpressionEngine Dev Team
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 9 | * @copyright Copyright (c) 2006, EllisLab, Inc.
|
Derek Jones | 7a9193a | 2008-01-21 18:39:20 +0000 | [diff] [blame] | 10 | * @license http://codeigniter.com/user_guide/license.html
|
| 11 | * @link http://codeigniter.com
|
Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 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
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 25 | * @author ExpressionEngine Dev Team
|
Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 26 | * @category Libraries
|
Derek Jones | 7a9193a | 2008-01-21 18:39:20 +0000 | [diff] [blame] | 27 | * @link http://codeigniter.com/user_guide/general/routing.html
|
Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 28 | */
|
| 29 | class CI_Router {
|
| 30 |
|
Rick Ellis | 30b4015 | 2007-07-20 00:01:13 +0000 | [diff] [blame] | 31 | var $config;
|
Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 32 | 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 Ellis | 30b4015 | 2007-07-20 00:01:13 +0000 | [diff] [blame] | 49 | $this->uri =& load_class('URI');
|
| 50 | $this->_set_routing();
|
Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 51 | 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 Ellis | 30b4015 | 2007-07-20 00:01:13 +0000 | [diff] [blame] | 65 | function _set_routing()
|
Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 66 | {
|
| 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 Ellis | 30b4015 | 2007-07-20 00:01:13 +0000 | [diff] [blame] | 71 | $this->set_class(trim($this->uri->_filter_uri($_GET[$this->config->item('controller_trigger')])));
|
Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 72 |
|
| 73 | if (isset($_GET[$this->config->item('function_trigger')]))
|
| 74 | {
|
Rick Ellis | 30b4015 | 2007-07-20 00:01:13 +0000 | [diff] [blame] | 75 | $this->set_method(trim($this->uri->_filter_uri($_GET[$this->config->item('function_trigger')])));
|
Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 76 | }
|
| 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 Ellis | 30b4015 | 2007-07-20 00:01:13 +0000 | [diff] [blame] | 91 | $this->uri->_fetch_uri_string();
|
Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 92 |
|
| 93 | // Is there a URI string? If not, the default controller specified in the "routes" file will be shown.
|
Rick Ellis | 30b4015 | 2007-07-20 00:01:13 +0000 | [diff] [blame] | 94 | if ($this->uri->uri_string == '')
|
Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 95 | {
|
| 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 Jones | d45379e | 2008-01-30 03:47:03 +0000 | [diff] [blame] | 103 | $this->_set_request(array($this->default_controller, 'index'));
|
| 104 |
|
Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 105 | log_message('debug', "No URI present. Default controller set.");
|
| 106 | return;
|
| 107 | }
|
| 108 | unset($this->routes['default_controller']);
|
| 109 |
|
Rick Ellis | 30b4015 | 2007-07-20 00:01:13 +0000 | [diff] [blame] | 110 | // Do we need to remove the URL suffix?
|
| 111 | $this->uri->_remove_url_suffix();
|
Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 112 |
|
Rick Ellis | 30b4015 | 2007-07-20 00:01:13 +0000 | [diff] [blame] | 113 | // Compile the segments into an array
|
| 114 | $this->uri->_explode_segments();
|
Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 115 |
|
| 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 Ellis | 30b4015 | 2007-07-20 00:01:13 +0000 | [diff] [blame] | 120 | $this->uri->_reindex_segments();
|
Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 121 | }
|
| 122 |
|
| 123 | // --------------------------------------------------------------------
|
| 124 |
|
| 125 | /**
|
Rick Ellis | 30b4015 | 2007-07-20 00:01:13 +0000 | [diff] [blame] | 126 | * Set the Route
|
Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 127 | *
|
| 128 | * This function takes an array of URI segments as
|
Rick Ellis | 30b4015 | 2007-07-20 00:01:13 +0000 | [diff] [blame] | 129 | * input, and sets the current class/method
|
Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 130 | *
|
| 131 | * @access private
|
| 132 | * @param array
|
| 133 | * @param bool
|
| 134 | * @return void
|
| 135 | */
|
Rick Ellis | 30b4015 | 2007-07-20 00:01:13 +0000 | [diff] [blame] | 136 | function _set_request($segments = array())
|
Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 137 | {
|
Rick Ellis | 30b4015 | 2007-07-20 00:01:13 +0000 | [diff] [blame] | 138 | $segments = $this->_validate_request($segments);
|
Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 139 |
|
| 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 Jones | c38c703 | 2008-01-30 15:38:56 +0000 | [diff] [blame] | 161 | 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 Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 167 |
|
| 168 | // Update our "routed" segment array to contain the segments.
|
| 169 | // Note: If there is no custom routing, this array will be
|
Rick Ellis | 30b4015 | 2007-07-20 00:01:13 +0000 | [diff] [blame] | 170 | // identical to $this->uri->segments
|
| 171 | $this->uri->rsegments = $segments;
|
Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 172 | }
|
| 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 Ellis | 30b4015 | 2007-07-20 00:01:13 +0000 | [diff] [blame] | 184 | function _validate_request($segments)
|
Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 185 | {
|
| 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 Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 227 |
|
Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 228 | // --------------------------------------------------------------------
|
| 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 Allard | 445b24d | 2007-04-24 12:48:19 +0000 | [diff] [blame] | 243 | // There is a default scaffolding trigger, so we'll look just for 1
|
| 244 | if (count($this->routes) == 1)
|
Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 245 | {
|
Rick Ellis | 30b4015 | 2007-07-20 00:01:13 +0000 | [diff] [blame] | 246 | $this->_set_request($this->uri->segments);
|
Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 247 | return;
|
| 248 | }
|
Derek Allard | 445b24d | 2007-04-24 12:48:19 +0000 | [diff] [blame] | 249 |
|
Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 250 | // Turn the segment array into a URI string
|
Rick Ellis | 30b4015 | 2007-07-20 00:01:13 +0000 | [diff] [blame] | 251 | $uri = implode('/', $this->uri->segments);
|
| 252 | $num = count($this->uri->segments);
|
Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 253 |
|
| 254 | // Is there a literal match? If so we're done
|
| 255 | if (isset($this->routes[$uri]))
|
| 256 | {
|
Rick Ellis | 30b4015 | 2007-07-20 00:01:13 +0000 | [diff] [blame] | 257 | $this->_set_request(explode('/', $this->routes[$uri]));
|
Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 258 | return;
|
| 259 | }
|
| 260 |
|
| 261 | // Loop through the route array looking for wild-cards
|
Rick Ellis | 183fe84 | 2007-07-22 02:09:13 +0000 | [diff] [blame] | 262 | foreach ($this->routes as $key => $val)
|
Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 263 | {
|
| 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 Ellis | 30b4015 | 2007-07-20 00:01:13 +0000 | [diff] [blame] | 276 | $this->_set_request(explode('/', $val));
|
Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 277 | return;
|
| 278 | }
|
| 279 | }
|
Derek Jones | c38c703 | 2008-01-30 15:38:56 +0000 | [diff] [blame] | 280 |
|
Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 281 | // If we got this far it means we didn't encounter a
|
| 282 | // matching route so we'll set the site default route
|
Rick Ellis | 30b4015 | 2007-07-20 00:01:13 +0000 | [diff] [blame] | 283 | $this->_set_request($this->uri->segments);
|
Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 284 | }
|
| 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 | {
|
paulburdick | 691010e | 2007-06-28 00:02:30 +0000 | [diff] [blame] | 297 | $this->class = $class;
|
Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 298 | }
|
| 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 | {
|
paulburdick | 691010e | 2007-06-28 00:02:30 +0000 | [diff] [blame] | 324 | $this->method = $method;
|
Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 325 | }
|
| 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 | {
|
paulburdick | 691010e | 2007-06-28 00:02:30 +0000 | [diff] [blame] | 356 | $this->directory = $dir.'/';
|
Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 357 | }
|
| 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
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 374 | ?> |