Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
|
Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 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);
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 83 | $this->routes = ( ! isset($route) OR ! is_array($route)) ? array() : $route;
|
Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 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.
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 88 | $this->default_controller = ( ! isset($this->routes['default_controller']) OR $this->routes['default_controller'] == '') ? FALSE : strtolower($this->routes['default_controller']);
|
Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 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 Jones | 881a79e | 2008-02-13 03:28:18 +0000 | [diff] [blame] | 105 | // re-index the routed segments array so it starts with 1 rather than 0
|
| 106 | $this->uri->_reindex_segments();
|
| 107 |
|
Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 108 | log_message('debug', "No URI present. Default controller set.");
|
| 109 | return;
|
| 110 | }
|
| 111 | unset($this->routes['default_controller']);
|
| 112 |
|
Rick Ellis | 30b4015 | 2007-07-20 00:01:13 +0000 | [diff] [blame] | 113 | // Do we need to remove the URL suffix?
|
| 114 | $this->uri->_remove_url_suffix();
|
Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 115 |
|
Rick Ellis | 30b4015 | 2007-07-20 00:01:13 +0000 | [diff] [blame] | 116 | // Compile the segments into an array
|
| 117 | $this->uri->_explode_segments();
|
Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 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
|
Rick Ellis | 30b4015 | 2007-07-20 00:01:13 +0000 | [diff] [blame] | 123 | $this->uri->_reindex_segments();
|
Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 124 | }
|
| 125 |
|
| 126 | // --------------------------------------------------------------------
|
| 127 |
|
| 128 | /**
|
Rick Ellis | 30b4015 | 2007-07-20 00:01:13 +0000 | [diff] [blame] | 129 | * Set the Route
|
Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 130 | *
|
| 131 | * This function takes an array of URI segments as
|
Rick Ellis | 30b4015 | 2007-07-20 00:01:13 +0000 | [diff] [blame] | 132 | * input, and sets the current class/method
|
Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 133 | *
|
| 134 | * @access private
|
| 135 | * @param array
|
| 136 | * @param bool
|
| 137 | * @return void
|
| 138 | */
|
Rick Ellis | 30b4015 | 2007-07-20 00:01:13 +0000 | [diff] [blame] | 139 | function _set_request($segments = array())
|
Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 140 | {
|
Rick Ellis | 30b4015 | 2007-07-20 00:01:13 +0000 | [diff] [blame] | 141 | $segments = $this->_validate_request($segments);
|
Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 142 |
|
| 143 | if (count($segments) == 0)
|
| 144 | {
|
| 145 | return;
|
| 146 | }
|
| 147 |
|
| 148 | $this->set_class($segments[0]);
|
| 149 |
|
| 150 | if (isset($segments[1]))
|
| 151 | {
|
| 152 | // A scaffolding request. No funny business with the URL
|
| 153 | if ($this->routes['scaffolding_trigger'] == $segments[1] AND $segments[1] != '_ci_scaffolding')
|
| 154 | {
|
| 155 | $this->scaffolding_request = TRUE;
|
| 156 | unset($this->routes['scaffolding_trigger']);
|
| 157 | }
|
| 158 | else
|
| 159 | {
|
| 160 | // A standard method request
|
| 161 | $this->set_method($segments[1]);
|
| 162 | }
|
| 163 | }
|
Derek Jones | c38c703 | 2008-01-30 15:38:56 +0000 | [diff] [blame] | 164 | else
|
| 165 | {
|
| 166 | // This lets the "routed" segment array identify that the default
|
| 167 | // index method is being used.
|
| 168 | $segments[1] = 'index';
|
| 169 | }
|
Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 170 |
|
| 171 | // Update our "routed" segment array to contain the segments.
|
| 172 | // Note: If there is no custom routing, this array will be
|
Rick Ellis | 30b4015 | 2007-07-20 00:01:13 +0000 | [diff] [blame] | 173 | // identical to $this->uri->segments
|
| 174 | $this->uri->rsegments = $segments;
|
Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 175 | }
|
| 176 |
|
| 177 | // --------------------------------------------------------------------
|
| 178 |
|
| 179 | /**
|
| 180 | * Validates the supplied segments. Attempts to determine the path to
|
| 181 | * the controller.
|
| 182 | *
|
| 183 | * @access private
|
| 184 | * @param array
|
| 185 | * @return array
|
| 186 | */
|
Rick Ellis | 30b4015 | 2007-07-20 00:01:13 +0000 | [diff] [blame] | 187 | function _validate_request($segments)
|
Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 188 | {
|
| 189 | // Does the requested controller exist in the root folder?
|
| 190 | if (file_exists(APPPATH.'controllers/'.$segments[0].EXT))
|
| 191 | {
|
| 192 | return $segments;
|
| 193 | }
|
| 194 |
|
| 195 | // Is the controller in a sub-folder?
|
| 196 | if (is_dir(APPPATH.'controllers/'.$segments[0]))
|
| 197 | {
|
| 198 | // Set the directory and remove it from the segment array
|
| 199 | $this->set_directory($segments[0]);
|
| 200 | $segments = array_slice($segments, 1);
|
| 201 |
|
| 202 | if (count($segments) > 0)
|
| 203 | {
|
| 204 | // Does the requested controller exist in the sub-folder?
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 205 | if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].EXT))
|
Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 206 | {
|
Derek Allard | f9d5348 | 2008-04-26 19:19:25 +0000 | [diff] [blame] | 207 | show_404($this->fetch_directory().$segments[0]);
|
Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 208 | }
|
| 209 | }
|
| 210 | else
|
| 211 | {
|
| 212 | $this->set_class($this->default_controller);
|
| 213 | $this->set_method('index');
|
| 214 |
|
| 215 | // Does the default controller exist in the sub-folder?
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 216 | if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT))
|
Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 217 | {
|
| 218 | $this->directory = '';
|
| 219 | return array();
|
| 220 | }
|
| 221 |
|
| 222 | }
|
Derek Allard | f9d5348 | 2008-04-26 19:19:25 +0000 | [diff] [blame] | 223 |
|
Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 224 | return $segments;
|
| 225 | }
|
Derek Allard | f9d5348 | 2008-04-26 19:19:25 +0000 | [diff] [blame] | 226 |
|
Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 227 | // Can't find the requested controller...
|
Derek Allard | f9d5348 | 2008-04-26 19:19:25 +0000 | [diff] [blame] | 228 | show_404($segments[0]);
|
Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 229 | }
|
Derek Allard | f9d5348 | 2008-04-26 19:19:25 +0000 | [diff] [blame] | 230 |
|
Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 231 | // --------------------------------------------------------------------
|
Derek Allard | f9d5348 | 2008-04-26 19:19:25 +0000 | [diff] [blame] | 232 |
|
Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 233 | /**
|
| 234 | * Parse Routes
|
| 235 | *
|
| 236 | * This function matches any routes that may exist in
|
| 237 | * the config/routes.php file against the URI to
|
| 238 | * determine if the class/method need to be remapped.
|
| 239 | *
|
| 240 | * @access private
|
| 241 | * @return void
|
| 242 | */
|
| 243 | function _parse_routes()
|
| 244 | {
|
| 245 | // Do we even have any custom routing to deal with?
|
Derek Allard | 445b24d | 2007-04-24 12:48:19 +0000 | [diff] [blame] | 246 | // There is a default scaffolding trigger, so we'll look just for 1
|
| 247 | if (count($this->routes) == 1)
|
Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 248 | {
|
Rick Ellis | 30b4015 | 2007-07-20 00:01:13 +0000 | [diff] [blame] | 249 | $this->_set_request($this->uri->segments);
|
Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 250 | return;
|
| 251 | }
|
Derek Allard | 445b24d | 2007-04-24 12:48:19 +0000 | [diff] [blame] | 252 |
|
Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 253 | // Turn the segment array into a URI string
|
Rick Ellis | 30b4015 | 2007-07-20 00:01:13 +0000 | [diff] [blame] | 254 | $uri = implode('/', $this->uri->segments);
|
Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 255 |
|
| 256 | // Is there a literal match? If so we're done
|
| 257 | if (isset($this->routes[$uri]))
|
| 258 | {
|
Rick Ellis | 30b4015 | 2007-07-20 00:01:13 +0000 | [diff] [blame] | 259 | $this->_set_request(explode('/', $this->routes[$uri]));
|
Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 260 | return;
|
| 261 | }
|
| 262 |
|
| 263 | // Loop through the route array looking for wild-cards
|
Rick Ellis | 183fe84 | 2007-07-22 02:09:13 +0000 | [diff] [blame] | 264 | foreach ($this->routes as $key => $val)
|
Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 265 | {
|
| 266 | // Convert wild-cards to RegEx
|
| 267 | $key = str_replace(':any', '.+', str_replace(':num', '[0-9]+', $key));
|
| 268 |
|
| 269 | // Does the RegEx match?
|
| 270 | if (preg_match('#^'.$key.'$#', $uri))
|
| 271 | {
|
| 272 | // Do we have a back-reference?
|
| 273 | if (strpos($val, '$') !== FALSE AND strpos($key, '(') !== FALSE)
|
| 274 | {
|
| 275 | $val = preg_replace('#^'.$key.'$#', $val, $uri);
|
| 276 | }
|
| 277 |
|
Rick Ellis | 30b4015 | 2007-07-20 00:01:13 +0000 | [diff] [blame] | 278 | $this->_set_request(explode('/', $val));
|
Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 279 | return;
|
| 280 | }
|
| 281 | }
|
Derek Jones | c38c703 | 2008-01-30 15:38:56 +0000 | [diff] [blame] | 282 |
|
Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 283 | // If we got this far it means we didn't encounter a
|
| 284 | // matching route so we'll set the site default route
|
Rick Ellis | 30b4015 | 2007-07-20 00:01:13 +0000 | [diff] [blame] | 285 | $this->_set_request($this->uri->segments);
|
Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 286 | }
|
| 287 |
|
| 288 | // --------------------------------------------------------------------
|
| 289 |
|
| 290 | /**
|
| 291 | * Set the class name
|
| 292 | *
|
| 293 | * @access public
|
| 294 | * @param string
|
| 295 | * @return void
|
| 296 | */
|
| 297 | function set_class($class)
|
| 298 | {
|
paulburdick | 691010e | 2007-06-28 00:02:30 +0000 | [diff] [blame] | 299 | $this->class = $class;
|
Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 300 | }
|
| 301 |
|
| 302 | // --------------------------------------------------------------------
|
| 303 |
|
| 304 | /**
|
| 305 | * Fetch the current class
|
| 306 | *
|
| 307 | * @access public
|
| 308 | * @return string
|
| 309 | */
|
| 310 | function fetch_class()
|
| 311 | {
|
| 312 | return $this->class;
|
| 313 | }
|
| 314 |
|
| 315 | // --------------------------------------------------------------------
|
| 316 |
|
| 317 | /**
|
| 318 | * Set the method name
|
| 319 | *
|
| 320 | * @access public
|
| 321 | * @param string
|
| 322 | * @return void
|
| 323 | */
|
| 324 | function set_method($method)
|
| 325 | {
|
paulburdick | 691010e | 2007-06-28 00:02:30 +0000 | [diff] [blame] | 326 | $this->method = $method;
|
Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 327 | }
|
| 328 |
|
| 329 | // --------------------------------------------------------------------
|
| 330 |
|
| 331 | /**
|
| 332 | * Fetch the current method
|
| 333 | *
|
| 334 | * @access public
|
| 335 | * @return string
|
| 336 | */
|
| 337 | function fetch_method()
|
| 338 | {
|
| 339 | if ($this->method == $this->fetch_class())
|
| 340 | {
|
| 341 | return 'index';
|
| 342 | }
|
| 343 |
|
| 344 | return $this->method;
|
| 345 | }
|
| 346 |
|
| 347 | // --------------------------------------------------------------------
|
| 348 |
|
| 349 | /**
|
| 350 | * Set the directory name
|
| 351 | *
|
| 352 | * @access public
|
| 353 | * @param string
|
| 354 | * @return void
|
| 355 | */
|
| 356 | function set_directory($dir)
|
| 357 | {
|
paulburdick | 691010e | 2007-06-28 00:02:30 +0000 | [diff] [blame] | 358 | $this->directory = $dir.'/';
|
Derek Allard | a72b60d | 2007-01-31 23:56:11 +0000 | [diff] [blame] | 359 | }
|
| 360 |
|
| 361 | // --------------------------------------------------------------------
|
| 362 |
|
| 363 | /**
|
| 364 | * Fetch the sub-directory (if any) that contains the requested controller class
|
| 365 | *
|
| 366 | * @access public
|
| 367 | * @return string
|
| 368 | */
|
| 369 | function fetch_directory()
|
| 370 | {
|
| 371 | return $this->directory;
|
| 372 | }
|
| 373 |
|
| 374 | }
|
| 375 | // END Router Class
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 376 |
|
| 377 | /* End of file Router.php */
|
Derek Jones | a3ffbbb | 2008-05-11 18:18:29 +0000 | [diff] [blame] | 378 | /* Location: ./system/libraries/Router.php */ |