Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -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 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame^] | 7 | * NOTICE OF LICENSE |
| 8 | * |
| 9 | * Licensed under the Open Software License version 3.0 |
| 10 | * |
| 11 | * This source file is subject to the Open Software License (OSL 3.0) that is |
| 12 | * bundled with this package in the files license.txt / license.rst. It is |
| 13 | * also available through the world wide web at this URL: |
| 14 | * http://opensource.org/licenses/OSL-3.0 |
| 15 | * If you did not receive a copy of the license and are unable to obtain it |
| 16 | * through the world wide web, please send an email to |
| 17 | * licensing@ellislab.com so we can send you a copy immediately. |
| 18 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame^] | 20 | * @author EllisLab Dev Team |
| 21 | * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. (http://ellislab.com/) |
| 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 1.0 |
| 25 | * @filesource |
| 26 | */ |
| 27 | |
| 28 | // ------------------------------------------------------------------------ |
| 29 | |
| 30 | /** |
| 31 | * Router Class |
| 32 | * |
| 33 | * Parses URIs and determines routing |
| 34 | * |
| 35 | * @package CodeIgniter |
| 36 | * @subpackage Libraries |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame^] | 37 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 38 | * @category Libraries |
| 39 | * @link http://codeigniter.com/user_guide/general/routing.html |
| 40 | */ |
| 41 | class CI_Router { |
| 42 | |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 43 | /** |
| 44 | * Config class |
| 45 | * |
| 46 | * @var object |
| 47 | * @access public |
| 48 | */ |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 49 | var $config; |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 50 | /** |
| 51 | * List of routes |
| 52 | * |
| 53 | * @var array |
| 54 | * @access public |
| 55 | */ |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 56 | var $routes = array(); |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 57 | /** |
| 58 | * List of error routes |
| 59 | * |
| 60 | * @var array |
| 61 | * @access public |
| 62 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 63 | var $error_routes = array(); |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 64 | /** |
| 65 | * Current class name |
| 66 | * |
| 67 | * @var string |
| 68 | * @access public |
| 69 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 70 | var $class = ''; |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 71 | /** |
| 72 | * Current method name |
| 73 | * |
| 74 | * @var string |
| 75 | * @access public |
| 76 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 77 | var $method = 'index'; |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 78 | /** |
| 79 | * Sub-directory that contains the requested controller class |
| 80 | * |
| 81 | * @var string |
| 82 | * @access public |
| 83 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 84 | var $directory = ''; |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 85 | /** |
| 86 | * Default controller (and method if specific) |
| 87 | * |
| 88 | * @var string |
| 89 | * @access public |
| 90 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 91 | var $default_controller; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 92 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 93 | /** |
| 94 | * Constructor |
| 95 | * |
| 96 | * Runs the route mapping function. |
| 97 | */ |
Greg Aker | a926328 | 2010-11-10 15:26:43 -0600 | [diff] [blame] | 98 | function __construct() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 99 | { |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 100 | $this->config =& load_class('Config', 'core'); |
| 101 | $this->uri =& load_class('URI', 'core'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 102 | log_message('debug', "Router Class Initialized"); |
| 103 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 104 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 105 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 106 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 107 | /** |
| 108 | * Set the route mapping |
| 109 | * |
| 110 | * This function determines what should be served based on the URI request, |
| 111 | * as well as any "routes" that have been set in the routing config file. |
| 112 | * |
| 113 | * @access private |
| 114 | * @return void |
| 115 | */ |
| 116 | function _set_routing() |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 117 | { |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 118 | // 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] | 119 | // 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] | 120 | // If this feature is enabled, we will gather the directory/class/method a little differently |
| 121 | $segments = array(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 122 | if ($this->config->item('enable_query_strings') === TRUE AND isset($_GET[$this->config->item('controller_trigger')])) |
| 123 | { |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 124 | if (isset($_GET[$this->config->item('directory_trigger')])) |
| 125 | { |
| 126 | $this->set_directory(trim($this->uri->_filter_uri($_GET[$this->config->item('directory_trigger')]))); |
| 127 | $segments[] = $this->fetch_directory(); |
| 128 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 129 | |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 130 | if (isset($_GET[$this->config->item('controller_trigger')])) |
| 131 | { |
| 132 | $this->set_class(trim($this->uri->_filter_uri($_GET[$this->config->item('controller_trigger')]))); |
| 133 | $segments[] = $this->fetch_class(); |
| 134 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 135 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 136 | if (isset($_GET[$this->config->item('function_trigger')])) |
| 137 | { |
| 138 | $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] | 139 | $segments[] = $this->fetch_method(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 140 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 141 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 142 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 143 | // Load the routes.php file. |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [diff] [blame] | 144 | if (defined('ENVIRONMENT') AND is_file(APPPATH.'config/'.ENVIRONMENT.'/routes.php')) |
bubbafoley | 0ea0414 | 2011-03-17 14:55:41 -0500 | [diff] [blame] | 145 | { |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [diff] [blame] | 146 | include(APPPATH.'config/'.ENVIRONMENT.'/routes.php'); |
bubbafoley | 0ea0414 | 2011-03-17 14:55:41 -0500 | [diff] [blame] | 147 | } |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [diff] [blame] | 148 | elseif (is_file(APPPATH.'config/routes.php')) |
bubbafoley | 0ea0414 | 2011-03-17 14:55:41 -0500 | [diff] [blame] | 149 | { |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [diff] [blame] | 150 | include(APPPATH.'config/routes.php'); |
bubbafoley | 0ea0414 | 2011-03-17 14:55:41 -0500 | [diff] [blame] | 151 | } |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 152 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 153 | $this->routes = ( ! isset($route) OR ! is_array($route)) ? array() : $route; |
| 154 | unset($route); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 155 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 156 | // Set the default controller so we can display it in the event |
| 157 | // the URI doesn't correlated to a valid controller. |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 158 | $this->default_controller = ( ! isset($this->routes['default_controller']) OR $this->routes['default_controller'] == '') ? FALSE : strtolower($this->routes['default_controller']); |
| 159 | |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 160 | // 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] | 161 | if (count($segments) > 0) |
| 162 | { |
| 163 | return $this->_validate_request($segments); |
| 164 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 165 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 166 | // Fetch the complete URI string |
| 167 | $this->uri->_fetch_uri_string(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 168 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 169 | // Is there a URI string? If not, the default controller specified in the "routes" file will be shown. |
| 170 | if ($this->uri->uri_string == '') |
| 171 | { |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 172 | return $this->_set_default_controller(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 173 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 174 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 175 | // Do we need to remove the URL suffix? |
| 176 | $this->uri->_remove_url_suffix(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 177 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 178 | // Compile the segments into an array |
| 179 | $this->uri->_explode_segments(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 180 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 181 | // Parse any custom routing that may exist |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 182 | $this->_parse_routes(); |
| 183 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 184 | // Re-index the segment array so that it starts with 1 rather than 0 |
| 185 | $this->uri->_reindex_segments(); |
| 186 | } |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 187 | |
| 188 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 189 | |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 190 | /** |
| 191 | * Set the default controller |
| 192 | * |
| 193 | * @access private |
| 194 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 195 | */ |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 196 | function _set_default_controller() |
| 197 | { |
| 198 | if ($this->default_controller === FALSE) |
| 199 | { |
| 200 | show_error("Unable to determine what should be displayed. A default route has not been specified in the routing file."); |
| 201 | } |
| 202 | // Is the method being specified? |
| 203 | if (strpos($this->default_controller, '/') !== FALSE) |
| 204 | { |
| 205 | $x = explode('/', $this->default_controller); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 206 | |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 207 | $this->set_class($x[0]); |
| 208 | $this->set_method($x[1]); |
Pascal Kriete | 790ebf3 | 2010-12-15 10:53:35 -0500 | [diff] [blame] | 209 | $this->_set_request($x); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 210 | } |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 211 | else |
| 212 | { |
| 213 | $this->set_class($this->default_controller); |
| 214 | $this->set_method('index'); |
| 215 | $this->_set_request(array($this->default_controller, 'index')); |
| 216 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 217 | |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 218 | // re-index the routed segments array so it starts with 1 rather than 0 |
| 219 | $this->uri->_reindex_segments(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 220 | |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 221 | log_message('debug', "No URI present. Default controller set."); |
| 222 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 223 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 224 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 225 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 226 | /** |
| 227 | * Set the Route |
| 228 | * |
| 229 | * This function takes an array of URI segments as |
| 230 | * input, and sets the current class/method |
| 231 | * |
| 232 | * @access private |
| 233 | * @param array |
| 234 | * @param bool |
| 235 | * @return void |
| 236 | */ |
| 237 | function _set_request($segments = array()) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 238 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 239 | $segments = $this->_validate_request($segments); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 240 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 241 | if (count($segments) == 0) |
| 242 | { |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 243 | return $this->_set_default_controller(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 244 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 245 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 246 | $this->set_class($segments[0]); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 247 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 248 | if (isset($segments[1])) |
| 249 | { |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 250 | // A standard method request |
| 251 | $this->set_method($segments[1]); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 252 | } |
| 253 | else |
| 254 | { |
| 255 | // This lets the "routed" segment array identify that the default |
| 256 | // index method is being used. |
| 257 | $segments[1] = 'index'; |
| 258 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 259 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 260 | // Update our "routed" segment array to contain the segments. |
| 261 | // Note: If there is no custom routing, this array will be |
| 262 | // identical to $this->uri->segments |
| 263 | $this->uri->rsegments = $segments; |
| 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 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 267 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 268 | /** |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 269 | * Validates the supplied segments. Attempts to determine the path to |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 270 | * the controller. |
| 271 | * |
| 272 | * @access private |
| 273 | * @param array |
| 274 | * @return array |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 275 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 276 | function _validate_request($segments) |
| 277 | { |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 278 | if (count($segments) == 0) |
| 279 | { |
| 280 | return $segments; |
| 281 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 282 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 283 | // Does the requested controller exist in the root folder? |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [diff] [blame] | 284 | if (file_exists(APPPATH.'controllers/'.$segments[0].'.php')) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 285 | { |
| 286 | return $segments; |
| 287 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 288 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 289 | // Is the controller in a sub-folder? |
| 290 | if (is_dir(APPPATH.'controllers/'.$segments[0])) |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 291 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 292 | // Set the directory and remove it from the segment array |
| 293 | $this->set_directory($segments[0]); |
| 294 | $segments = array_slice($segments, 1); |
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 | if (count($segments) > 0) |
| 297 | { |
| 298 | // Does the requested controller exist in the sub-folder? |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [diff] [blame] | 299 | if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].'.php')) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 300 | { |
Shane Pearson | 664a935 | 2011-08-10 16:02:32 -0500 | [diff] [blame] | 301 | if ( ! empty($this->routes['404_override'])) |
| 302 | { |
| 303 | $x = explode('/', $this->routes['404_override']); |
| 304 | |
| 305 | $this->set_directory(''); |
| 306 | $this->set_class($x[0]); |
| 307 | $this->set_method(isset($x[1]) ? $x[1] : 'index'); |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 308 | |
Shane Pearson | 664a935 | 2011-08-10 16:02:32 -0500 | [diff] [blame] | 309 | return $x; |
| 310 | } |
| 311 | else |
| 312 | { |
| 313 | show_404($this->fetch_directory().$segments[0]); |
| 314 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 315 | } |
| 316 | } |
| 317 | else |
| 318 | { |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 319 | // Is the method being specified in the route? |
| 320 | if (strpos($this->default_controller, '/') !== FALSE) |
| 321 | { |
| 322 | $x = explode('/', $this->default_controller); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 323 | |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 324 | $this->set_class($x[0]); |
| 325 | $this->set_method($x[1]); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 326 | } |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 327 | else |
| 328 | { |
| 329 | $this->set_class($this->default_controller); |
| 330 | $this->set_method('index'); |
| 331 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 332 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 333 | // Does the default controller exist in the sub-folder? |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [diff] [blame] | 334 | if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.'.php')) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 335 | { |
| 336 | $this->directory = ''; |
| 337 | return array(); |
| 338 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 339 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 340 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 341 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 342 | return $segments; |
| 343 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 344 | |
| 345 | |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 346 | // If we've gotten this far it means that the URI does not correlate to a valid |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 347 | // controller class. We will now see if there is an override |
Eric Barnes | c5bf616 | 2011-01-30 21:17:11 -0500 | [diff] [blame] | 348 | if ( ! empty($this->routes['404_override'])) |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 349 | { |
Phil Sturgeon | 23174a6 | 2010-12-15 15:18:16 +0000 | [diff] [blame] | 350 | $x = explode('/', $this->routes['404_override']); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 351 | |
Phil Sturgeon | 23174a6 | 2010-12-15 15:18:16 +0000 | [diff] [blame] | 352 | $this->set_class($x[0]); |
| 353 | $this->set_method(isset($x[1]) ? $x[1] : 'index'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 354 | |
Phil Sturgeon | 23174a6 | 2010-12-15 15:18:16 +0000 | [diff] [blame] | 355 | return $x; |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 356 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 357 | |
Phil Sturgeon | 23174a6 | 2010-12-15 15:18:16 +0000 | [diff] [blame] | 358 | |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 359 | // Nothing else to do at this point but show a 404 |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 360 | show_404($segments[0]); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 361 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 362 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 363 | // -------------------------------------------------------------------- |
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 | /** |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 366 | * Parse Routes |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 367 | * |
| 368 | * This function matches any routes that may exist in |
| 369 | * the config/routes.php file against the URI to |
| 370 | * determine if the class/method need to be remapped. |
| 371 | * |
| 372 | * @access private |
| 373 | * @return void |
| 374 | */ |
| 375 | function _parse_routes() |
| 376 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 377 | // Turn the segment array into a URI string |
| 378 | $uri = implode('/', $this->uri->segments); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 379 | |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 380 | // Is there a literal match? If so we're done |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 381 | if (isset($this->routes[$uri])) |
| 382 | { |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 383 | return $this->_set_request(explode('/', $this->routes[$uri])); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 | // Loop through the route array looking for wild-cards |
| 387 | foreach ($this->routes as $key => $val) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 388 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 389 | // Convert wild-cards to RegEx |
| 390 | $key = str_replace(':any', '.+', str_replace(':num', '[0-9]+', $key)); |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 391 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 392 | // Does the RegEx match? |
| 393 | if (preg_match('#^'.$key.'$#', $uri)) |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 394 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 395 | // Do we have a back-reference? |
| 396 | if (strpos($val, '$') !== FALSE AND strpos($key, '(') !== FALSE) |
| 397 | { |
| 398 | $val = preg_replace('#^'.$key.'$#', $val, $uri); |
| 399 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 400 | |
| 401 | return $this->_set_request(explode('/', $val)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 402 | } |
| 403 | } |
| 404 | |
| 405 | // If we got this far it means we didn't encounter a |
| 406 | // matching route so we'll set the site default route |
| 407 | $this->_set_request($this->uri->segments); |
| 408 | } |
| 409 | |
| 410 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 411 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 412 | /** |
| 413 | * Set the class name |
| 414 | * |
| 415 | * @access public |
| 416 | * @param string |
| 417 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 418 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 419 | function set_class($class) |
| 420 | { |
Derek Jones | 2615e41 | 2010-10-06 17:51:16 -0500 | [diff] [blame] | 421 | $this->class = str_replace(array('/', '.'), '', $class); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 422 | } |
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 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 425 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 426 | /** |
| 427 | * Fetch the current class |
| 428 | * |
| 429 | * @access public |
| 430 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 431 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 432 | function fetch_class() |
| 433 | { |
| 434 | return $this->class; |
| 435 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 436 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 437 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 438 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 439 | /** |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 440 | * Set the method name |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 441 | * |
| 442 | * @access public |
| 443 | * @param string |
| 444 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 445 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 446 | function set_method($method) |
| 447 | { |
| 448 | $this->method = $method; |
| 449 | } |
| 450 | |
| 451 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 452 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 453 | /** |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 454 | * Fetch the current method |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 455 | * |
| 456 | * @access public |
| 457 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 458 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 459 | function fetch_method() |
| 460 | { |
| 461 | if ($this->method == $this->fetch_class()) |
| 462 | { |
| 463 | return 'index'; |
| 464 | } |
| 465 | |
| 466 | return $this->method; |
| 467 | } |
| 468 | |
| 469 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 470 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 471 | /** |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 472 | * Set the directory name |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 473 | * |
| 474 | * @access public |
| 475 | * @param string |
| 476 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 477 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 478 | function set_directory($dir) |
| 479 | { |
Derek Jones | 2615e41 | 2010-10-06 17:51:16 -0500 | [diff] [blame] | 480 | $this->directory = str_replace(array('/', '.'), '', $dir).'/'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 484 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 485 | /** |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 486 | * Fetch the sub-directory (if any) that contains the requested controller class |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 487 | * |
| 488 | * @access public |
| 489 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 490 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 491 | function fetch_directory() |
| 492 | { |
| 493 | return $this->directory; |
| 494 | } |
| 495 | |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 496 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 497 | |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 498 | /** |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 499 | * Set the controller overrides |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 500 | * |
| 501 | * @access public |
| 502 | * @param array |
| 503 | * @return null |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 504 | */ |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 505 | function _set_overrides($routing) |
| 506 | { |
| 507 | if ( ! is_array($routing)) |
| 508 | { |
| 509 | return; |
| 510 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 511 | |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 512 | if (isset($routing['directory'])) |
| 513 | { |
| 514 | $this->set_directory($routing['directory']); |
| 515 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 516 | |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 517 | if (isset($routing['controller']) AND $routing['controller'] != '') |
| 518 | { |
| 519 | $this->set_class($routing['controller']); |
| 520 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 521 | |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 522 | if (isset($routing['function'])) |
| 523 | { |
| 524 | $routing['function'] = ($routing['function'] == '') ? 'index' : $routing['function']; |
| 525 | $this->set_method($routing['function']); |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 530 | } |
| 531 | // END Router Class |
| 532 | |
| 533 | /* End of file Router.php */ |
Derek Jones | c68dfbf | 2010-03-02 12:59:23 -0600 | [diff] [blame] | 534 | /* Location: ./system/core/Router.php */ |