Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Greg Aker | 741de1c | 2010-11-10 14:52:57 -0600 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.1.6 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
| 7 | * @package CodeIgniter |
| 8 | * @author ExpressionEngine Dev Team |
Greg Aker | 0711dc8 | 2011-01-05 10:49:40 -0600 | [diff] [blame] | 9 | * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 10 | * @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 | */ |
| 29 | class CI_Router { |
| 30 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 31 | var $config; |
| 32 | var $routes = array(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 33 | var $error_routes = array(); |
| 34 | var $class = ''; |
| 35 | var $method = 'index'; |
| 36 | var $directory = ''; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 37 | var $default_controller; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 38 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 39 | /** |
| 40 | * Constructor |
| 41 | * |
| 42 | * Runs the route mapping function. |
| 43 | */ |
Greg Aker | a926328 | 2010-11-10 15:26:43 -0600 | [diff] [blame] | 44 | function __construct() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 45 | { |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 46 | $this->config =& load_class('Config', 'core'); |
| 47 | $this->uri =& load_class('URI', 'core'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 48 | log_message('debug', "Router Class Initialized"); |
| 49 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 50 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 51 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 52 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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() |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 63 | { |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 64 | // Are query strings enabled in the config file? Normally CI doesn't utilize query strings |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 65 | // since URI segments are more search-engine friendly, but they can optionally be used. |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 66 | // If this feature is enabled, we will gather the directory/class/method a little differently |
| 67 | $segments = array(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 68 | if ($this->config->item('enable_query_strings') === TRUE AND isset($_GET[$this->config->item('controller_trigger')])) |
| 69 | { |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 70 | 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 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 75 | |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 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 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 81 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 82 | 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 Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 85 | $segments[] = $this->fetch_method(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 86 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 87 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 88 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 89 | // Load the routes.php file. |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [diff] [blame] | 90 | if (defined('ENVIRONMENT') AND is_file(APPPATH.'config/'.ENVIRONMENT.'/routes.php')) |
bubbafoley | 0ea0414 | 2011-03-17 14:55:41 -0500 | [diff] [blame] | 91 | { |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [diff] [blame] | 92 | include(APPPATH.'config/'.ENVIRONMENT.'/routes.php'); |
bubbafoley | 0ea0414 | 2011-03-17 14:55:41 -0500 | [diff] [blame] | 93 | } |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [diff] [blame] | 94 | elseif (is_file(APPPATH.'config/routes.php')) |
bubbafoley | 0ea0414 | 2011-03-17 14:55:41 -0500 | [diff] [blame] | 95 | { |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [diff] [blame] | 96 | include(APPPATH.'config/routes.php'); |
bubbafoley | 0ea0414 | 2011-03-17 14:55:41 -0500 | [diff] [blame] | 97 | } |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 98 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 99 | $this->routes = ( ! isset($route) OR ! is_array($route)) ? array() : $route; |
| 100 | unset($route); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 101 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 102 | // Set the default controller so we can display it in the event |
| 103 | // the URI doesn't correlated to a valid controller. |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 104 | $this->default_controller = ( ! isset($this->routes['default_controller']) OR $this->routes['default_controller'] == '') ? FALSE : strtolower($this->routes['default_controller']); |
| 105 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 106 | // Were there any query string segments? If so, we'll validate them and bail out since we're done. |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 107 | if (count($segments) > 0) |
| 108 | { |
| 109 | return $this->_validate_request($segments); |
| 110 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 111 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 112 | // Fetch the complete URI string |
| 113 | $this->uri->_fetch_uri_string(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 114 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 115 | // Is there a URI string? If not, the default controller specified in the "routes" file will be shown. |
| 116 | if ($this->uri->uri_string == '') |
| 117 | { |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 118 | return $this->_set_default_controller(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 119 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 120 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 121 | // Do we need to remove the URL suffix? |
| 122 | $this->uri->_remove_url_suffix(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 123 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 124 | // Compile the segments into an array |
| 125 | $this->uri->_explode_segments(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 126 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 127 | // Parse any custom routing that may exist |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 128 | $this->_parse_routes(); |
| 129 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 130 | // Re-index the segment array so that it starts with 1 rather than 0 |
| 131 | $this->uri->_reindex_segments(); |
| 132 | } |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 133 | |
| 134 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 135 | |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 136 | /** |
| 137 | * Set the default controller |
| 138 | * |
| 139 | * @access private |
| 140 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 141 | */ |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 142 | function _set_default_controller() |
| 143 | { |
| 144 | if ($this->default_controller === FALSE) |
| 145 | { |
| 146 | show_error("Unable to determine what should be displayed. A default route has not been specified in the routing file."); |
| 147 | } |
| 148 | // Is the method being specified? |
| 149 | if (strpos($this->default_controller, '/') !== FALSE) |
| 150 | { |
| 151 | $x = explode('/', $this->default_controller); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 152 | |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 153 | $this->set_class($x[0]); |
| 154 | $this->set_method($x[1]); |
Pascal Kriete | 790ebf3 | 2010-12-15 10:53:35 -0500 | [diff] [blame] | 155 | $this->_set_request($x); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 156 | } |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 157 | else |
| 158 | { |
| 159 | $this->set_class($this->default_controller); |
| 160 | $this->set_method('index'); |
| 161 | $this->_set_request(array($this->default_controller, 'index')); |
| 162 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 163 | |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 164 | // re-index the routed segments array so it starts with 1 rather than 0 |
| 165 | $this->uri->_reindex_segments(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 166 | |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 167 | log_message('debug', "No URI present. Default controller set."); |
| 168 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 169 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 170 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 171 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 172 | /** |
| 173 | * Set the Route |
| 174 | * |
| 175 | * This function takes an array of URI segments as |
| 176 | * input, and sets the current class/method |
| 177 | * |
| 178 | * @access private |
| 179 | * @param array |
| 180 | * @param bool |
| 181 | * @return void |
| 182 | */ |
| 183 | function _set_request($segments = array()) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 184 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 185 | $segments = $this->_validate_request($segments); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 186 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 187 | if (count($segments) == 0) |
| 188 | { |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 189 | return $this->_set_default_controller(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 190 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 191 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 192 | $this->set_class($segments[0]); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 193 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 194 | if (isset($segments[1])) |
| 195 | { |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 196 | // A standard method request |
| 197 | $this->set_method($segments[1]); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 198 | } |
| 199 | else |
| 200 | { |
| 201 | // This lets the "routed" segment array identify that the default |
| 202 | // index method is being used. |
| 203 | $segments[1] = 'index'; |
| 204 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 205 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 206 | // Update our "routed" segment array to contain the segments. |
| 207 | // Note: If there is no custom routing, this array will be |
| 208 | // identical to $this->uri->segments |
| 209 | $this->uri->rsegments = $segments; |
| 210 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 211 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 212 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 213 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 214 | /** |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 215 | * Validates the supplied segments. Attempts to determine the path to |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 216 | * the controller. |
| 217 | * |
| 218 | * @access private |
| 219 | * @param array |
| 220 | * @return array |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 221 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 222 | function _validate_request($segments) |
| 223 | { |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 224 | if (count($segments) == 0) |
| 225 | { |
| 226 | return $segments; |
| 227 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 228 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 229 | // Does the requested controller exist in the root folder? |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [diff] [blame] | 230 | if (file_exists(APPPATH.'controllers/'.$segments[0].'.php')) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 231 | { |
| 232 | return $segments; |
| 233 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 234 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 235 | // Is the controller in a sub-folder? |
| 236 | if (is_dir(APPPATH.'controllers/'.$segments[0])) |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 237 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 238 | // Set the directory and remove it from the segment array |
| 239 | $this->set_directory($segments[0]); |
| 240 | $segments = array_slice($segments, 1); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 241 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 242 | if (count($segments) > 0) |
| 243 | { |
| 244 | // Does the requested controller exist in the sub-folder? |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [diff] [blame] | 245 | if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].'.php')) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 246 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 247 | show_404($this->fetch_directory().$segments[0]); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | else |
| 251 | { |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 252 | // Is the method being specified in the route? |
| 253 | if (strpos($this->default_controller, '/') !== FALSE) |
| 254 | { |
| 255 | $x = explode('/', $this->default_controller); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 256 | |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 257 | $this->set_class($x[0]); |
| 258 | $this->set_method($x[1]); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 259 | } |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 260 | else |
| 261 | { |
| 262 | $this->set_class($this->default_controller); |
| 263 | $this->set_method('index'); |
| 264 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 265 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 266 | // Does the default controller exist in the sub-folder? |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [diff] [blame] | 267 | if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.'.php')) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 268 | { |
| 269 | $this->directory = ''; |
| 270 | return array(); |
| 271 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 272 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 273 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 274 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 275 | return $segments; |
| 276 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 277 | |
| 278 | |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 279 | // If we've gotten this far it means that the URI does not correlate to a valid |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 280 | // controller class. We will now see if there is an override |
Eric Barnes | c5bf616 | 2011-01-30 21:17:11 -0500 | [diff] [blame] | 281 | if ( ! empty($this->routes['404_override'])) |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 282 | { |
Phil Sturgeon | 23174a6 | 2010-12-15 15:18:16 +0000 | [diff] [blame] | 283 | $x = explode('/', $this->routes['404_override']); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 284 | |
Phil Sturgeon | 23174a6 | 2010-12-15 15:18:16 +0000 | [diff] [blame] | 285 | $this->set_class($x[0]); |
| 286 | $this->set_method(isset($x[1]) ? $x[1] : 'index'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 287 | |
Phil Sturgeon | 23174a6 | 2010-12-15 15:18:16 +0000 | [diff] [blame] | 288 | return $x; |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 289 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 290 | |
Phil Sturgeon | 23174a6 | 2010-12-15 15:18:16 +0000 | [diff] [blame] | 291 | |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 292 | // Nothing else to do at this point but show a 404 |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 293 | show_404($segments[0]); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 294 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 295 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 296 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 297 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 298 | /** |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 299 | * Parse Routes |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 300 | * |
| 301 | * This function matches any routes that may exist in |
| 302 | * the config/routes.php file against the URI to |
| 303 | * determine if the class/method need to be remapped. |
| 304 | * |
| 305 | * @access private |
| 306 | * @return void |
| 307 | */ |
| 308 | function _parse_routes() |
| 309 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 310 | // Turn the segment array into a URI string |
| 311 | $uri = implode('/', $this->uri->segments); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 312 | |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 313 | // Is there a literal match? If so we're done |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 314 | if (isset($this->routes[$uri])) |
| 315 | { |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 316 | return $this->_set_request(explode('/', $this->routes[$uri])); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 317 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 318 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 319 | // Loop through the route array looking for wild-cards |
| 320 | foreach ($this->routes as $key => $val) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 321 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 322 | // Convert wild-cards to RegEx |
| 323 | $key = str_replace(':any', '.+', str_replace(':num', '[0-9]+', $key)); |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 324 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 325 | // Does the RegEx match? |
| 326 | if (preg_match('#^'.$key.'$#', $uri)) |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 327 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 328 | // Do we have a back-reference? |
| 329 | if (strpos($val, '$') !== FALSE AND strpos($key, '(') !== FALSE) |
| 330 | { |
| 331 | $val = preg_replace('#^'.$key.'$#', $val, $uri); |
| 332 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 333 | |
| 334 | return $this->_set_request(explode('/', $val)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 335 | } |
| 336 | } |
| 337 | |
| 338 | // If we got this far it means we didn't encounter a |
| 339 | // matching route so we'll set the site default route |
| 340 | $this->_set_request($this->uri->segments); |
| 341 | } |
| 342 | |
| 343 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 344 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 345 | /** |
| 346 | * Set the class name |
| 347 | * |
| 348 | * @access public |
| 349 | * @param string |
| 350 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 351 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 352 | function set_class($class) |
| 353 | { |
Derek Jones | 2615e41 | 2010-10-06 17:51:16 -0500 | [diff] [blame] | 354 | $this->class = str_replace(array('/', '.'), '', $class); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 355 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 356 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 357 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 358 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 359 | /** |
| 360 | * Fetch the current class |
| 361 | * |
| 362 | * @access public |
| 363 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 364 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 365 | function fetch_class() |
| 366 | { |
| 367 | return $this->class; |
| 368 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 369 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 370 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 371 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 372 | /** |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 373 | * Set the method name |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 374 | * |
| 375 | * @access public |
| 376 | * @param string |
| 377 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 378 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 379 | function set_method($method) |
| 380 | { |
| 381 | $this->method = $method; |
| 382 | } |
| 383 | |
| 384 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 385 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 386 | /** |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 387 | * Fetch the current method |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 388 | * |
| 389 | * @access public |
| 390 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 391 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 392 | function fetch_method() |
| 393 | { |
| 394 | if ($this->method == $this->fetch_class()) |
| 395 | { |
| 396 | return 'index'; |
| 397 | } |
| 398 | |
| 399 | return $this->method; |
| 400 | } |
| 401 | |
| 402 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 403 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 404 | /** |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 405 | * Set the directory name |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 406 | * |
| 407 | * @access public |
| 408 | * @param string |
| 409 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 410 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 411 | function set_directory($dir) |
| 412 | { |
Derek Jones | 2615e41 | 2010-10-06 17:51:16 -0500 | [diff] [blame] | 413 | $this->directory = str_replace(array('/', '.'), '', $dir).'/'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 417 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 418 | /** |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 419 | * Fetch the sub-directory (if any) that contains the requested controller class |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 420 | * |
| 421 | * @access public |
| 422 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 423 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 424 | function fetch_directory() |
| 425 | { |
| 426 | return $this->directory; |
| 427 | } |
| 428 | |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 429 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 430 | |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 431 | /** |
Derek Jones | 4b9c629 | 2011-07-01 17:40:48 -0500 | [diff] [blame] | 432 | * Set the controller overrides |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 433 | * |
| 434 | * @access public |
| 435 | * @param array |
| 436 | * @return null |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 437 | */ |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 438 | function _set_overrides($routing) |
| 439 | { |
| 440 | if ( ! is_array($routing)) |
| 441 | { |
| 442 | return; |
| 443 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 444 | |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 445 | if (isset($routing['directory'])) |
| 446 | { |
| 447 | $this->set_directory($routing['directory']); |
| 448 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 449 | |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 450 | if (isset($routing['controller']) AND $routing['controller'] != '') |
| 451 | { |
| 452 | $this->set_class($routing['controller']); |
| 453 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 454 | |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 455 | if (isset($routing['function'])) |
| 456 | { |
| 457 | $routing['function'] = ($routing['function'] == '') ? 'index' : $routing['function']; |
| 458 | $this->set_method($routing['function']); |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 463 | } |
| 464 | // END Router Class |
| 465 | |
| 466 | /* End of file Router.php */ |
Derek Jones | c68dfbf | 2010-03-02 12:59:23 -0600 | [diff] [blame] | 467 | /* Location: ./system/core/Router.php */ |