blob: 7b92f70b0381278daf23728a246d05c040d2cfc9 [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 Andreevcce6bd12018-01-09 11:32:02 +02009 * Copyright (c) 2014 - 2018, 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
Andrey Andreev1924e872016-01-11 12:55:34 +020031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
Andrey Andreevcce6bd12018-01-09 11:32:02 +020032 * @copyright Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
Andrey Andreevbd202c92016-01-11 12:50:18 +020034 * @link https://codeigniter.com
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020035 * @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
Andrey Andreevbd202c92016-01-11 12:50:18 +020049 * @link https://codeigniter.com/user_guide/general/routing.html
Derek Allard2067d1a2008-11-13 22:59:24 +000050 */
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 *
Andrey Andreevf0f93f52015-12-07 12:12:44 +0200121 * @param array $routing
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300122 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 */
Andrey Andreevc26b9eb2014-02-24 11:31:36 +0200124 public function __construct($routing = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 {
Derek Jonesc7738402010-03-02 13:55:13 -0600126 $this->config =& load_class('Config', 'core');
127 $this->uri =& load_class('URI', 'core');
Andrey Andreev30d53242014-01-16 14:41:46 +0200128
129 $this->enable_query_strings = ( ! is_cli() && $this->config->item('enable_query_strings') === TRUE);
Andrey Andreev2ef5ed42015-07-17 14:24:26 +0300130
131 // If a directory override is configured, it has to be set before any dynamic routing logic
132 is_array($routing) && isset($routing['directory']) && $this->set_directory($routing['directory']);
Andrey Andreev08fec7b2013-07-19 16:25:51 +0300133 $this->_set_routing();
Andrey Andreev4e6c5282014-01-10 19:29:49 +0200134
135 // Set any routing overrides that may exist in the main index file
Andrey Andreevc26b9eb2014-02-24 11:31:36 +0200136 if (is_array($routing))
Andrey Andreev4e6c5282014-01-10 19:29:49 +0200137 {
Andrey Andreev2ef5ed42015-07-17 14:24:26 +0300138 empty($routing['controller']) OR $this->set_class($routing['controller']);
139 empty($routing['function']) OR $this->set_method($routing['function']);
Andrey Andreev4e6c5282014-01-10 19:29:49 +0200140 }
141
Andrey Andreev90726b82015-01-20 12:39:22 +0200142 log_message('info', 'Router Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 }
Barry Mienydd671972010-10-04 16:33:58 +0200144
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200146
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300148 * Set route mapping
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300150 * Determines what should be served based on the URI request,
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 * as well as any "routes" that have been set in the routing config file.
152 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000153 * @return void
154 */
Andrey Andreev08fec7b2013-07-19 16:25:51 +0300155 protected function _set_routing()
Barry Mienydd671972010-10-04 16:33:58 +0200156 {
Andrey Andreevf2239fe2015-09-14 13:48:03 +0300157 // Load the routes.php file. It would be great if we could
158 // skip this for enable_query_strings = TRUE, but then
159 // default_controller would be empty ...
160 if (file_exists(APPPATH.'config/routes.php'))
161 {
162 include(APPPATH.'config/routes.php');
163 }
164
165 if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/routes.php'))
166 {
167 include(APPPATH.'config/'.ENVIRONMENT.'/routes.php');
168 }
169
170 // Validate & get reserved routes
171 if (isset($route) && is_array($route))
172 {
173 isset($route['default_controller']) && $this->default_controller = $route['default_controller'];
174 isset($route['translate_uri_dashes']) && $this->translate_uri_dashes = $route['translate_uri_dashes'];
175 unset($route['default_controller'], $route['translate_uri_dashes']);
176 $this->routes = $route;
177 }
178
Andrey Andreevba6c0412012-01-07 21:10:09 +0200179 // Are query strings enabled in the config file? Normally CI doesn't utilize query strings
Barry Mienydd671972010-10-04 16:33:58 +0200180 // since URI segments are more search-engine friendly, but they can optionally be used.
Derek Jonesc7738402010-03-02 13:55:13 -0600181 // If this feature is enabled, we will gather the directory/class/method a little differently
Andrey Andreev30d53242014-01-16 14:41:46 +0200182 if ($this->enable_query_strings)
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 {
Andrey Andreev2ef5ed42015-07-17 14:24:26 +0300184 // If the directory is set at this time, it means an override exists, so skip the checks
185 if ( ! isset($this->directory))
Derek Jonesc7738402010-03-02 13:55:13 -0600186 {
Andrey Andreev2ef5ed42015-07-17 14:24:26 +0300187 $_d = $this->config->item('directory_trigger');
188 $_d = isset($_GET[$_d]) ? trim($_GET[$_d], " \t\n\r\0\x0B/") : '';
189
190 if ($_d !== '')
191 {
192 $this->uri->filter_uri($_d);
193 $this->set_directory($_d);
194 }
Derek Jonesc7738402010-03-02 13:55:13 -0600195 }
Barry Mienydd671972010-10-04 16:33:58 +0200196
Andrey Andreevbfa233f2014-12-05 12:00:11 +0200197 $_c = trim($this->config->item('controller_trigger'));
Andrey Andreev30d53242014-01-16 14:41:46 +0200198 if ( ! empty($_GET[$_c]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 {
Andrey Andreevbfa233f2014-12-05 12:00:11 +0200200 $this->uri->filter_uri($_GET[$_c]);
201 $this->set_class($_GET[$_c]);
Andrey Andreev30d53242014-01-16 14:41:46 +0200202
Andrey Andreevbfa233f2014-12-05 12:00:11 +0200203 $_f = trim($this->config->item('function_trigger'));
Andrey Andreev30d53242014-01-16 14:41:46 +0200204 if ( ! empty($_GET[$_f]))
205 {
Andrey Andreevbfa233f2014-12-05 12:00:11 +0200206 $this->uri->filter_uri($_GET[$_f]);
207 $this->set_method($_GET[$_f]);
Andrey Andreev30d53242014-01-16 14:41:46 +0200208 }
209
210 $this->uri->rsegments = array(
211 1 => $this->class,
212 2 => $this->method
213 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 }
Andrey Andreev30d53242014-01-16 14:41:46 +0200215 else
216 {
217 $this->_set_default_controller();
218 }
219
220 // Routing rules don't apply to query strings and we don't need to detect
221 // directories, so we're done here
222 return;
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 }
Barry Mienydd671972010-10-04 16:33:58 +0200224
Andrey Andreev30d53242014-01-16 14:41:46 +0200225 // Is there anything to parse?
226 if ($this->uri->uri_string !== '')
Derek Jonesc7738402010-03-02 13:55:13 -0600227 {
Andrey Andreev30d53242014-01-16 14:41:46 +0200228 $this->_parse_routes();
229 }
230 else
231 {
232 $this->_set_default_controller();
233 }
234 }
235
236 // --------------------------------------------------------------------
237
238 /**
239 * Set request route
240 *
241 * Takes an array of URI segments as input and sets the class/method
242 * to be called.
243 *
244 * @used-by CI_Router::_parse_routes()
245 * @param array $segments URI segments
246 * @return void
247 */
248 protected function _set_request($segments = array())
249 {
250 $segments = $this->_validate_request($segments);
251 // If we don't have any segments left - try the default controller;
252 // WARNING: Directories get shifted out of the segments array!
253 if (empty($segments))
254 {
255 $this->_set_default_controller();
256 return;
Derek Jonesc7738402010-03-02 13:55:13 -0600257 }
Barry Mienydd671972010-10-04 16:33:58 +0200258
Andrey Andreev30d53242014-01-16 14:41:46 +0200259 if ($this->translate_uri_dashes === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 {
Andrey Andreev30d53242014-01-16 14:41:46 +0200261 $segments[0] = str_replace('-', '_', $segments[0]);
262 if (isset($segments[1]))
263 {
264 $segments[1] = str_replace('-', '_', $segments[1]);
265 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000266 }
Barry Mienydd671972010-10-04 16:33:58 +0200267
Andrey Andreev30d53242014-01-16 14:41:46 +0200268 $this->set_class($segments[0]);
269 if (isset($segments[1]))
270 {
271 $this->set_method($segments[1]);
272 }
Andrey Andreev9cab4272014-03-30 18:58:23 +0300273 else
274 {
275 $segments[1] = 'index';
276 }
Andrey Andreev30d53242014-01-16 14:41:46 +0200277
278 array_unshift($segments, NULL);
279 unset($segments[0]);
280 $this->uri->rsegments = $segments;
Derek Allard2067d1a2008-11-13 22:59:24 +0000281 }
Derek Jonesc7738402010-03-02 13:55:13 -0600282
283 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200284
Derek Jonesc7738402010-03-02 13:55:13 -0600285 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300286 * Set default controller
Derek Jonesc7738402010-03-02 13:55:13 -0600287 *
Derek Jonesc7738402010-03-02 13:55:13 -0600288 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200289 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200290 protected function _set_default_controller()
Derek Jonesc7738402010-03-02 13:55:13 -0600291 {
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200292 if (empty($this->default_controller))
Derek Jonesc7738402010-03-02 13:55:13 -0600293 {
Andrey Andreevba6c0412012-01-07 21:10:09 +0200294 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 -0600295 }
Andrey Andreevd1097a12012-11-01 19:55:42 +0200296
Derek Jonesc7738402010-03-02 13:55:13 -0600297 // Is the method being specified?
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200298 if (sscanf($this->default_controller, '%[^/]/%s', $class, $method) !== 2)
Derek Jonesc7738402010-03-02 13:55:13 -0600299 {
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200300 $method = 'index';
Barry Mienydd671972010-10-04 16:33:58 +0200301 }
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200302
Andrey Andreev30d53242014-01-16 14:41:46 +0200303 if ( ! file_exists(APPPATH.'controllers/'.$this->directory.ucfirst($class).'.php'))
304 {
305 // This will trigger 404 later
306 return;
307 }
Barry Mienydd671972010-10-04 16:33:58 +0200308
Andrey Andreev30d53242014-01-16 14:41:46 +0200309 $this->set_class($class);
310 $this->set_method($method);
311
312 // Assign routed segments, index starting from 1
313 $this->uri->rsegments = array(
314 1 => $class,
315 2 => $method
316 );
Barry Mienydd671972010-10-04 16:33:58 +0200317
Andrey Andreevba6c0412012-01-07 21:10:09 +0200318 log_message('debug', 'No URI present. Default controller set.');
Derek Jonesc7738402010-03-02 13:55:13 -0600319 }
Barry Mienydd671972010-10-04 16:33:58 +0200320
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200322
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300324 * Validate request
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300326 * Attempts validate the URI request and determine the controller path.
327 *
Andrey Andreev30d53242014-01-16 14:41:46 +0200328 * @used-by CI_Router::_set_request()
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300329 * @param array $segments URI segments
Andrey Andreev30d53242014-01-16 14:41:46 +0200330 * @return mixed URI segments
Barry Mienydd671972010-10-04 16:33:58 +0200331 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200332 protected function _validate_request($segments)
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 {
Andrey Andreev30d53242014-01-16 14:41:46 +0200334 $c = count($segments);
Andrey Andreev2ef5ed42015-07-17 14:24:26 +0300335 $directory_override = isset($this->directory);
336
Andrey Andreev30d53242014-01-16 14:41:46 +0200337 // Loop through our segments and return as soon as a controller
338 // is found or when such a directory doesn't exist
339 while ($c-- > 0)
Derek Jonesc7738402010-03-02 13:55:13 -0600340 {
Andrey Andreev30d53242014-01-16 14:41:46 +0200341 $test = $this->directory
342 .ucfirst($this->translate_uri_dashes === TRUE ? str_replace('-', '_', $segments[0]) : $segments[0]);
Barry Mienydd671972010-10-04 16:33:58 +0200343
Andrey Andreev2ef5ed42015-07-17 14:24:26 +0300344 if ( ! file_exists(APPPATH.'controllers/'.$test.'.php')
345 && $directory_override === FALSE
346 && is_dir(APPPATH.'controllers/'.$this->directory.$segments[0])
347 )
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 {
Andrey Andreev30d53242014-01-16 14:41:46 +0200349 $this->set_directory(array_shift($segments), TRUE);
350 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 }
Barry Mienydd671972010-10-04 16:33:58 +0200352
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 return $segments;
354 }
Barry Mienydd671972010-10-04 16:33:58 +0200355
Andrey Andreev30d53242014-01-16 14:41:46 +0200356 // This means that all segments were actually directories
357 return $segments;
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 }
Barry Mienydd671972010-10-04 16:33:58 +0200359
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200361
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 /**
Andrey Andreev7b53d042012-03-26 23:02:32 +0300363 * Parse Routes
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300365 * Matches any routes that may exist in the config/routes.php file
366 * against the URI to determine if the class/method need to be remapped.
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 * @return void
369 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200370 protected function _parse_routes()
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100371 {
372 // Turn the segment array into a URI string
373 $uri = implode('/', $this->uri->segments);
Barry Mienydd671972010-10-04 16:33:58 +0200374
Fatih Kalifa442b50b2013-11-01 02:44:56 +0700375 // Get HTTP verb
Andrey Andreevc761a202013-11-11 14:02:15 +0200376 $http_verb = isset($_SERVER['REQUEST_METHOD']) ? strtolower($_SERVER['REQUEST_METHOD']) : 'cli';
Fatih Kalifa442b50b2013-11-01 02:44:56 +0700377
vlakoffc941d852013-08-06 14:44:40 +0200378 // Loop through the route array looking for wildcards
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100379 foreach ($this->routes as $key => $val)
380 {
Andrey Andreevabc299b2015-08-05 12:26:22 +0300381 // Check if route format is using HTTP verbs
Fatih Kalifa442b50b2013-11-01 02:44:56 +0700382 if (is_array($val))
383 {
Andrey Andreevabc299b2015-08-05 12:26:22 +0300384 $val = array_change_key_case($val, CASE_LOWER);
Fatih Kalifa442b50b2013-11-01 02:44:56 +0700385 if (isset($val[$http_verb]))
386 {
387 $val = $val[$http_verb];
388 }
Fatih Kalifa442b50b2013-11-01 02:44:56 +0700389 else
390 {
Fatih Kalifa442b50b2013-11-01 02:44:56 +0700391 continue;
392 }
393 }
394
vlakoffc941d852013-08-06 14:44:40 +0200395 // Convert wildcards to RegEx
Andrey Andreev7676c2d2012-10-30 13:42:01 +0200396 $key = str_replace(array(':any', ':num'), array('[^/]+', '[0-9]+'), $key);
Derek Jonesc7738402010-03-02 13:55:13 -0600397
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100398 // Does the RegEx match?
399 if (preg_match('#^'.$key.'$#', $uri, $matches))
400 {
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100401 // Are we using callbacks to process back-references?
Andrey Andreevda5562a2012-11-08 12:34:38 +0200402 if ( ! is_string($val) && is_callable($val))
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100403 {
404 // Remove the original string from the matches array.
405 array_shift($matches);
Derek Allard2067d1a2008-11-13 22:59:24 +0000406
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100407 // Execute the callback using the values in matches as its parameters.
408 $val = call_user_func_array($val, $matches);
409 }
Andrey Andreevda5562a2012-11-08 12:34:38 +0200410 // Are we using the default routing method for back-references?
411 elseif (strpos($val, '$') !== FALSE && strpos($key, '(') !== FALSE)
412 {
413 $val = preg_replace('#^'.$key.'$#', $val, $uri);
414 }
Jonatas Miguelc0d98b22012-08-06 15:42:50 +0100415
Andrey Andreev30d53242014-01-16 14:41:46 +0200416 $this->_set_request(explode('/', $val));
417 return;
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100418 }
419 }
Jonatas Miguelc0d98b22012-08-06 15:42:50 +0100420
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100421 // If we got this far it means we didn't encounter a
422 // matching route so we'll set the site default route
Andrey Andreeva9237cb2014-01-18 19:07:20 +0200423 $this->_set_request(array_values($this->uri->segments));
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100424 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000425
426 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200427
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300429 * Set class name
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300431 * @param string $class Class name
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200433 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200434 public function set_class($class)
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 {
Derek Jones2615e412010-10-06 17:51:16 -0500436 $this->class = str_replace(array('/', '.'), '', $class);
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 }
Barry Mienydd671972010-10-04 16:33:58 +0200438
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200440
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 /**
442 * Fetch the current class
443 *
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300444 * @deprecated 3.0.0 Read the 'class' property instead
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200446 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200447 public function fetch_class()
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 {
449 return $this->class;
450 }
Barry Mienydd671972010-10-04 16:33:58 +0200451
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200453
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300455 * Set method name
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300457 * @param string $method Method name
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200459 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200460 public function set_method($method)
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 {
462 $this->method = $method;
463 }
464
465 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200466
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 /**
Andrey Andreev7b53d042012-03-26 23:02:32 +0300468 * Fetch the current method
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 *
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300470 * @deprecated 3.0.0 Read the 'method' property instead
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200472 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200473 public function fetch_method()
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 {
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300475 return $this->method;
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 }
477
478 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200479
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300481 * Set directory name
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300483 * @param string $dir Directory name
Calvin Tam55bc5052015-07-24 02:27:24 -0700484 * @param bool $append Whether we're appending rather than setting the full value
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200486 */
Andrey Andreev30d53242014-01-16 14:41:46 +0200487 public function set_directory($dir, $append = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 {
Andrey Andreev30d53242014-01-16 14:41:46 +0200489 if ($append !== TRUE OR empty($this->directory))
490 {
491 $this->directory = str_replace('.', '', trim($dir, '/')).'/';
492 }
493 else
494 {
495 $this->directory .= str_replace('.', '', trim($dir, '/')).'/';
496 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 }
498
499 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200500
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300502 * Fetch directory
503 *
504 * Feches the sub-directory (if any) that contains the requested
505 * controller class.
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 *
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300507 * @deprecated 3.0.0 Read the 'directory' property instead
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200509 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200510 public function fetch_directory()
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 {
512 return $this->directory;
513 }
514
515}