blob: 30bf396573722051239559405d8f0cd186db5fe4 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
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 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * Router Class
31 *
32 * Parses URIs and determines routing
33 *
34 * @package CodeIgniter
35 * @subpackage Libraries
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @category Libraries
Andrey Andreev92ebfb62012-05-17 12:49:24 +030037 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000038 * @link http://codeigniter.com/user_guide/general/routing.html
39 */
40class CI_Router {
41
David Behler07b53422011-08-15 00:25:06 +020042 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +030043 * CI_Config class object
David Behler07b53422011-08-15 00:25:06 +020044 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +030045 * @var object
David Behler07b53422011-08-15 00:25:06 +020046 */
Andrey Andreevba6c0412012-01-07 21:10:09 +020047 public $config;
Andrey Andreev92ebfb62012-05-17 12:49:24 +030048
David Behler07b53422011-08-15 00:25:06 +020049 /**
50 * List of routes
51 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +030052 * @var array
David Behler07b53422011-08-15 00:25:06 +020053 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040054 public $routes = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +030055
David Behler07b53422011-08-15 00:25:06 +020056 /**
David Behler07b53422011-08-15 00:25:06 +020057 * Current class name
58 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +030059 * @var string
David Behler07b53422011-08-15 00:25:06 +020060 */
Andrey Andreev92ebfb62012-05-17 12:49:24 +030061 public $class = '';
62
David Behler07b53422011-08-15 00:25:06 +020063 /**
64 * Current method name
65 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +030066 * @var string
David Behler07b53422011-08-15 00:25:06 +020067 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040068 public $method = 'index';
Andrey Andreev92ebfb62012-05-17 12:49:24 +030069
David Behler07b53422011-08-15 00:25:06 +020070 /**
71 * Sub-directory that contains the requested controller class
72 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +030073 * @var string
David Behler07b53422011-08-15 00:25:06 +020074 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040075 public $directory = '';
Andrey Andreev92ebfb62012-05-17 12:49:24 +030076
David Behler07b53422011-08-15 00:25:06 +020077 /**
78 * Default controller (and method if specific)
79 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +030080 * @var string
David Behler07b53422011-08-15 00:25:06 +020081 */
Andrey Andreevba6c0412012-01-07 21:10:09 +020082 public $default_controller;
Barry Mienydd671972010-10-04 16:33:58 +020083
Derek Allard2067d1a2008-11-13 22:59:24 +000084 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +030085 * Class constructor
Derek Allard2067d1a2008-11-13 22:59:24 +000086 *
87 * Runs the route mapping function.
Andrey Andreev92ebfb62012-05-17 12:49:24 +030088 *
89 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +000090 */
Andrey Andreevba6c0412012-01-07 21:10:09 +020091 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000092 {
Derek Jonesc7738402010-03-02 13:55:13 -060093 $this->config =& load_class('Config', 'core');
94 $this->uri =& load_class('URI', 'core');
Andrey Andreevba6c0412012-01-07 21:10:09 +020095 log_message('debug', 'Router Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +000096 }
Barry Mienydd671972010-10-04 16:33:58 +020097
Derek Allard2067d1a2008-11-13 22:59:24 +000098 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020099
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300101 * Set route mapping
Derek Allard2067d1a2008-11-13 22:59:24 +0000102 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300103 * Determines what should be served based on the URI request,
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 * as well as any "routes" that have been set in the routing config file.
105 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000106 * @return void
107 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200108 public function _set_routing()
Barry Mienydd671972010-10-04 16:33:58 +0200109 {
Andrey Andreevba6c0412012-01-07 21:10:09 +0200110 // Are query strings enabled in the config file? Normally CI doesn't utilize query strings
Barry Mienydd671972010-10-04 16:33:58 +0200111 // since URI segments are more search-engine friendly, but they can optionally be used.
Derek Jonesc7738402010-03-02 13:55:13 -0600112 // If this feature is enabled, we will gather the directory/class/method a little differently
113 $segments = array();
Andrey Andreev7b53d042012-03-26 23:02:32 +0300114 if ($this->config->item('enable_query_strings') === TRUE && isset($_GET[$this->config->item('controller_trigger')]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 {
Derek Jonesc7738402010-03-02 13:55:13 -0600116 if (isset($_GET[$this->config->item('directory_trigger')]))
117 {
118 $this->set_directory(trim($this->uri->_filter_uri($_GET[$this->config->item('directory_trigger')])));
119 $segments[] = $this->fetch_directory();
120 }
Barry Mienydd671972010-10-04 16:33:58 +0200121
Derek Jonesc7738402010-03-02 13:55:13 -0600122 if (isset($_GET[$this->config->item('controller_trigger')]))
123 {
124 $this->set_class(trim($this->uri->_filter_uri($_GET[$this->config->item('controller_trigger')])));
125 $segments[] = $this->fetch_class();
126 }
Barry Mienydd671972010-10-04 16:33:58 +0200127
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 if (isset($_GET[$this->config->item('function_trigger')]))
129 {
130 $this->set_method(trim($this->uri->_filter_uri($_GET[$this->config->item('function_trigger')])));
Derek Jonesc7738402010-03-02 13:55:13 -0600131 $segments[] = $this->fetch_method();
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 }
Barry Mienydd671972010-10-04 16:33:58 +0200134
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 // Load the routes.php file.
Andrey Andreev7b53d042012-03-26 23:02:32 +0300136 if (defined('ENVIRONMENT') && is_file(APPPATH.'config/'.ENVIRONMENT.'/routes.php'))
Greg Akerd96f8822011-12-27 16:23:47 -0600137 {
138 include(APPPATH.'config/'.ENVIRONMENT.'/routes.php');
139 }
140 elseif (is_file(APPPATH.'config/routes.php'))
141 {
142 include(APPPATH.'config/routes.php');
143 }
David Behler07b53422011-08-15 00:25:06 +0200144
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200145 $this->routes = (isset($route) && is_array($route)) ? $route : array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 unset($route);
Barry Mienydd671972010-10-04 16:33:58 +0200147
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 // Set the default controller so we can display it in the event
149 // the URI doesn't correlated to a valid controller.
Andrey Andreeva3f5c5c2012-11-04 20:08:42 +0200150 $this->default_controller = empty($this->routes['default_controller']) ? FALSE : $this->routes['default_controller'];
Barry Mienydd671972010-10-04 16:33:58 +0200151
Andrey Andreevba6c0412012-01-07 21:10:09 +0200152 // 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 -0600153 if (count($segments) > 0)
154 {
155 return $this->_validate_request($segments);
156 }
Barry Mienydd671972010-10-04 16:33:58 +0200157
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 // Fetch the complete URI string
159 $this->uri->_fetch_uri_string();
Barry Mienydd671972010-10-04 16:33:58 +0200160
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 // 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 +0300162 if ($this->uri->uri_string == '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 {
Derek Jonesc7738402010-03-02 13:55:13 -0600164 return $this->_set_default_controller();
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 }
Barry Mienydd671972010-10-04 16:33:58 +0200166
Andrey Andreevba6c0412012-01-07 21:10:09 +0200167 $this->uri->_remove_url_suffix(); // Remove the URL suffix
168 $this->uri->_explode_segments(); // Compile the segments into an array
169 $this->_parse_routes(); // Parse any custom routing that may exist
170 $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 +0000171 }
Derek Jonesc7738402010-03-02 13:55:13 -0600172
173 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200174
Derek Jonesc7738402010-03-02 13:55:13 -0600175 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300176 * Set default controller
Derek Jonesc7738402010-03-02 13:55:13 -0600177 *
Derek Jonesc7738402010-03-02 13:55:13 -0600178 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200179 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200180 protected function _set_default_controller()
Derek Jonesc7738402010-03-02 13:55:13 -0600181 {
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200182 if (empty($this->default_controller))
Derek Jonesc7738402010-03-02 13:55:13 -0600183 {
Andrey Andreevba6c0412012-01-07 21:10:09 +0200184 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 -0600185 }
Andrey Andreevd1097a12012-11-01 19:55:42 +0200186
Derek Jonesc7738402010-03-02 13:55:13 -0600187 // Is the method being specified?
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200188 if (sscanf($this->default_controller, '%[^/]/%s', $class, $method) !== 2)
Derek Jonesc7738402010-03-02 13:55:13 -0600189 {
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200190 $method = 'index';
Barry Mienydd671972010-10-04 16:33:58 +0200191 }
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200192
193 $this->set_class($class);
194 $this->set_method($method);
195 $this->_set_request(array($class, $method));
Barry Mienydd671972010-10-04 16:33:58 +0200196
Derek Jonesc7738402010-03-02 13:55:13 -0600197 // re-index the routed segments array so it starts with 1 rather than 0
198 $this->uri->_reindex_segments();
Barry Mienydd671972010-10-04 16:33:58 +0200199
Andrey Andreevba6c0412012-01-07 21:10:09 +0200200 log_message('debug', 'No URI present. Default controller set.');
Derek Jonesc7738402010-03-02 13:55:13 -0600201 }
Barry Mienydd671972010-10-04 16:33:58 +0200202
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200204
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300206 * Set request route
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300208 * Takes an array of URI segments as input and sets the class/method
209 * to be called.
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300211 * @param array $segments URI segments
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 * @return void
213 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200214 protected function _set_request($segments = array())
Barry Mienydd671972010-10-04 16:33:58 +0200215 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000216 $segments = $this->_validate_request($segments);
Barry Mienydd671972010-10-04 16:33:58 +0200217
Andrey Andreevba6c0412012-01-07 21:10:09 +0200218 if (count($segments) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 {
Derek Jonesc7738402010-03-02 13:55:13 -0600220 return $this->_set_default_controller();
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 }
Barry Mienydd671972010-10-04 16:33:58 +0200222
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 $this->set_class($segments[0]);
Barry Mienydd671972010-10-04 16:33:58 +0200224
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200225 isset($segments[1]) OR $segments[1] = 'index';
226 $this->set_method($segments[1]);
Barry Mienydd671972010-10-04 16:33:58 +0200227
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 // Update our "routed" segment array to contain the segments.
229 // Note: If there is no custom routing, this array will be
Phil Sturgeon81aa94b2012-05-02 11:40:46 +0100230 // identical to $this->uri->segments
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 $this->uri->rsegments = $segments;
232 }
Barry Mienydd671972010-10-04 16:33:58 +0200233
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200235
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300237 * Validate request
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300239 * Attempts validate the URI request and determine the controller path.
240 *
241 * @param array $segments URI segments
242 * @return array URI segments
Barry Mienydd671972010-10-04 16:33:58 +0200243 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200244 protected function _validate_request($segments)
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 {
Andrey Andreevba6c0412012-01-07 21:10:09 +0200246 if (count($segments) === 0)
Derek Jonesc7738402010-03-02 13:55:13 -0600247 {
248 return $segments;
249 }
Barry Mienydd671972010-10-04 16:33:58 +0200250
Andrey Andreevd1097a12012-11-01 19:55:42 +0200251 $temp = str_replace('-', '_', $segments[0]);
252
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 // Does the requested controller exist in the root folder?
Andrey Andreevd1097a12012-11-01 19:55:42 +0200254 if (file_exists(APPPATH.'controllers/'.$temp.'.php'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 {
Andrey Andreevd1097a12012-11-01 19:55:42 +0200256 $segments[0] = $temp;
257 empty($segments[1]) OR $segments[1] = str_replace('-', '_', $segments[1]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 return $segments;
259 }
Barry Mienydd671972010-10-04 16:33:58 +0200260
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 // Is the controller in a sub-folder?
262 if (is_dir(APPPATH.'controllers/'.$segments[0]))
Derek Jonesc7738402010-03-02 13:55:13 -0600263 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 // Set the directory and remove it from the segment array
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200265 $this->set_directory(array_shift($segments));
Derek Allard2067d1a2008-11-13 22:59:24 +0000266 if (count($segments) > 0)
267 {
Andrey Andreevd1097a12012-11-01 19:55:42 +0200268 $segments[0] = str_replace('-', '_', $segments[0]);
269 empty($segments[1]) OR $segments[1] = str_replace('-', '_', $segments[1]);
270
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 // Does the requested controller exist in the sub-folder?
Greg Aker3a746652011-04-19 10:59:47 -0500272 if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].'.php'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 {
Shane Pearson664a9352011-08-10 16:02:32 -0500274 if ( ! empty($this->routes['404_override']))
275 {
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200276 $this->directory = '';
277 return explode('/', $this->routes['404_override'], 2);
Shane Pearson664a9352011-08-10 16:02:32 -0500278 }
279 else
280 {
281 show_404($this->fetch_directory().$segments[0]);
282 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 }
284 }
285 else
286 {
Derek Jonesc7738402010-03-02 13:55:13 -0600287 // Is the method being specified in the route?
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200288 $segments = explode('/', $this->default_controller);
289 if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].'.php'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 {
291 $this->directory = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 }
Barry Mienydd671972010-10-04 16:33:58 +0200294
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 return $segments;
296 }
Barry Mienydd671972010-10-04 16:33:58 +0200297
Derek Jonesc7738402010-03-02 13:55:13 -0600298 // If we've gotten this far it means that the URI does not correlate to a valid
Andrey Andreevba6c0412012-01-07 21:10:09 +0200299 // controller class. We will now see if there is an override
Eric Barnesc5bf6162011-01-30 21:17:11 -0500300 if ( ! empty($this->routes['404_override']))
Derek Jonesc7738402010-03-02 13:55:13 -0600301 {
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200302 if (sscanf($this->routes['404_override'], '%[^/]/%s', $class, $method) !== 2)
303 {
304 $method = 'index';
305 }
Barry Mienydd671972010-10-04 16:33:58 +0200306
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200307 return array($class, $method);
Derek Jonesc7738402010-03-02 13:55:13 -0600308 }
Barry Mienydd671972010-10-04 16:33:58 +0200309
Derek Jonesc7738402010-03-02 13:55:13 -0600310 // Nothing else to do at this point but show a 404
Barry Mienydd671972010-10-04 16:33:58 +0200311 show_404($segments[0]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 }
Barry Mienydd671972010-10-04 16:33:58 +0200313
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200315
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 /**
Andrey Andreev7b53d042012-03-26 23:02:32 +0300317 * Parse Routes
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300319 * Matches any routes that may exist in the config/routes.php file
320 * against the URI to determine if the class/method need to be remapped.
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 * @return void
323 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200324 protected function _parse_routes()
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100325 {
326 // Turn the segment array into a URI string
327 $uri = implode('/', $this->uri->segments);
Barry Mienydd671972010-10-04 16:33:58 +0200328
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100329 // Is there a literal match? If so we're done
330 if (isset($this->routes[$uri]))
331 {
332 return $this->_set_request(explode('/', $this->routes[$uri]));
333 }
Barry Mienydd671972010-10-04 16:33:58 +0200334
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100335 // Loop through the route array looking for wild-cards
336 foreach ($this->routes as $key => $val)
337 {
338 // Convert wild-cards to RegEx
Andrey Andreev7676c2d2012-10-30 13:42:01 +0200339 $key = str_replace(array(':any', ':num'), array('[^/]+', '[0-9]+'), $key);
Derek Jonesc7738402010-03-02 13:55:13 -0600340
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100341 // Does the RegEx match?
342 if (preg_match('#^'.$key.'$#', $uri, $matches))
343 {
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100344 // Are we using callbacks to process back-references?
Andrey Andreevda5562a2012-11-08 12:34:38 +0200345 if ( ! is_string($val) && is_callable($val))
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100346 {
347 // Remove the original string from the matches array.
348 array_shift($matches);
Derek Allard2067d1a2008-11-13 22:59:24 +0000349
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100350 // Get the match count.
351 $match_count = count($matches);
Jonatas Miguelc0d98b22012-08-06 15:42:50 +0100352
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100353 // Determine how many parameters the callback has.
354 $reflection = new ReflectionFunction($val);
355 $param_count = $reflection->getNumberOfParameters();
Jonatas Miguelc0d98b22012-08-06 15:42:50 +0100356
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100357 // Are there more parameters than matches?
Jonatas Miguel24296ca2012-09-27 12:10:01 +0100358 if ($param_count > $match_count)
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100359 {
360 // Any params without matches will be set to an empty string.
361 $matches = array_merge($matches, array_fill($match_count, $param_count - $match_count, ''));
Jonatas Miguelefb81662012-10-23 19:52:46 +0100362
363 $match_count = $param_count;
364 }
365
366 // Get the parameters so we can use their default values.
367 $params = $reflection->getParameters();
368
369 for ($m = 0; $m < $match_count; $m++)
370 {
371 // Is the match empty and does a default value exist?
372 if (empty($matches[$m]) && $params[$m]->isDefaultValueAvailable())
373 {
374 // Substitute the empty match for the default value.
375 $matches[$m] = $params[$m]->getDefaultValue();
376 }
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100377 }
Jonatas Miguelc0d98b22012-08-06 15:42:50 +0100378
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100379 // Execute the callback using the values in matches as its parameters.
380 $val = call_user_func_array($val, $matches);
381 }
Andrey Andreevda5562a2012-11-08 12:34:38 +0200382 // Are we using the default routing method for back-references?
383 elseif (strpos($val, '$') !== FALSE && strpos($key, '(') !== FALSE)
384 {
385 $val = preg_replace('#^'.$key.'$#', $val, $uri);
386 }
Jonatas Miguelc0d98b22012-08-06 15:42:50 +0100387
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100388 return $this->_set_request(explode('/', $val));
389 }
390 }
Jonatas Miguelc0d98b22012-08-06 15:42:50 +0100391
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100392 // If we got this far it means we didn't encounter a
393 // matching route so we'll set the site default route
394 $this->_set_request($this->uri->segments);
395 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000396
397 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200398
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300400 * Set class name
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300402 * @param string $class Class name
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200404 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200405 public function set_class($class)
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 {
Derek Jones2615e412010-10-06 17:51:16 -0500407 $this->class = str_replace(array('/', '.'), '', $class);
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 }
Barry Mienydd671972010-10-04 16:33:58 +0200409
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200411
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 /**
413 * Fetch the current class
414 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200416 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200417 public function fetch_class()
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 {
419 return $this->class;
420 }
Barry Mienydd671972010-10-04 16:33:58 +0200421
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200423
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300425 * Set method name
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300427 * @param string $method Method name
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200429 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200430 public function set_method($method)
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 {
432 $this->method = $method;
433 }
434
435 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200436
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 /**
Andrey Andreev7b53d042012-03-26 23:02:32 +0300438 * Fetch the current method
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200441 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200442 public function fetch_method()
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 {
Alex Bilbieed944a32012-06-02 11:07:47 +0100444 return ($this->method === $this->fetch_class()) ? 'index' : $this->method;
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 }
446
447 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200448
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300450 * Set directory name
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300452 * @param string $dir Directory name
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200454 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200455 public function set_directory($dir)
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 {
Derek Jones2615e412010-10-06 17:51:16 -0500457 $this->directory = str_replace(array('/', '.'), '', $dir).'/';
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 }
459
460 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200461
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300463 * Fetch directory
464 *
465 * Feches the sub-directory (if any) that contains the requested
466 * controller class.
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200469 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200470 public function fetch_directory()
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 {
472 return $this->directory;
473 }
474
Derek Jonesc7738402010-03-02 13:55:13 -0600475 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200476
Derek Jonesc7738402010-03-02 13:55:13 -0600477 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300478 * Set controller overrides
Derek Jonesc7738402010-03-02 13:55:13 -0600479 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300480 * @param array $routing Route overrides
Andrey Andreev7b53d042012-03-26 23:02:32 +0300481 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200482 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200483 public function _set_overrides($routing)
Derek Jonesc7738402010-03-02 13:55:13 -0600484 {
485 if ( ! is_array($routing))
486 {
487 return;
488 }
Barry Mienydd671972010-10-04 16:33:58 +0200489
Derek Jonesc7738402010-03-02 13:55:13 -0600490 if (isset($routing['directory']))
491 {
492 $this->set_directory($routing['directory']);
493 }
Barry Mienydd671972010-10-04 16:33:58 +0200494
Andrey Andreev9ba661b2012-06-04 14:44:34 +0300495 if ( ! empty($routing['controller']))
Derek Jonesc7738402010-03-02 13:55:13 -0600496 {
497 $this->set_class($routing['controller']);
498 }
Barry Mienydd671972010-10-04 16:33:58 +0200499
Derek Jonesc7738402010-03-02 13:55:13 -0600500 if (isset($routing['function']))
501 {
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200502 $routing['function'] = empty($routing['function']) ? 'index' : $routing['function'];
Derek Jonesc7738402010-03-02 13:55:13 -0600503 $this->set_method($routing['function']);
504 }
505 }
506
Derek Allard2067d1a2008-11-13 22:59:24 +0000507}
Derek Allard2067d1a2008-11-13 22:59:24 +0000508
509/* End of file Router.php */
Andrey Andreev7b53d042012-03-26 23:02:32 +0300510/* Location: ./system/core/Router.php */