blob: a84be1f1d2c8746be6b7a2d0ef1f4a40728c6b1c [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02005 * An open source application development framework for PHP
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Andrey Andreevba6c0412012-01-07 21:10:09 +02008 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02009 * Copyright (c) 2014 - 2015, British Columbia Institute of Technology
Andrey Andreevba6c0412012-01-07 21:10:09 +020010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
Derek Jonesf4a4bd82011-10-20 12:18:42 -050017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 *
29 * @package CodeIgniter
30 * @author EllisLab Dev Team
darwinel871754a2014-02-11 17:34:57 +010031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Andrey Andreevfe9309d2015-01-09 17:48:58 +020032 * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
34 * @link http://codeigniter.com
35 * @since Version 1.0.0
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000039
Derek Allard2067d1a2008-11-13 22:59:24 +000040/**
41 * Router Class
42 *
43 * Parses URIs and determines routing
44 *
45 * @package CodeIgniter
46 * @subpackage Libraries
Derek Allard2067d1a2008-11-13 22:59:24 +000047 * @category Libraries
Andrey Andreev92ebfb62012-05-17 12:49:24 +030048 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000049 * @link http://codeigniter.com/user_guide/general/routing.html
50 */
51class CI_Router {
52
David Behler07b53422011-08-15 00:25:06 +020053 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +030054 * CI_Config class object
David Behler07b53422011-08-15 00:25:06 +020055 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +030056 * @var object
David Behler07b53422011-08-15 00:25:06 +020057 */
Andrey Andreevba6c0412012-01-07 21:10:09 +020058 public $config;
Andrey Andreev92ebfb62012-05-17 12:49:24 +030059
David Behler07b53422011-08-15 00:25:06 +020060 /**
61 * List of routes
62 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +030063 * @var array
David Behler07b53422011-08-15 00:25:06 +020064 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040065 public $routes = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +030066
David Behler07b53422011-08-15 00:25:06 +020067 /**
David Behler07b53422011-08-15 00:25:06 +020068 * Current class name
69 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +030070 * @var string
David Behler07b53422011-08-15 00:25:06 +020071 */
Andrey Andreev92ebfb62012-05-17 12:49:24 +030072 public $class = '';
73
David Behler07b53422011-08-15 00:25:06 +020074 /**
75 * Current method name
76 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +030077 * @var string
David Behler07b53422011-08-15 00:25:06 +020078 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040079 public $method = 'index';
Andrey Andreev92ebfb62012-05-17 12:49:24 +030080
David Behler07b53422011-08-15 00:25:06 +020081 /**
82 * Sub-directory that contains the requested controller class
83 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +030084 * @var string
David Behler07b53422011-08-15 00:25:06 +020085 */
Andrey Andreev2ef5ed42015-07-17 14:24:26 +030086 public $directory;
Andrey Andreev92ebfb62012-05-17 12:49:24 +030087
David Behler07b53422011-08-15 00:25:06 +020088 /**
89 * Default controller (and method if specific)
90 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +030091 * @var string
David Behler07b53422011-08-15 00:25:06 +020092 */
Andrey Andreevba6c0412012-01-07 21:10:09 +020093 public $default_controller;
Barry Mienydd671972010-10-04 16:33:58 +020094
Derek Allard2067d1a2008-11-13 22:59:24 +000095 /**
Andrey Andreev08fec7b2013-07-19 16:25:51 +030096 * Translate URI dashes
97 *
98 * Determines whether dashes in controller & method segments
99 * should be automatically replaced by underscores.
100 *
101 * @var bool
102 */
103 public $translate_uri_dashes = FALSE;
104
Andrey Andreev30d53242014-01-16 14:41:46 +0200105 /**
106 * Enable query strings flag
107 *
ftwbzhaobf0488b2015-07-06 17:48:08 +0800108 * Determines whether to use GET parameters or segment URIs
Andrey Andreev30d53242014-01-16 14:41:46 +0200109 *
110 * @var bool
111 */
112 public $enable_query_strings = FALSE;
113
Andrey Andreev08fec7b2013-07-19 16:25:51 +0300114 // --------------------------------------------------------------------
115
116 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300117 * Class constructor
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 *
119 * Runs the route mapping function.
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300120 *
121 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 */
Andrey Andreevc26b9eb2014-02-24 11:31:36 +0200123 public function __construct($routing = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 {
Derek Jonesc7738402010-03-02 13:55:13 -0600125 $this->config =& load_class('Config', 'core');
126 $this->uri =& load_class('URI', 'core');
Andrey Andreev30d53242014-01-16 14:41:46 +0200127
128 $this->enable_query_strings = ( ! is_cli() && $this->config->item('enable_query_strings') === TRUE);
Andrey Andreev2ef5ed42015-07-17 14:24:26 +0300129
130 // If a directory override is configured, it has to be set before any dynamic routing logic
131 is_array($routing) && isset($routing['directory']) && $this->set_directory($routing['directory']);
Andrey Andreev08fec7b2013-07-19 16:25:51 +0300132 $this->_set_routing();
Andrey Andreev4e6c5282014-01-10 19:29:49 +0200133
134 // Set any routing overrides that may exist in the main index file
Andrey Andreevc26b9eb2014-02-24 11:31:36 +0200135 if (is_array($routing))
Andrey Andreev4e6c5282014-01-10 19:29:49 +0200136 {
Andrey Andreev2ef5ed42015-07-17 14:24:26 +0300137 empty($routing['controller']) OR $this->set_class($routing['controller']);
138 empty($routing['function']) OR $this->set_method($routing['function']);
Andrey Andreev4e6c5282014-01-10 19:29:49 +0200139 }
140
Andrey Andreev90726b82015-01-20 12:39:22 +0200141 log_message('info', 'Router Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 }
Barry Mienydd671972010-10-04 16:33:58 +0200143
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200145
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300147 * Set route mapping
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300149 * Determines what should be served based on the URI request,
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 * as well as any "routes" that have been set in the routing config file.
151 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 * @return void
153 */
Andrey Andreev08fec7b2013-07-19 16:25:51 +0300154 protected function _set_routing()
Barry Mienydd671972010-10-04 16:33:58 +0200155 {
Andrey Andreevf2239fe2015-09-14 13:48:03 +0300156 // Load the routes.php file. It would be great if we could
157 // skip this for enable_query_strings = TRUE, but then
158 // default_controller would be empty ...
159 if (file_exists(APPPATH.'config/routes.php'))
160 {
161 include(APPPATH.'config/routes.php');
162 }
163
164 if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/routes.php'))
165 {
166 include(APPPATH.'config/'.ENVIRONMENT.'/routes.php');
167 }
168
169 // Validate & get reserved routes
170 if (isset($route) && is_array($route))
171 {
172 isset($route['default_controller']) && $this->default_controller = $route['default_controller'];
173 isset($route['translate_uri_dashes']) && $this->translate_uri_dashes = $route['translate_uri_dashes'];
174 unset($route['default_controller'], $route['translate_uri_dashes']);
175 $this->routes = $route;
176 }
177
Andrey Andreevba6c0412012-01-07 21:10:09 +0200178 // Are query strings enabled in the config file? Normally CI doesn't utilize query strings
Barry Mienydd671972010-10-04 16:33:58 +0200179 // since URI segments are more search-engine friendly, but they can optionally be used.
Derek Jonesc7738402010-03-02 13:55:13 -0600180 // If this feature is enabled, we will gather the directory/class/method a little differently
Andrey Andreev30d53242014-01-16 14:41:46 +0200181 if ($this->enable_query_strings)
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 {
Andrey Andreev2ef5ed42015-07-17 14:24:26 +0300183 // If the directory is set at this time, it means an override exists, so skip the checks
184 if ( ! isset($this->directory))
Derek Jonesc7738402010-03-02 13:55:13 -0600185 {
Andrey Andreev2ef5ed42015-07-17 14:24:26 +0300186 $_d = $this->config->item('directory_trigger');
187 $_d = isset($_GET[$_d]) ? trim($_GET[$_d], " \t\n\r\0\x0B/") : '';
188
189 if ($_d !== '')
190 {
191 $this->uri->filter_uri($_d);
192 $this->set_directory($_d);
193 }
Derek Jonesc7738402010-03-02 13:55:13 -0600194 }
Barry Mienydd671972010-10-04 16:33:58 +0200195
Andrey Andreevbfa233f2014-12-05 12:00:11 +0200196 $_c = trim($this->config->item('controller_trigger'));
Andrey Andreev30d53242014-01-16 14:41:46 +0200197 if ( ! empty($_GET[$_c]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 {
Andrey Andreevbfa233f2014-12-05 12:00:11 +0200199 $this->uri->filter_uri($_GET[$_c]);
200 $this->set_class($_GET[$_c]);
Andrey Andreev30d53242014-01-16 14:41:46 +0200201
Andrey Andreevbfa233f2014-12-05 12:00:11 +0200202 $_f = trim($this->config->item('function_trigger'));
Andrey Andreev30d53242014-01-16 14:41:46 +0200203 if ( ! empty($_GET[$_f]))
204 {
Andrey Andreevbfa233f2014-12-05 12:00:11 +0200205 $this->uri->filter_uri($_GET[$_f]);
206 $this->set_method($_GET[$_f]);
Andrey Andreev30d53242014-01-16 14:41:46 +0200207 }
208
209 $this->uri->rsegments = array(
210 1 => $this->class,
211 2 => $this->method
212 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 }
Andrey Andreev30d53242014-01-16 14:41:46 +0200214 else
215 {
216 $this->_set_default_controller();
217 }
218
219 // Routing rules don't apply to query strings and we don't need to detect
220 // directories, so we're done here
221 return;
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 }
Barry Mienydd671972010-10-04 16:33:58 +0200223
Andrey Andreev30d53242014-01-16 14:41:46 +0200224 // Is there anything to parse?
225 if ($this->uri->uri_string !== '')
Derek Jonesc7738402010-03-02 13:55:13 -0600226 {
Andrey Andreev30d53242014-01-16 14:41:46 +0200227 $this->_parse_routes();
228 }
229 else
230 {
231 $this->_set_default_controller();
232 }
233 }
234
235 // --------------------------------------------------------------------
236
237 /**
238 * Set request route
239 *
240 * Takes an array of URI segments as input and sets the class/method
241 * to be called.
242 *
243 * @used-by CI_Router::_parse_routes()
244 * @param array $segments URI segments
245 * @return void
246 */
247 protected function _set_request($segments = array())
248 {
249 $segments = $this->_validate_request($segments);
250 // If we don't have any segments left - try the default controller;
251 // WARNING: Directories get shifted out of the segments array!
252 if (empty($segments))
253 {
254 $this->_set_default_controller();
255 return;
Derek Jonesc7738402010-03-02 13:55:13 -0600256 }
Barry Mienydd671972010-10-04 16:33:58 +0200257
Andrey Andreev30d53242014-01-16 14:41:46 +0200258 if ($this->translate_uri_dashes === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 {
Andrey Andreev30d53242014-01-16 14:41:46 +0200260 $segments[0] = str_replace('-', '_', $segments[0]);
261 if (isset($segments[1]))
262 {
263 $segments[1] = str_replace('-', '_', $segments[1]);
264 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 }
Barry Mienydd671972010-10-04 16:33:58 +0200266
Andrey Andreev30d53242014-01-16 14:41:46 +0200267 $this->set_class($segments[0]);
268 if (isset($segments[1]))
269 {
270 $this->set_method($segments[1]);
271 }
Andrey Andreev9cab4272014-03-30 18:58:23 +0300272 else
273 {
274 $segments[1] = 'index';
275 }
Andrey Andreev30d53242014-01-16 14:41:46 +0200276
277 array_unshift($segments, NULL);
278 unset($segments[0]);
279 $this->uri->rsegments = $segments;
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 }
Derek Jonesc7738402010-03-02 13:55:13 -0600281
282 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200283
Derek Jonesc7738402010-03-02 13:55:13 -0600284 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300285 * Set default controller
Derek Jonesc7738402010-03-02 13:55:13 -0600286 *
Derek Jonesc7738402010-03-02 13:55:13 -0600287 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200288 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200289 protected function _set_default_controller()
Derek Jonesc7738402010-03-02 13:55:13 -0600290 {
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200291 if (empty($this->default_controller))
Derek Jonesc7738402010-03-02 13:55:13 -0600292 {
Andrey Andreevba6c0412012-01-07 21:10:09 +0200293 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 -0600294 }
Andrey Andreevd1097a12012-11-01 19:55:42 +0200295
Derek Jonesc7738402010-03-02 13:55:13 -0600296 // Is the method being specified?
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200297 if (sscanf($this->default_controller, '%[^/]/%s', $class, $method) !== 2)
Derek Jonesc7738402010-03-02 13:55:13 -0600298 {
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200299 $method = 'index';
Barry Mienydd671972010-10-04 16:33:58 +0200300 }
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200301
Andrey Andreev30d53242014-01-16 14:41:46 +0200302 if ( ! file_exists(APPPATH.'controllers/'.$this->directory.ucfirst($class).'.php'))
303 {
304 // This will trigger 404 later
305 return;
306 }
Barry Mienydd671972010-10-04 16:33:58 +0200307
Andrey Andreev30d53242014-01-16 14:41:46 +0200308 $this->set_class($class);
309 $this->set_method($method);
310
311 // Assign routed segments, index starting from 1
312 $this->uri->rsegments = array(
313 1 => $class,
314 2 => $method
315 );
Barry Mienydd671972010-10-04 16:33:58 +0200316
Andrey Andreevba6c0412012-01-07 21:10:09 +0200317 log_message('debug', 'No URI present. Default controller set.');
Derek Jonesc7738402010-03-02 13:55:13 -0600318 }
Barry Mienydd671972010-10-04 16:33:58 +0200319
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200321
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300323 * Validate request
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300325 * Attempts validate the URI request and determine the controller path.
326 *
Andrey Andreev30d53242014-01-16 14:41:46 +0200327 * @used-by CI_Router::_set_request()
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300328 * @param array $segments URI segments
Andrey Andreev30d53242014-01-16 14:41:46 +0200329 * @return mixed URI segments
Barry Mienydd671972010-10-04 16:33:58 +0200330 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200331 protected function _validate_request($segments)
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 {
Andrey Andreev30d53242014-01-16 14:41:46 +0200333 $c = count($segments);
Andrey Andreev2ef5ed42015-07-17 14:24:26 +0300334 $directory_override = isset($this->directory);
335
Andrey Andreev30d53242014-01-16 14:41:46 +0200336 // Loop through our segments and return as soon as a controller
337 // is found or when such a directory doesn't exist
338 while ($c-- > 0)
Derek Jonesc7738402010-03-02 13:55:13 -0600339 {
Andrey Andreev30d53242014-01-16 14:41:46 +0200340 $test = $this->directory
341 .ucfirst($this->translate_uri_dashes === TRUE ? str_replace('-', '_', $segments[0]) : $segments[0]);
Barry Mienydd671972010-10-04 16:33:58 +0200342
Andrey Andreev2ef5ed42015-07-17 14:24:26 +0300343 if ( ! file_exists(APPPATH.'controllers/'.$test.'.php')
344 && $directory_override === FALSE
345 && is_dir(APPPATH.'controllers/'.$this->directory.$segments[0])
346 )
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 {
Andrey Andreev30d53242014-01-16 14:41:46 +0200348 $this->set_directory(array_shift($segments), TRUE);
349 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 }
Barry Mienydd671972010-10-04 16:33:58 +0200351
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 return $segments;
353 }
Barry Mienydd671972010-10-04 16:33:58 +0200354
Andrey Andreev30d53242014-01-16 14:41:46 +0200355 // This means that all segments were actually directories
356 return $segments;
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 }
Barry Mienydd671972010-10-04 16:33:58 +0200358
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200360
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 /**
Andrey Andreev7b53d042012-03-26 23:02:32 +0300362 * Parse Routes
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300364 * Matches any routes that may exist in the config/routes.php file
365 * against the URI to determine if the class/method need to be remapped.
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 * @return void
368 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200369 protected function _parse_routes()
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100370 {
371 // Turn the segment array into a URI string
372 $uri = implode('/', $this->uri->segments);
Barry Mienydd671972010-10-04 16:33:58 +0200373
Fatih Kalifa442b50b2013-11-01 02:44:56 +0700374 // Get HTTP verb
Andrey Andreevc761a202013-11-11 14:02:15 +0200375 $http_verb = isset($_SERVER['REQUEST_METHOD']) ? strtolower($_SERVER['REQUEST_METHOD']) : 'cli';
Fatih Kalifa442b50b2013-11-01 02:44:56 +0700376
vlakoffc941d852013-08-06 14:44:40 +0200377 // Loop through the route array looking for wildcards
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100378 foreach ($this->routes as $key => $val)
379 {
Andrey Andreevabc299b2015-08-05 12:26:22 +0300380 // Check if route format is using HTTP verbs
Fatih Kalifa442b50b2013-11-01 02:44:56 +0700381 if (is_array($val))
382 {
Andrey Andreevabc299b2015-08-05 12:26:22 +0300383 $val = array_change_key_case($val, CASE_LOWER);
Fatih Kalifa442b50b2013-11-01 02:44:56 +0700384 if (isset($val[$http_verb]))
385 {
386 $val = $val[$http_verb];
387 }
Fatih Kalifa442b50b2013-11-01 02:44:56 +0700388 else
389 {
Fatih Kalifa442b50b2013-11-01 02:44:56 +0700390 continue;
391 }
392 }
393
vlakoffc941d852013-08-06 14:44:40 +0200394 // Convert wildcards to RegEx
Andrey Andreev7676c2d2012-10-30 13:42:01 +0200395 $key = str_replace(array(':any', ':num'), array('[^/]+', '[0-9]+'), $key);
Derek Jonesc7738402010-03-02 13:55:13 -0600396
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100397 // Does the RegEx match?
398 if (preg_match('#^'.$key.'$#', $uri, $matches))
399 {
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100400 // Are we using callbacks to process back-references?
Andrey Andreevda5562a2012-11-08 12:34:38 +0200401 if ( ! is_string($val) && is_callable($val))
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100402 {
403 // Remove the original string from the matches array.
404 array_shift($matches);
Derek Allard2067d1a2008-11-13 22:59:24 +0000405
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100406 // Execute the callback using the values in matches as its parameters.
407 $val = call_user_func_array($val, $matches);
408 }
Andrey Andreevda5562a2012-11-08 12:34:38 +0200409 // Are we using the default routing method for back-references?
410 elseif (strpos($val, '$') !== FALSE && strpos($key, '(') !== FALSE)
411 {
412 $val = preg_replace('#^'.$key.'$#', $val, $uri);
413 }
Jonatas Miguelc0d98b22012-08-06 15:42:50 +0100414
Andrey Andreev30d53242014-01-16 14:41:46 +0200415 $this->_set_request(explode('/', $val));
416 return;
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100417 }
418 }
Jonatas Miguelc0d98b22012-08-06 15:42:50 +0100419
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100420 // If we got this far it means we didn't encounter a
421 // matching route so we'll set the site default route
Andrey Andreeva9237cb2014-01-18 19:07:20 +0200422 $this->_set_request(array_values($this->uri->segments));
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100423 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000424
425 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200426
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300428 * Set class name
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300430 * @param string $class Class name
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200432 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200433 public function set_class($class)
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 {
Derek Jones2615e412010-10-06 17:51:16 -0500435 $this->class = str_replace(array('/', '.'), '', $class);
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 }
Barry Mienydd671972010-10-04 16:33:58 +0200437
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200439
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 /**
441 * Fetch the current class
442 *
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300443 * @deprecated 3.0.0 Read the 'class' property instead
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200445 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200446 public function fetch_class()
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 {
448 return $this->class;
449 }
Barry Mienydd671972010-10-04 16:33:58 +0200450
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200452
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300454 * Set method name
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300456 * @param string $method Method name
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200458 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200459 public function set_method($method)
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 {
461 $this->method = $method;
462 }
463
464 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200465
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 /**
Andrey Andreev7b53d042012-03-26 23:02:32 +0300467 * Fetch the current method
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 *
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300469 * @deprecated 3.0.0 Read the 'method' property instead
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200471 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200472 public function fetch_method()
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 {
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300474 return $this->method;
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 }
476
477 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200478
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300480 * Set directory name
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300482 * @param string $dir Directory name
Calvin Tam55bc5052015-07-24 02:27:24 -0700483 * @param bool $append Whether we're appending rather than setting the full value
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200485 */
Andrey Andreev30d53242014-01-16 14:41:46 +0200486 public function set_directory($dir, $append = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 {
Andrey Andreev30d53242014-01-16 14:41:46 +0200488 if ($append !== TRUE OR empty($this->directory))
489 {
490 $this->directory = str_replace('.', '', trim($dir, '/')).'/';
491 }
492 else
493 {
494 $this->directory .= str_replace('.', '', trim($dir, '/')).'/';
495 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 }
497
498 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200499
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300501 * Fetch directory
502 *
503 * Feches the sub-directory (if any) that contains the requested
504 * controller class.
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 *
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300506 * @deprecated 3.0.0 Read the 'directory' property instead
Derek Allard2067d1a2008-11-13 22:59:24 +0000507 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200508 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200509 public function fetch_directory()
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 {
511 return $this->directory;
512 }
513
514}