blob: af87a305a87b0ea0b03cecc332cbfb21d0d9c992 [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 Andreevba6c0412012-01-07 21:10:09 +0200156 // Are query strings enabled in the config file? Normally CI doesn't utilize query strings
Barry Mienydd671972010-10-04 16:33:58 +0200157 // since URI segments are more search-engine friendly, but they can optionally be used.
Derek Jonesc7738402010-03-02 13:55:13 -0600158 // If this feature is enabled, we will gather the directory/class/method a little differently
Andrey Andreev30d53242014-01-16 14:41:46 +0200159 if ($this->enable_query_strings)
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 {
Andrey Andreev2ef5ed42015-07-17 14:24:26 +0300161 // If the directory is set at this time, it means an override exists, so skip the checks
162 if ( ! isset($this->directory))
Derek Jonesc7738402010-03-02 13:55:13 -0600163 {
Andrey Andreev2ef5ed42015-07-17 14:24:26 +0300164 $_d = $this->config->item('directory_trigger');
165 $_d = isset($_GET[$_d]) ? trim($_GET[$_d], " \t\n\r\0\x0B/") : '';
166
167 if ($_d !== '')
168 {
169 $this->uri->filter_uri($_d);
170 $this->set_directory($_d);
171 }
Derek Jonesc7738402010-03-02 13:55:13 -0600172 }
Barry Mienydd671972010-10-04 16:33:58 +0200173
Andrey Andreevbfa233f2014-12-05 12:00:11 +0200174 $_c = trim($this->config->item('controller_trigger'));
Andrey Andreev30d53242014-01-16 14:41:46 +0200175 if ( ! empty($_GET[$_c]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 {
Andrey Andreevbfa233f2014-12-05 12:00:11 +0200177 $this->uri->filter_uri($_GET[$_c]);
178 $this->set_class($_GET[$_c]);
Andrey Andreev30d53242014-01-16 14:41:46 +0200179
Andrey Andreevbfa233f2014-12-05 12:00:11 +0200180 $_f = trim($this->config->item('function_trigger'));
Andrey Andreev30d53242014-01-16 14:41:46 +0200181 if ( ! empty($_GET[$_f]))
182 {
Andrey Andreevbfa233f2014-12-05 12:00:11 +0200183 $this->uri->filter_uri($_GET[$_f]);
184 $this->set_method($_GET[$_f]);
Andrey Andreev30d53242014-01-16 14:41:46 +0200185 }
186
187 $this->uri->rsegments = array(
188 1 => $this->class,
189 2 => $this->method
190 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 }
Andrey Andreev30d53242014-01-16 14:41:46 +0200192 else
193 {
194 $this->_set_default_controller();
195 }
196
197 // Routing rules don't apply to query strings and we don't need to detect
198 // directories, so we're done here
199 return;
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 }
Barry Mienydd671972010-10-04 16:33:58 +0200201
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 // Load the routes.php file.
Andrey Andreev06879112013-01-29 15:05:02 +0200203 if (file_exists(APPPATH.'config/routes.php'))
Greg Akerd96f8822011-12-27 16:23:47 -0600204 {
205 include(APPPATH.'config/routes.php');
206 }
David Behler07b53422011-08-15 00:25:06 +0200207
Andrey Andreev06879112013-01-29 15:05:02 +0200208 if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/routes.php'))
209 {
210 include(APPPATH.'config/'.ENVIRONMENT.'/routes.php');
211 }
212
Andrey Andreev08fec7b2013-07-19 16:25:51 +0300213 // Validate & get reserved routes
214 if (isset($route) && is_array($route))
215 {
216 isset($route['default_controller']) && $this->default_controller = $route['default_controller'];
217 isset($route['translate_uri_dashes']) && $this->translate_uri_dashes = $route['translate_uri_dashes'];
218 unset($route['default_controller'], $route['translate_uri_dashes']);
219 $this->routes = $route;
220 }
Barry Mienydd671972010-10-04 16:33:58 +0200221
Andrey Andreev30d53242014-01-16 14:41:46 +0200222 // Is there anything to parse?
223 if ($this->uri->uri_string !== '')
Derek Jonesc7738402010-03-02 13:55:13 -0600224 {
Andrey Andreev30d53242014-01-16 14:41:46 +0200225 $this->_parse_routes();
226 }
227 else
228 {
229 $this->_set_default_controller();
230 }
231 }
232
233 // --------------------------------------------------------------------
234
235 /**
236 * Set request route
237 *
238 * Takes an array of URI segments as input and sets the class/method
239 * to be called.
240 *
241 * @used-by CI_Router::_parse_routes()
242 * @param array $segments URI segments
243 * @return void
244 */
245 protected function _set_request($segments = array())
246 {
247 $segments = $this->_validate_request($segments);
248 // If we don't have any segments left - try the default controller;
249 // WARNING: Directories get shifted out of the segments array!
250 if (empty($segments))
251 {
252 $this->_set_default_controller();
253 return;
Derek Jonesc7738402010-03-02 13:55:13 -0600254 }
Barry Mienydd671972010-10-04 16:33:58 +0200255
Andrey Andreev30d53242014-01-16 14:41:46 +0200256 if ($this->translate_uri_dashes === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 {
Andrey Andreev30d53242014-01-16 14:41:46 +0200258 $segments[0] = str_replace('-', '_', $segments[0]);
259 if (isset($segments[1]))
260 {
261 $segments[1] = str_replace('-', '_', $segments[1]);
262 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 }
Barry Mienydd671972010-10-04 16:33:58 +0200264
Andrey Andreev30d53242014-01-16 14:41:46 +0200265 $this->set_class($segments[0]);
266 if (isset($segments[1]))
267 {
268 $this->set_method($segments[1]);
269 }
Andrey Andreev9cab4272014-03-30 18:58:23 +0300270 else
271 {
272 $segments[1] = 'index';
273 }
Andrey Andreev30d53242014-01-16 14:41:46 +0200274
275 array_unshift($segments, NULL);
276 unset($segments[0]);
277 $this->uri->rsegments = $segments;
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 }
Derek Jonesc7738402010-03-02 13:55:13 -0600279
280 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200281
Derek Jonesc7738402010-03-02 13:55:13 -0600282 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300283 * Set default controller
Derek Jonesc7738402010-03-02 13:55:13 -0600284 *
Derek Jonesc7738402010-03-02 13:55:13 -0600285 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200286 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200287 protected function _set_default_controller()
Derek Jonesc7738402010-03-02 13:55:13 -0600288 {
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200289 if (empty($this->default_controller))
Derek Jonesc7738402010-03-02 13:55:13 -0600290 {
Andrey Andreevba6c0412012-01-07 21:10:09 +0200291 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 -0600292 }
Andrey Andreevd1097a12012-11-01 19:55:42 +0200293
Derek Jonesc7738402010-03-02 13:55:13 -0600294 // Is the method being specified?
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200295 if (sscanf($this->default_controller, '%[^/]/%s', $class, $method) !== 2)
Derek Jonesc7738402010-03-02 13:55:13 -0600296 {
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200297 $method = 'index';
Barry Mienydd671972010-10-04 16:33:58 +0200298 }
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200299
Andrey Andreev30d53242014-01-16 14:41:46 +0200300 if ( ! file_exists(APPPATH.'controllers/'.$this->directory.ucfirst($class).'.php'))
301 {
302 // This will trigger 404 later
303 return;
304 }
Barry Mienydd671972010-10-04 16:33:58 +0200305
Andrey Andreev30d53242014-01-16 14:41:46 +0200306 $this->set_class($class);
307 $this->set_method($method);
308
309 // Assign routed segments, index starting from 1
310 $this->uri->rsegments = array(
311 1 => $class,
312 2 => $method
313 );
Barry Mienydd671972010-10-04 16:33:58 +0200314
Andrey Andreevba6c0412012-01-07 21:10:09 +0200315 log_message('debug', 'No URI present. Default controller set.');
Derek Jonesc7738402010-03-02 13:55:13 -0600316 }
Barry Mienydd671972010-10-04 16:33:58 +0200317
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200319
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300321 * Validate request
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300323 * Attempts validate the URI request and determine the controller path.
324 *
Andrey Andreev30d53242014-01-16 14:41:46 +0200325 * @used-by CI_Router::_set_request()
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300326 * @param array $segments URI segments
Andrey Andreev30d53242014-01-16 14:41:46 +0200327 * @return mixed URI segments
Barry Mienydd671972010-10-04 16:33:58 +0200328 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200329 protected function _validate_request($segments)
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 {
Andrey Andreev30d53242014-01-16 14:41:46 +0200331 $c = count($segments);
Andrey Andreev2ef5ed42015-07-17 14:24:26 +0300332 $directory_override = isset($this->directory);
333
Andrey Andreev30d53242014-01-16 14:41:46 +0200334 // Loop through our segments and return as soon as a controller
335 // is found or when such a directory doesn't exist
336 while ($c-- > 0)
Derek Jonesc7738402010-03-02 13:55:13 -0600337 {
Andrey Andreev30d53242014-01-16 14:41:46 +0200338 $test = $this->directory
339 .ucfirst($this->translate_uri_dashes === TRUE ? str_replace('-', '_', $segments[0]) : $segments[0]);
Barry Mienydd671972010-10-04 16:33:58 +0200340
Andrey Andreev2ef5ed42015-07-17 14:24:26 +0300341 if ( ! file_exists(APPPATH.'controllers/'.$test.'.php')
342 && $directory_override === FALSE
343 && is_dir(APPPATH.'controllers/'.$this->directory.$segments[0])
344 )
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 {
Andrey Andreev30d53242014-01-16 14:41:46 +0200346 $this->set_directory(array_shift($segments), TRUE);
347 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 }
Barry Mienydd671972010-10-04 16:33:58 +0200349
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 return $segments;
351 }
Barry Mienydd671972010-10-04 16:33:58 +0200352
Andrey Andreev30d53242014-01-16 14:41:46 +0200353 // This means that all segments were actually directories
354 return $segments;
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 }
Barry Mienydd671972010-10-04 16:33:58 +0200356
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200358
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 /**
Andrey Andreev7b53d042012-03-26 23:02:32 +0300360 * Parse Routes
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300362 * Matches any routes that may exist in the config/routes.php file
363 * against the URI to determine if the class/method need to be remapped.
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 * @return void
366 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200367 protected function _parse_routes()
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100368 {
369 // Turn the segment array into a URI string
370 $uri = implode('/', $this->uri->segments);
Barry Mienydd671972010-10-04 16:33:58 +0200371
Fatih Kalifa442b50b2013-11-01 02:44:56 +0700372 // Get HTTP verb
Andrey Andreevc761a202013-11-11 14:02:15 +0200373 $http_verb = isset($_SERVER['REQUEST_METHOD']) ? strtolower($_SERVER['REQUEST_METHOD']) : 'cli';
Fatih Kalifa442b50b2013-11-01 02:44:56 +0700374
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100375 // Is there a literal match? If so we're done
Fatih Kalifa0b58b3c2013-11-05 15:36:40 +0700376 if (isset($this->routes[$uri]))
Fatih Kalifa442b50b2013-11-01 02:44:56 +0700377 {
Fatih Kalifa0b58b3c2013-11-05 15:36:40 +0700378 // Check default routes format
379 if (is_string($this->routes[$uri]))
380 {
Andrey Andreev30d53242014-01-16 14:41:46 +0200381 $this->_set_request(explode('/', $this->routes[$uri]));
382 return;
Fatih Kalifa0b58b3c2013-11-05 15:36:40 +0700383 }
Andrey Andreevc761a202013-11-11 14:02:15 +0200384 // Is there a matching http verb?
Fatih Kalifa0b58b3c2013-11-05 15:36:40 +0700385 elseif (is_array($this->routes[$uri]) && isset($this->routes[$uri][$http_verb]))
386 {
Andrey Andreev30d53242014-01-16 14:41:46 +0200387 $this->_set_request(explode('/', $this->routes[$uri][$http_verb]));
388 return;
Fatih Kalifa0b58b3c2013-11-05 15:36:40 +0700389 }
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100390 }
Barry Mienydd671972010-10-04 16:33:58 +0200391
vlakoffc941d852013-08-06 14:44:40 +0200392 // Loop through the route array looking for wildcards
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100393 foreach ($this->routes as $key => $val)
394 {
Fatih Kalifa0b58b3c2013-11-05 15:36:40 +0700395 // Check if route format is using http verb
Fatih Kalifa442b50b2013-11-01 02:44:56 +0700396 if (is_array($val))
397 {
Fatih Kalifa442b50b2013-11-01 02:44:56 +0700398 if (isset($val[$http_verb]))
399 {
400 $val = $val[$http_verb];
401 }
Fatih Kalifa442b50b2013-11-01 02:44:56 +0700402 else
403 {
Fatih Kalifa442b50b2013-11-01 02:44:56 +0700404 continue;
405 }
406 }
407
vlakoffc941d852013-08-06 14:44:40 +0200408 // Convert wildcards to RegEx
Andrey Andreev7676c2d2012-10-30 13:42:01 +0200409 $key = str_replace(array(':any', ':num'), array('[^/]+', '[0-9]+'), $key);
Derek Jonesc7738402010-03-02 13:55:13 -0600410
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100411 // Does the RegEx match?
412 if (preg_match('#^'.$key.'$#', $uri, $matches))
413 {
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100414 // Are we using callbacks to process back-references?
Andrey Andreevda5562a2012-11-08 12:34:38 +0200415 if ( ! is_string($val) && is_callable($val))
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100416 {
417 // Remove the original string from the matches array.
418 array_shift($matches);
Derek Allard2067d1a2008-11-13 22:59:24 +0000419
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100420 // Execute the callback using the values in matches as its parameters.
421 $val = call_user_func_array($val, $matches);
422 }
Andrey Andreevda5562a2012-11-08 12:34:38 +0200423 // Are we using the default routing method for back-references?
424 elseif (strpos($val, '$') !== FALSE && strpos($key, '(') !== FALSE)
425 {
426 $val = preg_replace('#^'.$key.'$#', $val, $uri);
427 }
Jonatas Miguelc0d98b22012-08-06 15:42:50 +0100428
Andrey Andreev30d53242014-01-16 14:41:46 +0200429 $this->_set_request(explode('/', $val));
430 return;
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100431 }
432 }
Jonatas Miguelc0d98b22012-08-06 15:42:50 +0100433
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100434 // If we got this far it means we didn't encounter a
435 // matching route so we'll set the site default route
Andrey Andreeva9237cb2014-01-18 19:07:20 +0200436 $this->_set_request(array_values($this->uri->segments));
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100437 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000438
439 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200440
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300442 * Set class name
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300444 * @param string $class Class name
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200446 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200447 public function set_class($class)
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 {
Derek Jones2615e412010-10-06 17:51:16 -0500449 $this->class = str_replace(array('/', '.'), '', $class);
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 }
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 /**
455 * Fetch the current class
456 *
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300457 * @deprecated 3.0.0 Read the 'class' property instead
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200459 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200460 public function fetch_class()
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 {
462 return $this->class;
463 }
Barry Mienydd671972010-10-04 16:33:58 +0200464
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200466
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300468 * Set method name
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300470 * @param string $method Method name
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200472 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200473 public function set_method($method)
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 {
475 $this->method = $method;
476 }
477
478 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200479
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 /**
Andrey Andreev7b53d042012-03-26 23:02:32 +0300481 * Fetch the current method
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 *
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300483 * @deprecated 3.0.0 Read the 'method' property instead
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200485 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200486 public function fetch_method()
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 {
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300488 return $this->method;
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 }
490
491 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200492
Derek Allard2067d1a2008-11-13 22:59:24 +0000493 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300494 * Set directory name
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300496 * @param string $dir Directory name
Andrey Andreev1924eb32015-04-08 17:19:24 +0300497 * @param bool $appent Whether we're appending rather than setting the full value
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200499 */
Andrey Andreev30d53242014-01-16 14:41:46 +0200500 public function set_directory($dir, $append = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 {
Andrey Andreev30d53242014-01-16 14:41:46 +0200502 if ($append !== TRUE OR empty($this->directory))
503 {
504 $this->directory = str_replace('.', '', trim($dir, '/')).'/';
505 }
506 else
507 {
508 $this->directory .= str_replace('.', '', trim($dir, '/')).'/';
509 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 }
511
512 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200513
Derek Allard2067d1a2008-11-13 22:59:24 +0000514 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300515 * Fetch directory
516 *
517 * Feches the sub-directory (if any) that contains the requested
518 * controller class.
Derek Allard2067d1a2008-11-13 22:59:24 +0000519 *
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300520 * @deprecated 3.0.0 Read the 'directory' property instead
Derek Allard2067d1a2008-11-13 22:59:24 +0000521 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200522 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200523 public function fetch_directory()
Derek Allard2067d1a2008-11-13 22:59:24 +0000524 {
525 return $this->directory;
526 }
527
528}