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