blob: 529cbb7c8e21507e18a4cf4e50dbc29d3416390b [file] [log] [blame]
Andrey Andreevba6c0412012-01-07 21:10:09 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreevba6c0412012-01-07 21:10:09 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreevba6c0412012-01-07 21:10:09 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * 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 Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * Router Class
30 *
31 * Parses URIs and determines routing
32 *
33 * @package CodeIgniter
34 * @subpackage Libraries
Derek Allard2067d1a2008-11-13 22:59:24 +000035 * @category Libraries
Andrey Andreev92ebfb62012-05-17 12:49:24 +030036 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000037 * @link http://codeigniter.com/user_guide/general/routing.html
38 */
39class CI_Router {
40
David Behler07b53422011-08-15 00:25:06 +020041 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +030042 * CI_Config class object
David Behler07b53422011-08-15 00:25:06 +020043 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +030044 * @var object
David Behler07b53422011-08-15 00:25:06 +020045 */
Andrey Andreevba6c0412012-01-07 21:10:09 +020046 public $config;
Andrey Andreev92ebfb62012-05-17 12:49:24 +030047
David Behler07b53422011-08-15 00:25:06 +020048 /**
49 * List of routes
50 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +030051 * @var array
David Behler07b53422011-08-15 00:25:06 +020052 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040053 public $routes = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +030054
David Behler07b53422011-08-15 00:25:06 +020055 /**
56 * List of error routes
57 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +030058 * @var array
David Behler07b53422011-08-15 00:25:06 +020059 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040060 public $error_routes = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +030061
David Behler07b53422011-08-15 00:25:06 +020062 /**
63 * Current class name
64 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +030065 * @var string
David Behler07b53422011-08-15 00:25:06 +020066 */
Andrey Andreev92ebfb62012-05-17 12:49:24 +030067 public $class = '';
68
David Behler07b53422011-08-15 00:25:06 +020069 /**
70 * Current method name
71 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +030072 * @var string
David Behler07b53422011-08-15 00:25:06 +020073 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040074 public $method = 'index';
Andrey Andreev92ebfb62012-05-17 12:49:24 +030075
David Behler07b53422011-08-15 00:25:06 +020076 /**
77 * Sub-directory that contains the requested controller class
78 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +030079 * @var string
David Behler07b53422011-08-15 00:25:06 +020080 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040081 public $directory = '';
Andrey Andreev92ebfb62012-05-17 12:49:24 +030082
David Behler07b53422011-08-15 00:25:06 +020083 /**
84 * Default controller (and method if specific)
85 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +030086 * @var string
David Behler07b53422011-08-15 00:25:06 +020087 */
Andrey Andreevba6c0412012-01-07 21:10:09 +020088 public $default_controller;
Barry Mienydd671972010-10-04 16:33:58 +020089
Derek Allard2067d1a2008-11-13 22:59:24 +000090 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +030091 * Class constructor
Derek Allard2067d1a2008-11-13 22:59:24 +000092 *
93 * Runs the route mapping function.
Andrey Andreev92ebfb62012-05-17 12:49:24 +030094 *
95 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +000096 */
Andrey Andreevba6c0412012-01-07 21:10:09 +020097 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000098 {
Derek Jonesc7738402010-03-02 13:55:13 -060099 $this->config =& load_class('Config', 'core');
100 $this->uri =& load_class('URI', 'core');
Andrey Andreevba6c0412012-01-07 21:10:09 +0200101 log_message('debug', 'Router Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000102 }
Barry Mienydd671972010-10-04 16:33:58 +0200103
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200105
Derek Allard2067d1a2008-11-13 22:59:24 +0000106 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300107 * Set route mapping
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300109 * Determines what should be served based on the URI request,
Derek Allard2067d1a2008-11-13 22:59:24 +0000110 * as well as any "routes" that have been set in the routing config file.
111 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000112 * @return void
113 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200114 public function _set_routing()
Barry Mienydd671972010-10-04 16:33:58 +0200115 {
Andrey Andreevba6c0412012-01-07 21:10:09 +0200116 // Are query strings enabled in the config file? Normally CI doesn't utilize query strings
Barry Mienydd671972010-10-04 16:33:58 +0200117 // since URI segments are more search-engine friendly, but they can optionally be used.
Derek Jonesc7738402010-03-02 13:55:13 -0600118 // If this feature is enabled, we will gather the directory/class/method a little differently
119 $segments = array();
Andrey Andreev7b53d042012-03-26 23:02:32 +0300120 if ($this->config->item('enable_query_strings') === TRUE && isset($_GET[$this->config->item('controller_trigger')]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 {
Derek Jonesc7738402010-03-02 13:55:13 -0600122 if (isset($_GET[$this->config->item('directory_trigger')]))
123 {
124 $this->set_directory(trim($this->uri->_filter_uri($_GET[$this->config->item('directory_trigger')])));
125 $segments[] = $this->fetch_directory();
126 }
Barry Mienydd671972010-10-04 16:33:58 +0200127
Derek Jonesc7738402010-03-02 13:55:13 -0600128 if (isset($_GET[$this->config->item('controller_trigger')]))
129 {
130 $this->set_class(trim($this->uri->_filter_uri($_GET[$this->config->item('controller_trigger')])));
131 $segments[] = $this->fetch_class();
132 }
Barry Mienydd671972010-10-04 16:33:58 +0200133
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 if (isset($_GET[$this->config->item('function_trigger')]))
135 {
136 $this->set_method(trim($this->uri->_filter_uri($_GET[$this->config->item('function_trigger')])));
Derek Jonesc7738402010-03-02 13:55:13 -0600137 $segments[] = $this->fetch_method();
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 }
Barry Mienydd671972010-10-04 16:33:58 +0200140
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 // Load the routes.php file.
Andrey Andreev7b53d042012-03-26 23:02:32 +0300142 if (defined('ENVIRONMENT') && is_file(APPPATH.'config/'.ENVIRONMENT.'/routes.php'))
Greg Akerd96f8822011-12-27 16:23:47 -0600143 {
144 include(APPPATH.'config/'.ENVIRONMENT.'/routes.php');
145 }
146 elseif (is_file(APPPATH.'config/routes.php'))
147 {
148 include(APPPATH.'config/routes.php');
149 }
David Behler07b53422011-08-15 00:25:06 +0200150
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 $this->routes = ( ! isset($route) OR ! is_array($route)) ? array() : $route;
152 unset($route);
Barry Mienydd671972010-10-04 16:33:58 +0200153
Derek Allard2067d1a2008-11-13 22:59:24 +0000154 // Set the default controller so we can display it in the event
155 // the URI doesn't correlated to a valid controller.
Andrey Andreev7b53d042012-03-26 23:02:32 +0300156 $this->default_controller = empty($this->routes['default_controller']) ? FALSE : strtolower($this->routes['default_controller']);
Barry Mienydd671972010-10-04 16:33:58 +0200157
Andrey Andreevba6c0412012-01-07 21:10:09 +0200158 // Were there any query string segments? If so, we'll validate them and bail out since we're done.
Derek Jonesc7738402010-03-02 13:55:13 -0600159 if (count($segments) > 0)
160 {
161 return $this->_validate_request($segments);
162 }
Barry Mienydd671972010-10-04 16:33:58 +0200163
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 // Fetch the complete URI string
165 $this->uri->_fetch_uri_string();
Barry Mienydd671972010-10-04 16:33:58 +0200166
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 // Is there a URI string? If not, the default controller specified in the "routes" file will be shown.
Andrey Andreev9ba661b2012-06-04 14:44:34 +0300168 if ($this->uri->uri_string == '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 {
Derek Jonesc7738402010-03-02 13:55:13 -0600170 return $this->_set_default_controller();
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 }
Barry Mienydd671972010-10-04 16:33:58 +0200172
Andrey Andreevba6c0412012-01-07 21:10:09 +0200173 $this->uri->_remove_url_suffix(); // Remove the URL suffix
174 $this->uri->_explode_segments(); // Compile the segments into an array
175 $this->_parse_routes(); // Parse any custom routing that may exist
176 $this->uri->_reindex_segments(); // Re-index the segment array so that it starts with 1 rather than 0
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 }
Derek Jonesc7738402010-03-02 13:55:13 -0600178
179 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200180
Derek Jonesc7738402010-03-02 13:55:13 -0600181 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300182 * Set default controller
Derek Jonesc7738402010-03-02 13:55:13 -0600183 *
Derek Jonesc7738402010-03-02 13:55:13 -0600184 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200185 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200186 protected function _set_default_controller()
Derek Jonesc7738402010-03-02 13:55:13 -0600187 {
188 if ($this->default_controller === FALSE)
189 {
Andrey Andreevba6c0412012-01-07 21:10:09 +0200190 show_error('Unable to determine what should be displayed. A default route has not been specified in the routing file.');
Derek Jonesc7738402010-03-02 13:55:13 -0600191 }
192 // Is the method being specified?
193 if (strpos($this->default_controller, '/') !== FALSE)
194 {
195 $x = explode('/', $this->default_controller);
Derek Jonesc7738402010-03-02 13:55:13 -0600196 $this->set_class($x[0]);
197 $this->set_method($x[1]);
Pascal Kriete790ebf32010-12-15 10:53:35 -0500198 $this->_set_request($x);
Barry Mienydd671972010-10-04 16:33:58 +0200199 }
Derek Jonesc7738402010-03-02 13:55:13 -0600200 else
201 {
202 $this->set_class($this->default_controller);
203 $this->set_method('index');
204 $this->_set_request(array($this->default_controller, 'index'));
205 }
Barry Mienydd671972010-10-04 16:33:58 +0200206
Derek Jonesc7738402010-03-02 13:55:13 -0600207 // re-index the routed segments array so it starts with 1 rather than 0
208 $this->uri->_reindex_segments();
Barry Mienydd671972010-10-04 16:33:58 +0200209
Andrey Andreevba6c0412012-01-07 21:10:09 +0200210 log_message('debug', 'No URI present. Default controller set.');
Derek Jonesc7738402010-03-02 13:55:13 -0600211 }
Barry Mienydd671972010-10-04 16:33:58 +0200212
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200214
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300216 * Set request route
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300218 * Takes an array of URI segments as input and sets the class/method
219 * to be called.
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300221 * @param array $segments URI segments
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 * @return void
223 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200224 protected function _set_request($segments = array())
Barry Mienydd671972010-10-04 16:33:58 +0200225 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 $segments = $this->_validate_request($segments);
Barry Mienydd671972010-10-04 16:33:58 +0200227
Andrey Andreevba6c0412012-01-07 21:10:09 +0200228 if (count($segments) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 {
Derek Jonesc7738402010-03-02 13:55:13 -0600230 return $this->_set_default_controller();
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 }
Barry Mienydd671972010-10-04 16:33:58 +0200232
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 $this->set_class($segments[0]);
Barry Mienydd671972010-10-04 16:33:58 +0200234
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 if (isset($segments[1]))
236 {
Derek Jonesc7738402010-03-02 13:55:13 -0600237 // A standard method request
238 $this->set_method($segments[1]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 }
240 else
241 {
242 // This lets the "routed" segment array identify that the default
243 // index method is being used.
244 $segments[1] = 'index';
245 }
Barry Mienydd671972010-10-04 16:33:58 +0200246
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 // Update our "routed" segment array to contain the segments.
248 // Note: If there is no custom routing, this array will be
Phil Sturgeon81aa94b2012-05-02 11:40:46 +0100249 // identical to $this->uri->segments
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 $this->uri->rsegments = $segments;
251 }
Barry Mienydd671972010-10-04 16:33:58 +0200252
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200254
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300256 * Validate request
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300258 * Attempts validate the URI request and determine the controller path.
259 *
260 * @param array $segments URI segments
261 * @return array URI segments
Barry Mienydd671972010-10-04 16:33:58 +0200262 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200263 protected function _validate_request($segments)
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 {
Andrey Andreevba6c0412012-01-07 21:10:09 +0200265 if (count($segments) === 0)
Derek Jonesc7738402010-03-02 13:55:13 -0600266 {
267 return $segments;
268 }
Barry Mienydd671972010-10-04 16:33:58 +0200269
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 // Does the requested controller exist in the root folder?
Greg Aker3a746652011-04-19 10:59:47 -0500271 if (file_exists(APPPATH.'controllers/'.$segments[0].'.php'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 {
273 return $segments;
274 }
Barry Mienydd671972010-10-04 16:33:58 +0200275
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 // Is the controller in a sub-folder?
277 if (is_dir(APPPATH.'controllers/'.$segments[0]))
Derek Jonesc7738402010-03-02 13:55:13 -0600278 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 // Set the directory and remove it from the segment array
280 $this->set_directory($segments[0]);
281 $segments = array_slice($segments, 1);
Barry Mienydd671972010-10-04 16:33:58 +0200282
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 if (count($segments) > 0)
284 {
285 // Does the requested controller exist in the sub-folder?
Greg Aker3a746652011-04-19 10:59:47 -0500286 if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].'.php'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 {
Shane Pearson664a9352011-08-10 16:02:32 -0500288 if ( ! empty($this->routes['404_override']))
289 {
290 $x = explode('/', $this->routes['404_override']);
Shane Pearson664a9352011-08-10 16:02:32 -0500291 $this->set_directory('');
292 $this->set_class($x[0]);
293 $this->set_method(isset($x[1]) ? $x[1] : 'index');
David Behler07b53422011-08-15 00:25:06 +0200294
Shane Pearson664a9352011-08-10 16:02:32 -0500295 return $x;
296 }
297 else
298 {
299 show_404($this->fetch_directory().$segments[0]);
300 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 }
302 }
303 else
304 {
Derek Jonesc7738402010-03-02 13:55:13 -0600305 // Is the method being specified in the route?
306 if (strpos($this->default_controller, '/') !== FALSE)
307 {
308 $x = explode('/', $this->default_controller);
Derek Jonesc7738402010-03-02 13:55:13 -0600309 $this->set_class($x[0]);
310 $this->set_method($x[1]);
Barry Mienydd671972010-10-04 16:33:58 +0200311 }
Derek Jonesc7738402010-03-02 13:55:13 -0600312 else
313 {
314 $this->set_class($this->default_controller);
315 $this->set_method('index');
316 }
Barry Mienydd671972010-10-04 16:33:58 +0200317
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 // Does the default controller exist in the sub-folder?
Greg Aker3a746652011-04-19 10:59:47 -0500319 if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.'.php'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 {
321 $this->directory = '';
322 return array();
323 }
Barry Mienydd671972010-10-04 16:33:58 +0200324
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 }
Barry Mienydd671972010-10-04 16:33:58 +0200326
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 return $segments;
328 }
Barry Mienydd671972010-10-04 16:33:58 +0200329
330
Derek Jonesc7738402010-03-02 13:55:13 -0600331 // If we've gotten this far it means that the URI does not correlate to a valid
Andrey Andreevba6c0412012-01-07 21:10:09 +0200332 // controller class. We will now see if there is an override
Eric Barnesc5bf6162011-01-30 21:17:11 -0500333 if ( ! empty($this->routes['404_override']))
Derek Jonesc7738402010-03-02 13:55:13 -0600334 {
Phil Sturgeon23174a62010-12-15 15:18:16 +0000335 $x = explode('/', $this->routes['404_override']);
Phil Sturgeon23174a62010-12-15 15:18:16 +0000336 $this->set_class($x[0]);
337 $this->set_method(isset($x[1]) ? $x[1] : 'index');
Barry Mienydd671972010-10-04 16:33:58 +0200338
Phil Sturgeon23174a62010-12-15 15:18:16 +0000339 return $x;
Derek Jonesc7738402010-03-02 13:55:13 -0600340 }
Barry Mienydd671972010-10-04 16:33:58 +0200341
Derek Jonesc7738402010-03-02 13:55:13 -0600342 // Nothing else to do at this point but show a 404
Barry Mienydd671972010-10-04 16:33:58 +0200343 show_404($segments[0]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 }
Barry Mienydd671972010-10-04 16:33:58 +0200345
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200347
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 /**
Andrey Andreev7b53d042012-03-26 23:02:32 +0300349 * Parse Routes
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300351 * Matches any routes that may exist in the config/routes.php file
352 * against the URI to determine if the class/method need to be remapped.
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 * @return void
355 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200356 protected function _parse_routes()
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100357 {
358 // Turn the segment array into a URI string
359 $uri = implode('/', $this->uri->segments);
Barry Mienydd671972010-10-04 16:33:58 +0200360
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100361 // Is there a literal match? If so we're done
362 if (isset($this->routes[$uri]))
363 {
364 return $this->_set_request(explode('/', $this->routes[$uri]));
365 }
Barry Mienydd671972010-10-04 16:33:58 +0200366
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100367 // Loop through the route array looking for wild-cards
368 foreach ($this->routes as $key => $val)
369 {
370 // Convert wild-cards to RegEx
Andrey Andreev7676c2d2012-10-30 13:42:01 +0200371 $key = str_replace(array(':any', ':num'), array('[^/]+', '[0-9]+'), $key);
Derek Jonesc7738402010-03-02 13:55:13 -0600372
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100373 // Does the RegEx match?
374 if (preg_match('#^'.$key.'$#', $uri, $matches))
375 {
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100376 // Are we using callbacks to process back-references?
Jonatas Miguel24296ca2012-09-27 12:10:01 +0100377 if ( ! is_string($val) && is_callable($val))
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100378 {
379 // Remove the original string from the matches array.
380 array_shift($matches);
Derek Allard2067d1a2008-11-13 22:59:24 +0000381
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100382 // Get the match count.
383 $match_count = count($matches);
Jonatas Miguelc0d98b22012-08-06 15:42:50 +0100384
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100385 // Determine how many parameters the callback has.
386 $reflection = new ReflectionFunction($val);
387 $param_count = $reflection->getNumberOfParameters();
Jonatas Miguelc0d98b22012-08-06 15:42:50 +0100388
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100389 // Are there more parameters than matches?
Jonatas Miguel24296ca2012-09-27 12:10:01 +0100390 if ($param_count > $match_count)
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100391 {
392 // Any params without matches will be set to an empty string.
393 $matches = array_merge($matches, array_fill($match_count, $param_count - $match_count, ''));
Jonatas Miguelefb81662012-10-23 19:52:46 +0100394
395 $match_count = $param_count;
396 }
397
398 // Get the parameters so we can use their default values.
399 $params = $reflection->getParameters();
400
401 for ($m = 0; $m < $match_count; $m++)
402 {
403 // Is the match empty and does a default value exist?
404 if (empty($matches[$m]) && $params[$m]->isDefaultValueAvailable())
405 {
406 // Substitute the empty match for the default value.
407 $matches[$m] = $params[$m]->getDefaultValue();
408 }
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100409 }
Jonatas Miguelc0d98b22012-08-06 15:42:50 +0100410
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100411 // Execute the callback using the values in matches as its parameters.
412 $val = call_user_func_array($val, $matches);
413 }
414 // Are we using the default routing method for back-references?
Jonatas Miguelf950e5c2012-09-27 11:39:33 +0100415 elseif (strpos($val, '$') !== FALSE && strpos($key, '(') !== FALSE)
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100416 {
417 $val = preg_replace('#^'.$key.'$#', $val, $uri);
418 }
Jonatas Miguelc0d98b22012-08-06 15:42:50 +0100419
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100420 return $this->_set_request(explode('/', $val));
421 }
422 }
Jonatas Miguelc0d98b22012-08-06 15:42:50 +0100423
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100424 // If we got this far it means we didn't encounter a
425 // matching route so we'll set the site default route
426 $this->_set_request($this->uri->segments);
427 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000428
429 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200430
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300432 * Set class name
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300434 * @param string $class Class name
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200436 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200437 public function set_class($class)
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 {
Derek Jones2615e412010-10-06 17:51:16 -0500439 $this->class = str_replace(array('/', '.'), '', $class);
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 }
Barry Mienydd671972010-10-04 16:33:58 +0200441
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200443
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 /**
445 * Fetch the current class
446 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200448 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200449 public function fetch_class()
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 {
451 return $this->class;
452 }
Barry Mienydd671972010-10-04 16:33:58 +0200453
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200455
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300457 * Set method name
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300459 * @param string $method Method name
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200461 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200462 public function set_method($method)
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 {
464 $this->method = $method;
465 }
466
467 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200468
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 /**
Andrey Andreev7b53d042012-03-26 23:02:32 +0300470 * Fetch the current method
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200473 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200474 public function fetch_method()
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 {
Alex Bilbieed944a32012-06-02 11:07:47 +0100476 return ($this->method === $this->fetch_class()) ? 'index' : $this->method;
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 }
478
479 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200480
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300482 * Set directory name
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300484 * @param string $dir Directory name
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200486 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200487 public function set_directory($dir)
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 {
Derek Jones2615e412010-10-06 17:51:16 -0500489 $this->directory = str_replace(array('/', '.'), '', $dir).'/';
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 }
491
492 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200493
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300495 * Fetch directory
496 *
497 * Feches the sub-directory (if any) that contains the requested
498 * controller class.
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200501 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200502 public function fetch_directory()
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 {
504 return $this->directory;
505 }
506
Derek Jonesc7738402010-03-02 13:55:13 -0600507 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200508
Derek Jonesc7738402010-03-02 13:55:13 -0600509 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300510 * Set controller overrides
Derek Jonesc7738402010-03-02 13:55:13 -0600511 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300512 * @param array $routing Route overrides
Andrey Andreev7b53d042012-03-26 23:02:32 +0300513 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200514 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200515 public function _set_overrides($routing)
Derek Jonesc7738402010-03-02 13:55:13 -0600516 {
517 if ( ! is_array($routing))
518 {
519 return;
520 }
Barry Mienydd671972010-10-04 16:33:58 +0200521
Derek Jonesc7738402010-03-02 13:55:13 -0600522 if (isset($routing['directory']))
523 {
524 $this->set_directory($routing['directory']);
525 }
Barry Mienydd671972010-10-04 16:33:58 +0200526
Andrey Andreev9ba661b2012-06-04 14:44:34 +0300527 if ( ! empty($routing['controller']))
Derek Jonesc7738402010-03-02 13:55:13 -0600528 {
529 $this->set_class($routing['controller']);
530 }
Barry Mienydd671972010-10-04 16:33:58 +0200531
Derek Jonesc7738402010-03-02 13:55:13 -0600532 if (isset($routing['function']))
533 {
Andrey Andreev9ba661b2012-06-04 14:44:34 +0300534 $routing['function'] = ($routing['function'] == '') ? 'index' : $routing['function'];
Derek Jonesc7738402010-03-02 13:55:13 -0600535 $this->set_method($routing['function']);
536 }
537 }
538
Derek Allard2067d1a2008-11-13 22:59:24 +0000539}
Derek Allard2067d1a2008-11-13 22:59:24 +0000540
541/* End of file Router.php */
Andrey Andreev7b53d042012-03-26 23:02:32 +0300542/* Location: ./system/core/Router.php */