blob: 7f18adbf596af864de48facf052425c4a79a3f47 [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 Andreev30d53242014-01-16 14:41:46 +0200174 $this->set_directory($this->uri->filter_uri($_d));
Derek Jonesc7738402010-03-02 13:55:13 -0600175 }
Barry Mienydd671972010-10-04 16:33:58 +0200176
Andrey Andreev30d53242014-01-16 14:41:46 +0200177 $_c = $this->config->item('controller_trigger');
178 if ( ! empty($_GET[$_c]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 {
Andrey Andreev30d53242014-01-16 14:41:46 +0200180 $this->set_class(trim($this->uri->filter_uri(trim($_GET[$_c]))));
181
182 $_f = $this->config->item('function_trigger');
183 if ( ! empty($_GET[$_f]))
184 {
185 $this->set_method(trim($this->uri->filter_uri($_GET[$_f])));
186 }
187
188 $this->uri->rsegments = array(
189 1 => $this->class,
190 2 => $this->method
191 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 }
Andrey Andreev30d53242014-01-16 14:41:46 +0200193 else
194 {
195 $this->_set_default_controller();
196 }
197
198 // Routing rules don't apply to query strings and we don't need to detect
199 // directories, so we're done here
200 return;
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 }
Barry Mienydd671972010-10-04 16:33:58 +0200202
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 // Load the routes.php file.
Andrey Andreev06879112013-01-29 15:05:02 +0200204 if (file_exists(APPPATH.'config/routes.php'))
Greg Akerd96f8822011-12-27 16:23:47 -0600205 {
206 include(APPPATH.'config/routes.php');
207 }
David Behler07b53422011-08-15 00:25:06 +0200208
Andrey Andreev06879112013-01-29 15:05:02 +0200209 if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/routes.php'))
210 {
211 include(APPPATH.'config/'.ENVIRONMENT.'/routes.php');
212 }
213
Andrey Andreev08fec7b2013-07-19 16:25:51 +0300214 // Validate & get reserved routes
215 if (isset($route) && is_array($route))
216 {
217 isset($route['default_controller']) && $this->default_controller = $route['default_controller'];
218 isset($route['translate_uri_dashes']) && $this->translate_uri_dashes = $route['translate_uri_dashes'];
219 unset($route['default_controller'], $route['translate_uri_dashes']);
220 $this->routes = $route;
221 }
Barry Mienydd671972010-10-04 16:33:58 +0200222
Andrey Andreev30d53242014-01-16 14:41:46 +0200223 // Is there anything to parse?
224 if ($this->uri->uri_string !== '')
Derek Jonesc7738402010-03-02 13:55:13 -0600225 {
Andrey Andreev30d53242014-01-16 14:41:46 +0200226 $this->_parse_routes();
227 }
228 else
229 {
230 $this->_set_default_controller();
231 }
232 }
233
234 // --------------------------------------------------------------------
235
236 /**
237 * Set request route
238 *
239 * Takes an array of URI segments as input and sets the class/method
240 * to be called.
241 *
242 * @used-by CI_Router::_parse_routes()
243 * @param array $segments URI segments
244 * @return void
245 */
246 protected function _set_request($segments = array())
247 {
248 $segments = $this->_validate_request($segments);
249 // If we don't have any segments left - try the default controller;
250 // WARNING: Directories get shifted out of the segments array!
251 if (empty($segments))
252 {
253 $this->_set_default_controller();
254 return;
Derek Jonesc7738402010-03-02 13:55:13 -0600255 }
Barry Mienydd671972010-10-04 16:33:58 +0200256
Andrey Andreev30d53242014-01-16 14:41:46 +0200257 if ($this->translate_uri_dashes === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 {
Andrey Andreev30d53242014-01-16 14:41:46 +0200259 $segments[0] = str_replace('-', '_', $segments[0]);
260 if (isset($segments[1]))
261 {
262 $segments[1] = str_replace('-', '_', $segments[1]);
263 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 }
Barry Mienydd671972010-10-04 16:33:58 +0200265
Andrey Andreev30d53242014-01-16 14:41:46 +0200266 $this->set_class($segments[0]);
267 if (isset($segments[1]))
268 {
269 $this->set_method($segments[1]);
270 }
Andrey Andreev9cab4272014-03-30 18:58:23 +0300271 else
272 {
273 $segments[1] = 'index';
274 }
Andrey Andreev30d53242014-01-16 14:41:46 +0200275
276 array_unshift($segments, NULL);
277 unset($segments[0]);
278 $this->uri->rsegments = $segments;
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 }
Derek Jonesc7738402010-03-02 13:55:13 -0600280
281 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200282
Derek Jonesc7738402010-03-02 13:55:13 -0600283 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300284 * Set default controller
Derek Jonesc7738402010-03-02 13:55:13 -0600285 *
Derek Jonesc7738402010-03-02 13:55:13 -0600286 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200287 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200288 protected function _set_default_controller()
Derek Jonesc7738402010-03-02 13:55:13 -0600289 {
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200290 if (empty($this->default_controller))
Derek Jonesc7738402010-03-02 13:55:13 -0600291 {
Andrey Andreevba6c0412012-01-07 21:10:09 +0200292 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 -0600293 }
Andrey Andreevd1097a12012-11-01 19:55:42 +0200294
Derek Jonesc7738402010-03-02 13:55:13 -0600295 // Is the method being specified?
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200296 if (sscanf($this->default_controller, '%[^/]/%s', $class, $method) !== 2)
Derek Jonesc7738402010-03-02 13:55:13 -0600297 {
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200298 $method = 'index';
Barry Mienydd671972010-10-04 16:33:58 +0200299 }
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200300
Andrey Andreev30d53242014-01-16 14:41:46 +0200301 if ( ! file_exists(APPPATH.'controllers/'.$this->directory.ucfirst($class).'.php'))
302 {
303 // This will trigger 404 later
304 return;
305 }
Barry Mienydd671972010-10-04 16:33:58 +0200306
Andrey Andreev30d53242014-01-16 14:41:46 +0200307 $this->set_class($class);
308 $this->set_method($method);
309
310 // Assign routed segments, index starting from 1
311 $this->uri->rsegments = array(
312 1 => $class,
313 2 => $method
314 );
Barry Mienydd671972010-10-04 16:33:58 +0200315
Andrey Andreevba6c0412012-01-07 21:10:09 +0200316 log_message('debug', 'No URI present. Default controller set.');
Derek Jonesc7738402010-03-02 13:55:13 -0600317 }
Barry Mienydd671972010-10-04 16:33:58 +0200318
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200320
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300322 * Validate request
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300324 * Attempts validate the URI request and determine the controller path.
325 *
Andrey Andreev30d53242014-01-16 14:41:46 +0200326 * @used-by CI_Router::_set_request()
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300327 * @param array $segments URI segments
Andrey Andreev30d53242014-01-16 14:41:46 +0200328 * @return mixed URI segments
Barry Mienydd671972010-10-04 16:33:58 +0200329 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200330 protected function _validate_request($segments)
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 {
Andrey Andreev30d53242014-01-16 14:41:46 +0200332 $c = count($segments);
333 // Loop through our segments and return as soon as a controller
334 // is found or when such a directory doesn't exist
335 while ($c-- > 0)
Derek Jonesc7738402010-03-02 13:55:13 -0600336 {
Andrey Andreev30d53242014-01-16 14:41:46 +0200337 $test = $this->directory
338 .ucfirst($this->translate_uri_dashes === TRUE ? str_replace('-', '_', $segments[0]) : $segments[0]);
Barry Mienydd671972010-10-04 16:33:58 +0200339
Andrey Andreev30d53242014-01-16 14:41:46 +0200340 if ( ! file_exists(APPPATH.'controllers/'.$test.'.php') && is_dir(APPPATH.'controllers/'.$this->directory.$segments[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 {
Andrey Andreev30d53242014-01-16 14:41:46 +0200342 $this->set_directory(array_shift($segments), TRUE);
343 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 }
Barry Mienydd671972010-10-04 16:33:58 +0200345
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 return $segments;
347 }
Barry Mienydd671972010-10-04 16:33:58 +0200348
Andrey Andreev30d53242014-01-16 14:41:46 +0200349 // This means that all segments were actually directories
350 return $segments;
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 }
Barry Mienydd671972010-10-04 16:33:58 +0200352
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200354
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 /**
Andrey Andreev7b53d042012-03-26 23:02:32 +0300356 * Parse Routes
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300358 * Matches any routes that may exist in the config/routes.php file
359 * against the URI to determine if the class/method need to be remapped.
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 * @return void
362 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200363 protected function _parse_routes()
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100364 {
365 // Turn the segment array into a URI string
366 $uri = implode('/', $this->uri->segments);
Barry Mienydd671972010-10-04 16:33:58 +0200367
Fatih Kalifa442b50b2013-11-01 02:44:56 +0700368 // Get HTTP verb
Andrey Andreevc761a202013-11-11 14:02:15 +0200369 $http_verb = isset($_SERVER['REQUEST_METHOD']) ? strtolower($_SERVER['REQUEST_METHOD']) : 'cli';
Fatih Kalifa442b50b2013-11-01 02:44:56 +0700370
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100371 // Is there a literal match? If so we're done
Fatih Kalifa0b58b3c2013-11-05 15:36:40 +0700372 if (isset($this->routes[$uri]))
Fatih Kalifa442b50b2013-11-01 02:44:56 +0700373 {
Fatih Kalifa0b58b3c2013-11-05 15:36:40 +0700374 // Check default routes format
375 if (is_string($this->routes[$uri]))
376 {
Andrey Andreev30d53242014-01-16 14:41:46 +0200377 $this->_set_request(explode('/', $this->routes[$uri]));
378 return;
Fatih Kalifa0b58b3c2013-11-05 15:36:40 +0700379 }
Andrey Andreevc761a202013-11-11 14:02:15 +0200380 // Is there a matching http verb?
Fatih Kalifa0b58b3c2013-11-05 15:36:40 +0700381 elseif (is_array($this->routes[$uri]) && isset($this->routes[$uri][$http_verb]))
382 {
Andrey Andreev30d53242014-01-16 14:41:46 +0200383 $this->_set_request(explode('/', $this->routes[$uri][$http_verb]));
384 return;
Fatih Kalifa0b58b3c2013-11-05 15:36:40 +0700385 }
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100386 }
Barry Mienydd671972010-10-04 16:33:58 +0200387
vlakoffc941d852013-08-06 14:44:40 +0200388 // Loop through the route array looking for wildcards
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100389 foreach ($this->routes as $key => $val)
390 {
Fatih Kalifa0b58b3c2013-11-05 15:36:40 +0700391 // Check if route format is using http verb
Fatih Kalifa442b50b2013-11-01 02:44:56 +0700392 if (is_array($val))
393 {
Fatih Kalifa442b50b2013-11-01 02:44:56 +0700394 if (isset($val[$http_verb]))
395 {
396 $val = $val[$http_verb];
397 }
Fatih Kalifa442b50b2013-11-01 02:44:56 +0700398 else
399 {
Fatih Kalifa442b50b2013-11-01 02:44:56 +0700400 continue;
401 }
402 }
403
vlakoffc941d852013-08-06 14:44:40 +0200404 // Convert wildcards to RegEx
Andrey Andreev7676c2d2012-10-30 13:42:01 +0200405 $key = str_replace(array(':any', ':num'), array('[^/]+', '[0-9]+'), $key);
Derek Jonesc7738402010-03-02 13:55:13 -0600406
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100407 // Does the RegEx match?
408 if (preg_match('#^'.$key.'$#', $uri, $matches))
409 {
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100410 // Are we using callbacks to process back-references?
Andrey Andreevda5562a2012-11-08 12:34:38 +0200411 if ( ! is_string($val) && is_callable($val))
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100412 {
413 // Remove the original string from the matches array.
414 array_shift($matches);
Derek Allard2067d1a2008-11-13 22:59:24 +0000415
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100416 // Execute the callback using the values in matches as its parameters.
417 $val = call_user_func_array($val, $matches);
418 }
Andrey Andreevda5562a2012-11-08 12:34:38 +0200419 // Are we using the default routing method for back-references?
420 elseif (strpos($val, '$') !== FALSE && strpos($key, '(') !== FALSE)
421 {
422 $val = preg_replace('#^'.$key.'$#', $val, $uri);
423 }
Jonatas Miguelc0d98b22012-08-06 15:42:50 +0100424
Andrey Andreev30d53242014-01-16 14:41:46 +0200425 $this->_set_request(explode('/', $val));
426 return;
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100427 }
428 }
Jonatas Miguelc0d98b22012-08-06 15:42:50 +0100429
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100430 // If we got this far it means we didn't encounter a
431 // matching route so we'll set the site default route
Andrey Andreeva9237cb2014-01-18 19:07:20 +0200432 $this->_set_request(array_values($this->uri->segments));
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100433 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000434
435 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200436
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300438 * Set class name
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300440 * @param string $class Class name
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200442 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200443 public function set_class($class)
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 {
Derek Jones2615e412010-10-06 17:51:16 -0500445 $this->class = str_replace(array('/', '.'), '', $class);
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 }
Barry Mienydd671972010-10-04 16:33:58 +0200447
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200449
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 /**
451 * Fetch the current class
452 *
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300453 * @deprecated 3.0.0 Read the 'class' property instead
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200455 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200456 public function fetch_class()
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 {
458 return $this->class;
459 }
Barry Mienydd671972010-10-04 16:33:58 +0200460
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200462
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300464 * Set method name
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300466 * @param string $method Method name
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200468 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200469 public function set_method($method)
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 {
471 $this->method = $method;
472 }
473
474 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200475
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 /**
Andrey Andreev7b53d042012-03-26 23:02:32 +0300477 * Fetch the current method
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 *
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300479 * @deprecated 3.0.0 Read the 'method' property instead
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200481 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200482 public function fetch_method()
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 {
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300484 return $this->method;
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 }
486
487 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200488
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300490 * Set directory name
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300492 * @param string $dir Directory name
Andrey Andreev30d53242014-01-16 14:41:46 +0200493 * @param bool $appent Whether we're appending rather then setting the full value
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200495 */
Andrey Andreev30d53242014-01-16 14:41:46 +0200496 public function set_directory($dir, $append = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 {
Andrey Andreev30d53242014-01-16 14:41:46 +0200498 if ($append !== TRUE OR empty($this->directory))
499 {
500 $this->directory = str_replace('.', '', trim($dir, '/')).'/';
501 }
502 else
503 {
504 $this->directory .= str_replace('.', '', trim($dir, '/')).'/';
505 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 }
507
508 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200509
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300511 * Fetch directory
512 *
513 * Feches the sub-directory (if any) that contains the requested
514 * controller class.
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 *
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300516 * @deprecated 3.0.0 Read the 'directory' property instead
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200518 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200519 public function fetch_directory()
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 {
521 return $this->directory;
522 }
523
524}
Derek Allard2067d1a2008-11-13 22:59:24 +0000525
526/* End of file Router.php */
Andrey Andreev7b53d042012-03-26 23:02:32 +0300527/* Location: ./system/core/Router.php */