blob: 1bb18b0885ed916880cac881fda2a34b5cc29803 [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 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreevba6c0412012-01-07 21:10:09 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreevba6c0412012-01-07 21:10:09 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
darwinel871754a2014-02-11 17:34:57 +010021 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * Router Class
31 *
32 * Parses URIs and determines routing
33 *
34 * @package CodeIgniter
35 * @subpackage Libraries
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @category Libraries
Andrey Andreev92ebfb62012-05-17 12:49:24 +030037 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000038 * @link http://codeigniter.com/user_guide/general/routing.html
39 */
40class CI_Router {
41
David Behler07b53422011-08-15 00:25:06 +020042 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +030043 * CI_Config class object
David Behler07b53422011-08-15 00:25:06 +020044 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +030045 * @var object
David Behler07b53422011-08-15 00:25:06 +020046 */
Andrey Andreevba6c0412012-01-07 21:10:09 +020047 public $config;
Andrey Andreev92ebfb62012-05-17 12:49:24 +030048
David Behler07b53422011-08-15 00:25:06 +020049 /**
50 * List of routes
51 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +030052 * @var array
David Behler07b53422011-08-15 00:25:06 +020053 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040054 public $routes = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +030055
David Behler07b53422011-08-15 00:25:06 +020056 /**
David Behler07b53422011-08-15 00:25:06 +020057 * Current class name
58 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +030059 * @var string
David Behler07b53422011-08-15 00:25:06 +020060 */
Andrey Andreev92ebfb62012-05-17 12:49:24 +030061 public $class = '';
62
David Behler07b53422011-08-15 00:25:06 +020063 /**
64 * Current method name
65 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +030066 * @var string
David Behler07b53422011-08-15 00:25:06 +020067 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040068 public $method = 'index';
Andrey Andreev92ebfb62012-05-17 12:49:24 +030069
David Behler07b53422011-08-15 00:25:06 +020070 /**
71 * Sub-directory that contains the requested controller class
72 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +030073 * @var string
David Behler07b53422011-08-15 00:25:06 +020074 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040075 public $directory = '';
Andrey Andreev92ebfb62012-05-17 12:49:24 +030076
David Behler07b53422011-08-15 00:25:06 +020077 /**
78 * Default controller (and method if specific)
79 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +030080 * @var string
David Behler07b53422011-08-15 00:25:06 +020081 */
Andrey Andreevba6c0412012-01-07 21:10:09 +020082 public $default_controller;
Barry Mienydd671972010-10-04 16:33:58 +020083
Derek Allard2067d1a2008-11-13 22:59:24 +000084 /**
Andrey Andreev08fec7b2013-07-19 16:25:51 +030085 * Translate URI dashes
86 *
87 * Determines whether dashes in controller & method segments
88 * should be automatically replaced by underscores.
89 *
90 * @var bool
91 */
92 public $translate_uri_dashes = FALSE;
93
Andrey Andreev30d53242014-01-16 14:41:46 +020094 /**
95 * Enable query strings flag
96 *
97 * Determines wether to use GET parameters or segment URIs
98 *
99 * @var bool
100 */
101 public $enable_query_strings = FALSE;
102
Andrey Andreev08fec7b2013-07-19 16:25:51 +0300103 // --------------------------------------------------------------------
104
105 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300106 * Class constructor
Derek Allard2067d1a2008-11-13 22:59:24 +0000107 *
108 * Runs the route mapping function.
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300109 *
110 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 */
Andrey Andreevc26b9eb2014-02-24 11:31:36 +0200112 public function __construct($routing = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 {
Derek Jonesc7738402010-03-02 13:55:13 -0600114 $this->config =& load_class('Config', 'core');
115 $this->uri =& load_class('URI', 'core');
Andrey Andreev30d53242014-01-16 14:41:46 +0200116
117 $this->enable_query_strings = ( ! is_cli() && $this->config->item('enable_query_strings') === TRUE);
Andrey Andreev08fec7b2013-07-19 16:25:51 +0300118 $this->_set_routing();
Andrey Andreev4e6c5282014-01-10 19:29:49 +0200119
120 // Set any routing overrides that may exist in the main index file
Andrey Andreevc26b9eb2014-02-24 11:31:36 +0200121 if (is_array($routing))
Andrey Andreev4e6c5282014-01-10 19:29:49 +0200122 {
123 if (isset($routing['directory']))
124 {
125 $this->set_directory($routing['directory']);
126 }
127
128 if ( ! empty($routing['controller']))
129 {
130 $this->set_class($routing['controller']);
131 }
132
Andrey Andreevbc89b3c2014-04-15 17:46:05 +0300133 if ( ! empty($routing['function']))
Andrey Andreev4e6c5282014-01-10 19:29:49 +0200134 {
Andrey Andreev4e6c5282014-01-10 19:29:49 +0200135 $this->set_method($routing['function']);
136 }
137 }
138
Andrey Andreevba6c0412012-01-07 21:10:09 +0200139 log_message('debug', 'Router Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 }
Barry Mienydd671972010-10-04 16:33:58 +0200141
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200143
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300145 * Set route mapping
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300147 * Determines what should be served based on the URI request,
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 * as well as any "routes" that have been set in the routing config file.
149 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 * @return void
151 */
Andrey Andreev08fec7b2013-07-19 16:25:51 +0300152 protected function _set_routing()
Barry Mienydd671972010-10-04 16:33:58 +0200153 {
Andrey Andreevba6c0412012-01-07 21:10:09 +0200154 // Are query strings enabled in the config file? Normally CI doesn't utilize query strings
Barry Mienydd671972010-10-04 16:33:58 +0200155 // since URI segments are more search-engine friendly, but they can optionally be used.
Derek Jonesc7738402010-03-02 13:55:13 -0600156 // If this feature is enabled, we will gather the directory/class/method a little differently
Andrey Andreev30d53242014-01-16 14:41:46 +0200157 if ($this->enable_query_strings)
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 {
Andrey Andreev30d53242014-01-16 14:41:46 +0200159 $_d = $this->config->item('directory_trigger');
160 $_d = isset($_GET[$_d]) ? trim($_GET[$_d], " \t\n\r\0\x0B/") : '';
161 if ($_d !== '')
Derek Jonesc7738402010-03-02 13:55:13 -0600162 {
Andrey Andreev30d53242014-01-16 14:41:46 +0200163 $this->set_directory($this->uri->filter_uri($_d));
Derek Jonesc7738402010-03-02 13:55:13 -0600164 }
Barry Mienydd671972010-10-04 16:33:58 +0200165
Andrey Andreev30d53242014-01-16 14:41:46 +0200166 $_c = $this->config->item('controller_trigger');
167 if ( ! empty($_GET[$_c]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 {
Andrey Andreev30d53242014-01-16 14:41:46 +0200169 $this->set_class(trim($this->uri->filter_uri(trim($_GET[$_c]))));
170
171 $_f = $this->config->item('function_trigger');
172 if ( ! empty($_GET[$_f]))
173 {
174 $this->set_method(trim($this->uri->filter_uri($_GET[$_f])));
175 }
176
177 $this->uri->rsegments = array(
178 1 => $this->class,
179 2 => $this->method
180 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 }
Andrey Andreev30d53242014-01-16 14:41:46 +0200182 else
183 {
184 $this->_set_default_controller();
185 }
186
187 // Routing rules don't apply to query strings and we don't need to detect
188 // directories, so we're done here
189 return;
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 }
Barry Mienydd671972010-10-04 16:33:58 +0200191
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 // Load the routes.php file.
Andrey Andreev06879112013-01-29 15:05:02 +0200193 if (file_exists(APPPATH.'config/routes.php'))
Greg Akerd96f8822011-12-27 16:23:47 -0600194 {
195 include(APPPATH.'config/routes.php');
196 }
David Behler07b53422011-08-15 00:25:06 +0200197
Andrey Andreev06879112013-01-29 15:05:02 +0200198 if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/routes.php'))
199 {
200 include(APPPATH.'config/'.ENVIRONMENT.'/routes.php');
201 }
202
Andrey Andreev08fec7b2013-07-19 16:25:51 +0300203 // Validate & get reserved routes
204 if (isset($route) && is_array($route))
205 {
206 isset($route['default_controller']) && $this->default_controller = $route['default_controller'];
207 isset($route['translate_uri_dashes']) && $this->translate_uri_dashes = $route['translate_uri_dashes'];
208 unset($route['default_controller'], $route['translate_uri_dashes']);
209 $this->routes = $route;
210 }
Barry Mienydd671972010-10-04 16:33:58 +0200211
Andrey Andreev30d53242014-01-16 14:41:46 +0200212 // Is there anything to parse?
213 if ($this->uri->uri_string !== '')
Derek Jonesc7738402010-03-02 13:55:13 -0600214 {
Andrey Andreev30d53242014-01-16 14:41:46 +0200215 $this->_parse_routes();
216 }
217 else
218 {
219 $this->_set_default_controller();
220 }
221 }
222
223 // --------------------------------------------------------------------
224
225 /**
226 * Set request route
227 *
228 * Takes an array of URI segments as input and sets the class/method
229 * to be called.
230 *
231 * @used-by CI_Router::_parse_routes()
232 * @param array $segments URI segments
233 * @return void
234 */
235 protected function _set_request($segments = array())
236 {
237 $segments = $this->_validate_request($segments);
238 // If we don't have any segments left - try the default controller;
239 // WARNING: Directories get shifted out of the segments array!
240 if (empty($segments))
241 {
242 $this->_set_default_controller();
243 return;
Derek Jonesc7738402010-03-02 13:55:13 -0600244 }
Barry Mienydd671972010-10-04 16:33:58 +0200245
Andrey Andreev30d53242014-01-16 14:41:46 +0200246 if ($this->translate_uri_dashes === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 {
Andrey Andreev30d53242014-01-16 14:41:46 +0200248 $segments[0] = str_replace('-', '_', $segments[0]);
249 if (isset($segments[1]))
250 {
251 $segments[1] = str_replace('-', '_', $segments[1]);
252 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 }
Barry Mienydd671972010-10-04 16:33:58 +0200254
Andrey Andreev30d53242014-01-16 14:41:46 +0200255 $this->set_class($segments[0]);
256 if (isset($segments[1]))
257 {
258 $this->set_method($segments[1]);
259 }
Andrey Andreev9cab4272014-03-30 18:58:23 +0300260 else
261 {
262 $segments[1] = 'index';
263 }
Andrey Andreev30d53242014-01-16 14:41:46 +0200264
265 array_unshift($segments, NULL);
266 unset($segments[0]);
267 $this->uri->rsegments = $segments;
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 }
Derek Jonesc7738402010-03-02 13:55:13 -0600269
270 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200271
Derek Jonesc7738402010-03-02 13:55:13 -0600272 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300273 * Set default controller
Derek Jonesc7738402010-03-02 13:55:13 -0600274 *
Derek Jonesc7738402010-03-02 13:55:13 -0600275 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200276 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200277 protected function _set_default_controller()
Derek Jonesc7738402010-03-02 13:55:13 -0600278 {
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200279 if (empty($this->default_controller))
Derek Jonesc7738402010-03-02 13:55:13 -0600280 {
Andrey Andreevba6c0412012-01-07 21:10:09 +0200281 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 -0600282 }
Andrey Andreevd1097a12012-11-01 19:55:42 +0200283
Derek Jonesc7738402010-03-02 13:55:13 -0600284 // Is the method being specified?
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200285 if (sscanf($this->default_controller, '%[^/]/%s', $class, $method) !== 2)
Derek Jonesc7738402010-03-02 13:55:13 -0600286 {
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200287 $method = 'index';
Barry Mienydd671972010-10-04 16:33:58 +0200288 }
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200289
Andrey Andreev30d53242014-01-16 14:41:46 +0200290 if ( ! file_exists(APPPATH.'controllers/'.$this->directory.ucfirst($class).'.php'))
291 {
292 // This will trigger 404 later
293 return;
294 }
Barry Mienydd671972010-10-04 16:33:58 +0200295
Andrey Andreev30d53242014-01-16 14:41:46 +0200296 $this->set_class($class);
297 $this->set_method($method);
298
299 // Assign routed segments, index starting from 1
300 $this->uri->rsegments = array(
301 1 => $class,
302 2 => $method
303 );
Barry Mienydd671972010-10-04 16:33:58 +0200304
Andrey Andreevba6c0412012-01-07 21:10:09 +0200305 log_message('debug', 'No URI present. Default controller set.');
Derek Jonesc7738402010-03-02 13:55:13 -0600306 }
Barry Mienydd671972010-10-04 16:33:58 +0200307
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200309
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300311 * Validate request
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300313 * Attempts validate the URI request and determine the controller path.
314 *
Andrey Andreev30d53242014-01-16 14:41:46 +0200315 * @used-by CI_Router::_set_request()
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300316 * @param array $segments URI segments
Andrey Andreev30d53242014-01-16 14:41:46 +0200317 * @return mixed URI segments
Barry Mienydd671972010-10-04 16:33:58 +0200318 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200319 protected function _validate_request($segments)
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 {
Andrey Andreev30d53242014-01-16 14:41:46 +0200321 $c = count($segments);
322 // Loop through our segments and return as soon as a controller
323 // is found or when such a directory doesn't exist
324 while ($c-- > 0)
Derek Jonesc7738402010-03-02 13:55:13 -0600325 {
Andrey Andreev30d53242014-01-16 14:41:46 +0200326 $test = $this->directory
327 .ucfirst($this->translate_uri_dashes === TRUE ? str_replace('-', '_', $segments[0]) : $segments[0]);
Barry Mienydd671972010-10-04 16:33:58 +0200328
Andrey Andreev30d53242014-01-16 14:41:46 +0200329 if ( ! file_exists(APPPATH.'controllers/'.$test.'.php') && is_dir(APPPATH.'controllers/'.$this->directory.$segments[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 {
Andrey Andreev30d53242014-01-16 14:41:46 +0200331 $this->set_directory(array_shift($segments), TRUE);
332 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 }
Barry Mienydd671972010-10-04 16:33:58 +0200334
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 return $segments;
336 }
Barry Mienydd671972010-10-04 16:33:58 +0200337
Andrey Andreev30d53242014-01-16 14:41:46 +0200338 // This means that all segments were actually directories
339 return $segments;
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 }
Barry Mienydd671972010-10-04 16:33:58 +0200341
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200343
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 /**
Andrey Andreev7b53d042012-03-26 23:02:32 +0300345 * Parse Routes
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300347 * Matches any routes that may exist in the config/routes.php file
348 * against the URI to determine if the class/method need to be remapped.
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 * @return void
351 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200352 protected function _parse_routes()
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100353 {
354 // Turn the segment array into a URI string
355 $uri = implode('/', $this->uri->segments);
Barry Mienydd671972010-10-04 16:33:58 +0200356
Fatih Kalifa442b50b2013-11-01 02:44:56 +0700357 // Get HTTP verb
Andrey Andreevc761a202013-11-11 14:02:15 +0200358 $http_verb = isset($_SERVER['REQUEST_METHOD']) ? strtolower($_SERVER['REQUEST_METHOD']) : 'cli';
Fatih Kalifa442b50b2013-11-01 02:44:56 +0700359
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100360 // Is there a literal match? If so we're done
Fatih Kalifa0b58b3c2013-11-05 15:36:40 +0700361 if (isset($this->routes[$uri]))
Fatih Kalifa442b50b2013-11-01 02:44:56 +0700362 {
Fatih Kalifa0b58b3c2013-11-05 15:36:40 +0700363 // Check default routes format
364 if (is_string($this->routes[$uri]))
365 {
Andrey Andreev30d53242014-01-16 14:41:46 +0200366 $this->_set_request(explode('/', $this->routes[$uri]));
367 return;
Fatih Kalifa0b58b3c2013-11-05 15:36:40 +0700368 }
Andrey Andreevc761a202013-11-11 14:02:15 +0200369 // Is there a matching http verb?
Fatih Kalifa0b58b3c2013-11-05 15:36:40 +0700370 elseif (is_array($this->routes[$uri]) && isset($this->routes[$uri][$http_verb]))
371 {
Andrey Andreev30d53242014-01-16 14:41:46 +0200372 $this->_set_request(explode('/', $this->routes[$uri][$http_verb]));
373 return;
Fatih Kalifa0b58b3c2013-11-05 15:36:40 +0700374 }
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100375 }
Barry Mienydd671972010-10-04 16:33:58 +0200376
vlakoffc941d852013-08-06 14:44:40 +0200377 // Loop through the route array looking for wildcards
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100378 foreach ($this->routes as $key => $val)
379 {
Fatih Kalifa0b58b3c2013-11-05 15:36:40 +0700380 // Check if route format is using http verb
Fatih Kalifa442b50b2013-11-01 02:44:56 +0700381 if (is_array($val))
382 {
Fatih Kalifa442b50b2013-11-01 02:44:56 +0700383 if (isset($val[$http_verb]))
384 {
385 $val = $val[$http_verb];
386 }
Fatih Kalifa442b50b2013-11-01 02:44:56 +0700387 else
388 {
Fatih Kalifa442b50b2013-11-01 02:44:56 +0700389 continue;
390 }
391 }
392
vlakoffc941d852013-08-06 14:44:40 +0200393 // Convert wildcards to RegEx
Andrey Andreev7676c2d2012-10-30 13:42:01 +0200394 $key = str_replace(array(':any', ':num'), array('[^/]+', '[0-9]+'), $key);
Derek Jonesc7738402010-03-02 13:55:13 -0600395
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100396 // Does the RegEx match?
397 if (preg_match('#^'.$key.'$#', $uri, $matches))
398 {
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100399 // Are we using callbacks to process back-references?
Andrey Andreevda5562a2012-11-08 12:34:38 +0200400 if ( ! is_string($val) && is_callable($val))
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100401 {
402 // Remove the original string from the matches array.
403 array_shift($matches);
Derek Allard2067d1a2008-11-13 22:59:24 +0000404
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100405 // Execute the callback using the values in matches as its parameters.
406 $val = call_user_func_array($val, $matches);
407 }
Andrey Andreevda5562a2012-11-08 12:34:38 +0200408 // Are we using the default routing method for back-references?
409 elseif (strpos($val, '$') !== FALSE && strpos($key, '(') !== FALSE)
410 {
411 $val = preg_replace('#^'.$key.'$#', $val, $uri);
412 }
Jonatas Miguelc0d98b22012-08-06 15:42:50 +0100413
Andrey Andreev30d53242014-01-16 14:41:46 +0200414 $this->_set_request(explode('/', $val));
415 return;
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100416 }
417 }
Jonatas Miguelc0d98b22012-08-06 15:42:50 +0100418
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100419 // If we got this far it means we didn't encounter a
420 // matching route so we'll set the site default route
Andrey Andreeva9237cb2014-01-18 19:07:20 +0200421 $this->_set_request(array_values($this->uri->segments));
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100422 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000423
424 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200425
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300427 * Set class name
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300429 * @param string $class Class name
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200431 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200432 public function set_class($class)
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 {
Derek Jones2615e412010-10-06 17:51:16 -0500434 $this->class = str_replace(array('/', '.'), '', $class);
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 }
Barry Mienydd671972010-10-04 16:33:58 +0200436
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200438
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 /**
440 * Fetch the current class
441 *
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300442 * @deprecated 3.0.0 Read the 'class' property instead
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200444 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200445 public function fetch_class()
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 {
447 return $this->class;
448 }
Barry Mienydd671972010-10-04 16:33:58 +0200449
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200451
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300453 * Set method name
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300455 * @param string $method Method name
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200457 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200458 public function set_method($method)
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 {
460 $this->method = $method;
461 }
462
463 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200464
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 /**
Andrey Andreev7b53d042012-03-26 23:02:32 +0300466 * Fetch the current method
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 *
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300468 * @deprecated 3.0.0 Read the 'method' property instead
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200470 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200471 public function fetch_method()
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 {
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300473 return $this->method;
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 }
475
476 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200477
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300479 * Set directory name
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300481 * @param string $dir Directory name
Andrey Andreev30d53242014-01-16 14:41:46 +0200482 * @param bool $appent Whether we're appending rather then setting the full value
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200484 */
Andrey Andreev30d53242014-01-16 14:41:46 +0200485 public function set_directory($dir, $append = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 {
Andrey Andreev30d53242014-01-16 14:41:46 +0200487 if ($append !== TRUE OR empty($this->directory))
488 {
489 $this->directory = str_replace('.', '', trim($dir, '/')).'/';
490 }
491 else
492 {
493 $this->directory .= str_replace('.', '', trim($dir, '/')).'/';
494 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 }
496
497 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200498
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300500 * Fetch directory
501 *
502 * Feches the sub-directory (if any) that contains the requested
503 * controller class.
Derek Allard2067d1a2008-11-13 22:59:24 +0000504 *
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300505 * @deprecated 3.0.0 Read the 'directory' property instead
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200507 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200508 public function fetch_directory()
Derek Allard2067d1a2008-11-13 22:59:24 +0000509 {
510 return $this->directory;
511 }
512
513}
Derek Allard2067d1a2008-11-13 22:59:24 +0000514
515/* End of file Router.php */
Andrey Andreev7b53d042012-03-26 23:02:32 +0300516/* Location: ./system/core/Router.php */