blob: d86735f5fc1d44ef547c8c924da57d488471544e [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 *
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 Andreevbdb96ca2014-10-28 00:13:31 +02009 * Copyright (c) 2014, 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 Andreevbdb96ca2014-10-28 00:13:31 +020032 * @copyright Copyright (c) 2014, British Columbia Institute of Technology (http://bcit.ca/)
33 * @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 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040086 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 *
108 * Determines wether to use GET parameters or segment URIs
109 *
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 Andreev08fec7b2013-07-19 16:25:51 +0300129 $this->_set_routing();
Andrey Andreev4e6c5282014-01-10 19:29:49 +0200130
131 // Set any routing overrides that may exist in the main index file
Andrey Andreevc26b9eb2014-02-24 11:31:36 +0200132 if (is_array($routing))
Andrey Andreev4e6c5282014-01-10 19:29:49 +0200133 {
134 if (isset($routing['directory']))
135 {
136 $this->set_directory($routing['directory']);
137 }
138
139 if ( ! empty($routing['controller']))
140 {
141 $this->set_class($routing['controller']);
142 }
143
Andrey Andreevbc89b3c2014-04-15 17:46:05 +0300144 if ( ! empty($routing['function']))
Andrey Andreev4e6c5282014-01-10 19:29:49 +0200145 {
Andrey Andreev4e6c5282014-01-10 19:29:49 +0200146 $this->set_method($routing['function']);
147 }
148 }
149
Andrey Andreevba6c0412012-01-07 21:10:09 +0200150 log_message('debug', 'Router Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 }
Barry Mienydd671972010-10-04 16:33:58 +0200152
Derek Allard2067d1a2008-11-13 22:59:24 +0000153 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200154
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300156 * Set route mapping
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300158 * Determines what should be served based on the URI request,
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 * as well as any "routes" that have been set in the routing config file.
160 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 * @return void
162 */
Andrey Andreev08fec7b2013-07-19 16:25:51 +0300163 protected function _set_routing()
Barry Mienydd671972010-10-04 16:33:58 +0200164 {
Andrey Andreevba6c0412012-01-07 21:10:09 +0200165 // Are query strings enabled in the config file? Normally CI doesn't utilize query strings
Barry Mienydd671972010-10-04 16:33:58 +0200166 // since URI segments are more search-engine friendly, but they can optionally be used.
Derek Jonesc7738402010-03-02 13:55:13 -0600167 // If this feature is enabled, we will gather the directory/class/method a little differently
Andrey Andreev30d53242014-01-16 14:41:46 +0200168 if ($this->enable_query_strings)
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 {
Andrey Andreev30d53242014-01-16 14:41:46 +0200170 $_d = $this->config->item('directory_trigger');
171 $_d = isset($_GET[$_d]) ? trim($_GET[$_d], " \t\n\r\0\x0B/") : '';
172 if ($_d !== '')
Derek Jonesc7738402010-03-02 13:55:13 -0600173 {
Andrey Andreevbfa233f2014-12-05 12:00:11 +0200174 $this->uri->filter_uri($_d);
175 $this->set_directory($_d);
Derek Jonesc7738402010-03-02 13:55:13 -0600176 }
Barry Mienydd671972010-10-04 16:33:58 +0200177
Andrey Andreevbfa233f2014-12-05 12:00:11 +0200178 $_c = trim($this->config->item('controller_trigger'));
Andrey Andreev30d53242014-01-16 14:41:46 +0200179 if ( ! empty($_GET[$_c]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 {
Andrey Andreevbfa233f2014-12-05 12:00:11 +0200181 $this->uri->filter_uri($_GET[$_c]);
182 $this->set_class($_GET[$_c]);
Andrey Andreev30d53242014-01-16 14:41:46 +0200183
Andrey Andreevbfa233f2014-12-05 12:00:11 +0200184 $_f = trim($this->config->item('function_trigger'));
Andrey Andreev30d53242014-01-16 14:41:46 +0200185 if ( ! empty($_GET[$_f]))
186 {
Andrey Andreevbfa233f2014-12-05 12:00:11 +0200187 $this->uri->filter_uri($_GET[$_f]);
188 $this->set_method($_GET[$_f]);
Andrey Andreev30d53242014-01-16 14:41:46 +0200189 }
190
191 $this->uri->rsegments = array(
192 1 => $this->class,
193 2 => $this->method
194 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 }
Andrey Andreev30d53242014-01-16 14:41:46 +0200196 else
197 {
198 $this->_set_default_controller();
199 }
200
201 // Routing rules don't apply to query strings and we don't need to detect
202 // directories, so we're done here
203 return;
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 }
Barry Mienydd671972010-10-04 16:33:58 +0200205
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 // Load the routes.php file.
Andrey Andreev06879112013-01-29 15:05:02 +0200207 if (file_exists(APPPATH.'config/routes.php'))
Greg Akerd96f8822011-12-27 16:23:47 -0600208 {
209 include(APPPATH.'config/routes.php');
210 }
David Behler07b53422011-08-15 00:25:06 +0200211
Andrey Andreev06879112013-01-29 15:05:02 +0200212 if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/routes.php'))
213 {
214 include(APPPATH.'config/'.ENVIRONMENT.'/routes.php');
215 }
216
Andrey Andreev08fec7b2013-07-19 16:25:51 +0300217 // Validate & get reserved routes
218 if (isset($route) && is_array($route))
219 {
220 isset($route['default_controller']) && $this->default_controller = $route['default_controller'];
221 isset($route['translate_uri_dashes']) && $this->translate_uri_dashes = $route['translate_uri_dashes'];
222 unset($route['default_controller'], $route['translate_uri_dashes']);
223 $this->routes = $route;
224 }
Barry Mienydd671972010-10-04 16:33:58 +0200225
Andrey Andreev30d53242014-01-16 14:41:46 +0200226 // Is there anything to parse?
227 if ($this->uri->uri_string !== '')
Derek Jonesc7738402010-03-02 13:55:13 -0600228 {
Andrey Andreev30d53242014-01-16 14:41:46 +0200229 $this->_parse_routes();
230 }
231 else
232 {
233 $this->_set_default_controller();
234 }
235 }
236
237 // --------------------------------------------------------------------
238
239 /**
240 * Set request route
241 *
242 * Takes an array of URI segments as input and sets the class/method
243 * to be called.
244 *
245 * @used-by CI_Router::_parse_routes()
246 * @param array $segments URI segments
247 * @return void
248 */
249 protected function _set_request($segments = array())
250 {
251 $segments = $this->_validate_request($segments);
252 // If we don't have any segments left - try the default controller;
253 // WARNING: Directories get shifted out of the segments array!
254 if (empty($segments))
255 {
256 $this->_set_default_controller();
257 return;
Derek Jonesc7738402010-03-02 13:55:13 -0600258 }
Barry Mienydd671972010-10-04 16:33:58 +0200259
Andrey Andreev30d53242014-01-16 14:41:46 +0200260 if ($this->translate_uri_dashes === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 {
Andrey Andreev30d53242014-01-16 14:41:46 +0200262 $segments[0] = str_replace('-', '_', $segments[0]);
263 if (isset($segments[1]))
264 {
265 $segments[1] = str_replace('-', '_', $segments[1]);
266 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 }
Barry Mienydd671972010-10-04 16:33:58 +0200268
Andrey Andreev30d53242014-01-16 14:41:46 +0200269 $this->set_class($segments[0]);
270 if (isset($segments[1]))
271 {
272 $this->set_method($segments[1]);
273 }
Andrey Andreev9cab4272014-03-30 18:58:23 +0300274 else
275 {
276 $segments[1] = 'index';
277 }
Andrey Andreev30d53242014-01-16 14:41:46 +0200278
279 array_unshift($segments, NULL);
280 unset($segments[0]);
281 $this->uri->rsegments = $segments;
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 }
Derek Jonesc7738402010-03-02 13:55:13 -0600283
284 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200285
Derek Jonesc7738402010-03-02 13:55:13 -0600286 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300287 * Set default controller
Derek Jonesc7738402010-03-02 13:55:13 -0600288 *
Derek Jonesc7738402010-03-02 13:55:13 -0600289 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200290 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200291 protected function _set_default_controller()
Derek Jonesc7738402010-03-02 13:55:13 -0600292 {
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200293 if (empty($this->default_controller))
Derek Jonesc7738402010-03-02 13:55:13 -0600294 {
Andrey Andreevba6c0412012-01-07 21:10:09 +0200295 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 -0600296 }
Andrey Andreevd1097a12012-11-01 19:55:42 +0200297
Derek Jonesc7738402010-03-02 13:55:13 -0600298 // Is the method being specified?
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200299 if (sscanf($this->default_controller, '%[^/]/%s', $class, $method) !== 2)
Derek Jonesc7738402010-03-02 13:55:13 -0600300 {
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200301 $method = 'index';
Barry Mienydd671972010-10-04 16:33:58 +0200302 }
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200303
Andrey Andreev30d53242014-01-16 14:41:46 +0200304 if ( ! file_exists(APPPATH.'controllers/'.$this->directory.ucfirst($class).'.php'))
305 {
306 // This will trigger 404 later
307 return;
308 }
Barry Mienydd671972010-10-04 16:33:58 +0200309
Andrey Andreev30d53242014-01-16 14:41:46 +0200310 $this->set_class($class);
311 $this->set_method($method);
312
313 // Assign routed segments, index starting from 1
314 $this->uri->rsegments = array(
315 1 => $class,
316 2 => $method
317 );
Barry Mienydd671972010-10-04 16:33:58 +0200318
Andrey Andreevba6c0412012-01-07 21:10:09 +0200319 log_message('debug', 'No URI present. Default controller set.');
Derek Jonesc7738402010-03-02 13:55:13 -0600320 }
Barry Mienydd671972010-10-04 16:33:58 +0200321
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200323
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300325 * Validate request
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300327 * Attempts validate the URI request and determine the controller path.
328 *
Andrey Andreev30d53242014-01-16 14:41:46 +0200329 * @used-by CI_Router::_set_request()
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300330 * @param array $segments URI segments
Andrey Andreev30d53242014-01-16 14:41:46 +0200331 * @return mixed URI segments
Barry Mienydd671972010-10-04 16:33:58 +0200332 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200333 protected function _validate_request($segments)
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 {
Andrey Andreev30d53242014-01-16 14:41:46 +0200335 $c = count($segments);
336 // 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 Andreev30d53242014-01-16 14:41:46 +0200343 if ( ! file_exists(APPPATH.'controllers/'.$test.'.php') && is_dir(APPPATH.'controllers/'.$this->directory.$segments[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 {
Andrey Andreev30d53242014-01-16 14:41:46 +0200345 $this->set_directory(array_shift($segments), TRUE);
346 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 }
Barry Mienydd671972010-10-04 16:33:58 +0200348
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 return $segments;
350 }
Barry Mienydd671972010-10-04 16:33:58 +0200351
Andrey Andreev30d53242014-01-16 14:41:46 +0200352 // This means that all segments were actually directories
353 return $segments;
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 }
Barry Mienydd671972010-10-04 16:33:58 +0200355
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200357
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 /**
Andrey Andreev7b53d042012-03-26 23:02:32 +0300359 * Parse Routes
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300361 * Matches any routes that may exist in the config/routes.php file
362 * against the URI to determine if the class/method need to be remapped.
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 * @return void
365 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200366 protected function _parse_routes()
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100367 {
368 // Turn the segment array into a URI string
369 $uri = implode('/', $this->uri->segments);
Barry Mienydd671972010-10-04 16:33:58 +0200370
Fatih Kalifa442b50b2013-11-01 02:44:56 +0700371 // Get HTTP verb
Andrey Andreevc761a202013-11-11 14:02:15 +0200372 $http_verb = isset($_SERVER['REQUEST_METHOD']) ? strtolower($_SERVER['REQUEST_METHOD']) : 'cli';
Fatih Kalifa442b50b2013-11-01 02:44:56 +0700373
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100374 // Is there a literal match? If so we're done
Fatih Kalifa0b58b3c2013-11-05 15:36:40 +0700375 if (isset($this->routes[$uri]))
Fatih Kalifa442b50b2013-11-01 02:44:56 +0700376 {
Fatih Kalifa0b58b3c2013-11-05 15:36:40 +0700377 // Check default routes format
378 if (is_string($this->routes[$uri]))
379 {
Andrey Andreev30d53242014-01-16 14:41:46 +0200380 $this->_set_request(explode('/', $this->routes[$uri]));
381 return;
Fatih Kalifa0b58b3c2013-11-05 15:36:40 +0700382 }
Andrey Andreevc761a202013-11-11 14:02:15 +0200383 // Is there a matching http verb?
Fatih Kalifa0b58b3c2013-11-05 15:36:40 +0700384 elseif (is_array($this->routes[$uri]) && isset($this->routes[$uri][$http_verb]))
385 {
Andrey Andreev30d53242014-01-16 14:41:46 +0200386 $this->_set_request(explode('/', $this->routes[$uri][$http_verb]));
387 return;
Fatih Kalifa0b58b3c2013-11-05 15:36:40 +0700388 }
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100389 }
Barry Mienydd671972010-10-04 16:33:58 +0200390
vlakoffc941d852013-08-06 14:44:40 +0200391 // Loop through the route array looking for wildcards
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100392 foreach ($this->routes as $key => $val)
393 {
Fatih Kalifa0b58b3c2013-11-05 15:36:40 +0700394 // Check if route format is using http verb
Fatih Kalifa442b50b2013-11-01 02:44:56 +0700395 if (is_array($val))
396 {
Fatih Kalifa442b50b2013-11-01 02:44:56 +0700397 if (isset($val[$http_verb]))
398 {
399 $val = $val[$http_verb];
400 }
Fatih Kalifa442b50b2013-11-01 02:44:56 +0700401 else
402 {
Fatih Kalifa442b50b2013-11-01 02:44:56 +0700403 continue;
404 }
405 }
406
vlakoffc941d852013-08-06 14:44:40 +0200407 // Convert wildcards to RegEx
Andrey Andreev7676c2d2012-10-30 13:42:01 +0200408 $key = str_replace(array(':any', ':num'), array('[^/]+', '[0-9]+'), $key);
Derek Jonesc7738402010-03-02 13:55:13 -0600409
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100410 // Does the RegEx match?
411 if (preg_match('#^'.$key.'$#', $uri, $matches))
412 {
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100413 // Are we using callbacks to process back-references?
Andrey Andreevda5562a2012-11-08 12:34:38 +0200414 if ( ! is_string($val) && is_callable($val))
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100415 {
416 // Remove the original string from the matches array.
417 array_shift($matches);
Derek Allard2067d1a2008-11-13 22:59:24 +0000418
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100419 // Execute the callback using the values in matches as its parameters.
420 $val = call_user_func_array($val, $matches);
421 }
Andrey Andreevda5562a2012-11-08 12:34:38 +0200422 // Are we using the default routing method for back-references?
423 elseif (strpos($val, '$') !== FALSE && strpos($key, '(') !== FALSE)
424 {
425 $val = preg_replace('#^'.$key.'$#', $val, $uri);
426 }
Jonatas Miguelc0d98b22012-08-06 15:42:50 +0100427
Andrey Andreev30d53242014-01-16 14:41:46 +0200428 $this->_set_request(explode('/', $val));
429 return;
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100430 }
431 }
Jonatas Miguelc0d98b22012-08-06 15:42:50 +0100432
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100433 // If we got this far it means we didn't encounter a
434 // matching route so we'll set the site default route
Andrey Andreeva9237cb2014-01-18 19:07:20 +0200435 $this->_set_request(array_values($this->uri->segments));
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100436 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000437
438 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200439
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300441 * Set class name
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300443 * @param string $class Class name
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200445 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200446 public function set_class($class)
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 {
Derek Jones2615e412010-10-06 17:51:16 -0500448 $this->class = str_replace(array('/', '.'), '', $class);
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 }
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 /**
454 * Fetch the current class
455 *
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300456 * @deprecated 3.0.0 Read the 'class' property instead
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200458 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200459 public function fetch_class()
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 {
461 return $this->class;
462 }
Barry Mienydd671972010-10-04 16:33:58 +0200463
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200465
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300467 * Set method name
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300469 * @param string $method Method name
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200471 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200472 public function set_method($method)
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 {
474 $this->method = $method;
475 }
476
477 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200478
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 /**
Andrey Andreev7b53d042012-03-26 23:02:32 +0300480 * Fetch the current method
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 *
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300482 * @deprecated 3.0.0 Read the 'method' property instead
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200484 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200485 public function fetch_method()
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 {
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300487 return $this->method;
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 }
489
490 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200491
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300493 * Set directory name
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300495 * @param string $dir Directory name
Andrey Andreev30d53242014-01-16 14:41:46 +0200496 * @param bool $appent Whether we're appending rather then setting the full value
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200498 */
Andrey Andreev30d53242014-01-16 14:41:46 +0200499 public function set_directory($dir, $append = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 {
Andrey Andreev30d53242014-01-16 14:41:46 +0200501 if ($append !== TRUE OR empty($this->directory))
502 {
503 $this->directory = str_replace('.', '', trim($dir, '/')).'/';
504 }
505 else
506 {
507 $this->directory .= str_replace('.', '', trim($dir, '/')).'/';
508 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000509 }
510
511 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200512
Derek Allard2067d1a2008-11-13 22:59:24 +0000513 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300514 * Fetch directory
515 *
516 * Feches the sub-directory (if any) that contains the requested
517 * controller class.
Derek Allard2067d1a2008-11-13 22:59:24 +0000518 *
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300519 * @deprecated 3.0.0 Read the 'directory' property instead
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200521 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200522 public function fetch_directory()
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 {
524 return $this->directory;
525 }
526
527}
Derek Allard2067d1a2008-11-13 22:59:24 +0000528
529/* End of file Router.php */
Andrey Andreev7b53d042012-03-26 23:02:32 +0300530/* Location: ./system/core/Router.php */