blob: e3c911511f98b56dd5db2333682c31e74c9d0025 [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
Andrey Andreev80500af2013-01-01 08:16:53 +020021 * @copyright Copyright (c) 2008 - 2013, 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 Andreevba6c0412012-01-07 21:10:09 +0200112 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 {
Andrey Andreev4e6c5282014-01-10 19:29:49 +0200114 global $routing;
115
Derek Jonesc7738402010-03-02 13:55:13 -0600116 $this->config =& load_class('Config', 'core');
117 $this->uri =& load_class('URI', 'core');
Andrey Andreev30d53242014-01-16 14:41:46 +0200118
119 $this->enable_query_strings = ( ! is_cli() && $this->config->item('enable_query_strings') === TRUE);
Andrey Andreev08fec7b2013-07-19 16:25:51 +0300120 $this->_set_routing();
Andrey Andreev4e6c5282014-01-10 19:29:49 +0200121
122 // Set any routing overrides that may exist in the main index file
123 if (isset($routing) && is_array($routing))
124 {
125 if (isset($routing['directory']))
126 {
127 $this->set_directory($routing['directory']);
128 }
129
130 if ( ! empty($routing['controller']))
131 {
132 $this->set_class($routing['controller']);
133 }
134
135 if (isset($routing['function']))
136 {
137 $routing['function'] = empty($routing['function']) ? 'index' : $routing['function'];
138 $this->set_method($routing['function']);
139 }
140 }
141
Andrey Andreevba6c0412012-01-07 21:10:09 +0200142 log_message('debug', 'Router Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 }
Barry Mienydd671972010-10-04 16:33:58 +0200144
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200146
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300148 * Set route mapping
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300150 * Determines what should be served based on the URI request,
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 * as well as any "routes" that have been set in the routing config file.
152 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000153 * @return void
154 */
Andrey Andreev08fec7b2013-07-19 16:25:51 +0300155 protected function _set_routing()
Barry Mienydd671972010-10-04 16:33:58 +0200156 {
Andrey Andreevba6c0412012-01-07 21:10:09 +0200157 // Are query strings enabled in the config file? Normally CI doesn't utilize query strings
Barry Mienydd671972010-10-04 16:33:58 +0200158 // since URI segments are more search-engine friendly, but they can optionally be used.
Derek Jonesc7738402010-03-02 13:55:13 -0600159 // If this feature is enabled, we will gather the directory/class/method a little differently
Andrey Andreev30d53242014-01-16 14:41:46 +0200160 if ($this->enable_query_strings)
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 {
Andrey Andreev30d53242014-01-16 14:41:46 +0200162 $_d = $this->config->item('directory_trigger');
163 $_d = isset($_GET[$_d]) ? trim($_GET[$_d], " \t\n\r\0\x0B/") : '';
164 if ($_d !== '')
Derek Jonesc7738402010-03-02 13:55:13 -0600165 {
Andrey Andreev30d53242014-01-16 14:41:46 +0200166 $this->set_directory($this->uri->filter_uri($_d));
Derek Jonesc7738402010-03-02 13:55:13 -0600167 }
Barry Mienydd671972010-10-04 16:33:58 +0200168
Andrey Andreev30d53242014-01-16 14:41:46 +0200169 $_c = $this->config->item('controller_trigger');
170 if ( ! empty($_GET[$_c]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 {
Andrey Andreev30d53242014-01-16 14:41:46 +0200172 $this->set_class(trim($this->uri->filter_uri(trim($_GET[$_c]))));
173
174 $_f = $this->config->item('function_trigger');
175 if ( ! empty($_GET[$_f]))
176 {
177 $this->set_method(trim($this->uri->filter_uri($_GET[$_f])));
178 }
179
180 $this->uri->rsegments = array(
181 1 => $this->class,
182 2 => $this->method
183 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 }
Andrey Andreev30d53242014-01-16 14:41:46 +0200185 else
186 {
187 $this->_set_default_controller();
188 }
189
190 // Routing rules don't apply to query strings and we don't need to detect
191 // directories, so we're done here
192 return;
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 }
Barry Mienydd671972010-10-04 16:33:58 +0200194
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 // Load the routes.php file.
Andrey Andreev06879112013-01-29 15:05:02 +0200196 if (file_exists(APPPATH.'config/routes.php'))
Greg Akerd96f8822011-12-27 16:23:47 -0600197 {
198 include(APPPATH.'config/routes.php');
199 }
David Behler07b53422011-08-15 00:25:06 +0200200
Andrey Andreev06879112013-01-29 15:05:02 +0200201 if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/routes.php'))
202 {
203 include(APPPATH.'config/'.ENVIRONMENT.'/routes.php');
204 }
205
Andrey Andreev08fec7b2013-07-19 16:25:51 +0300206 // Validate & get reserved routes
207 if (isset($route) && is_array($route))
208 {
209 isset($route['default_controller']) && $this->default_controller = $route['default_controller'];
210 isset($route['translate_uri_dashes']) && $this->translate_uri_dashes = $route['translate_uri_dashes'];
211 unset($route['default_controller'], $route['translate_uri_dashes']);
212 $this->routes = $route;
213 }
Barry Mienydd671972010-10-04 16:33:58 +0200214
Andrey Andreev30d53242014-01-16 14:41:46 +0200215 // Is there anything to parse?
216 if ($this->uri->uri_string !== '')
Derek Jonesc7738402010-03-02 13:55:13 -0600217 {
Andrey Andreev30d53242014-01-16 14:41:46 +0200218 $this->_parse_routes();
219 }
220 else
221 {
222 $this->_set_default_controller();
223 }
224 }
225
226 // --------------------------------------------------------------------
227
228 /**
229 * Set request route
230 *
231 * Takes an array of URI segments as input and sets the class/method
232 * to be called.
233 *
234 * @used-by CI_Router::_parse_routes()
235 * @param array $segments URI segments
236 * @return void
237 */
238 protected function _set_request($segments = array())
239 {
240 $segments = $this->_validate_request($segments);
241 // If we don't have any segments left - try the default controller;
242 // WARNING: Directories get shifted out of the segments array!
243 if (empty($segments))
244 {
245 $this->_set_default_controller();
246 return;
Derek Jonesc7738402010-03-02 13:55:13 -0600247 }
Barry Mienydd671972010-10-04 16:33:58 +0200248
Andrey Andreev30d53242014-01-16 14:41:46 +0200249 if ($this->translate_uri_dashes === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 {
Andrey Andreev30d53242014-01-16 14:41:46 +0200251 $segments[0] = str_replace('-', '_', $segments[0]);
252 if (isset($segments[1]))
253 {
254 $segments[1] = str_replace('-', '_', $segments[1]);
255 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 }
Barry Mienydd671972010-10-04 16:33:58 +0200257
Andrey Andreev30d53242014-01-16 14:41:46 +0200258 $this->set_class($segments[0]);
259 if (isset($segments[1]))
260 {
261 $this->set_method($segments[1]);
262 }
263
264 array_unshift($segments, NULL);
265 unset($segments[0]);
266 $this->uri->rsegments = $segments;
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 }
Derek Jonesc7738402010-03-02 13:55:13 -0600268
269 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200270
Derek Jonesc7738402010-03-02 13:55:13 -0600271 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300272 * Set default controller
Derek Jonesc7738402010-03-02 13:55:13 -0600273 *
Derek Jonesc7738402010-03-02 13:55:13 -0600274 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200275 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200276 protected function _set_default_controller()
Derek Jonesc7738402010-03-02 13:55:13 -0600277 {
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200278 if (empty($this->default_controller))
Derek Jonesc7738402010-03-02 13:55:13 -0600279 {
Andrey Andreevba6c0412012-01-07 21:10:09 +0200280 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 -0600281 }
Andrey Andreevd1097a12012-11-01 19:55:42 +0200282
Derek Jonesc7738402010-03-02 13:55:13 -0600283 // Is the method being specified?
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200284 if (sscanf($this->default_controller, '%[^/]/%s', $class, $method) !== 2)
Derek Jonesc7738402010-03-02 13:55:13 -0600285 {
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200286 $method = 'index';
Barry Mienydd671972010-10-04 16:33:58 +0200287 }
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200288
Andrey Andreev30d53242014-01-16 14:41:46 +0200289 if ( ! file_exists(APPPATH.'controllers/'.$this->directory.ucfirst($class).'.php'))
290 {
291 // This will trigger 404 later
292 return;
293 }
Barry Mienydd671972010-10-04 16:33:58 +0200294
Andrey Andreev30d53242014-01-16 14:41:46 +0200295 $this->set_class($class);
296 $this->set_method($method);
297
298 // Assign routed segments, index starting from 1
299 $this->uri->rsegments = array(
300 1 => $class,
301 2 => $method
302 );
Barry Mienydd671972010-10-04 16:33:58 +0200303
Andrey Andreevba6c0412012-01-07 21:10:09 +0200304 log_message('debug', 'No URI present. Default controller set.');
Derek Jonesc7738402010-03-02 13:55:13 -0600305 }
Barry Mienydd671972010-10-04 16:33:58 +0200306
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200308
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300310 * Validate request
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300312 * Attempts validate the URI request and determine the controller path.
313 *
Andrey Andreev30d53242014-01-16 14:41:46 +0200314 * @used-by CI_Router::_set_request()
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300315 * @param array $segments URI segments
Andrey Andreev30d53242014-01-16 14:41:46 +0200316 * @return mixed URI segments
Barry Mienydd671972010-10-04 16:33:58 +0200317 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200318 protected function _validate_request($segments)
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 {
Andrey Andreev30d53242014-01-16 14:41:46 +0200320 $c = count($segments);
321 // Loop through our segments and return as soon as a controller
322 // is found or when such a directory doesn't exist
323 while ($c-- > 0)
Derek Jonesc7738402010-03-02 13:55:13 -0600324 {
Andrey Andreev30d53242014-01-16 14:41:46 +0200325 $test = $this->directory
326 .ucfirst($this->translate_uri_dashes === TRUE ? str_replace('-', '_', $segments[0]) : $segments[0]);
Barry Mienydd671972010-10-04 16:33:58 +0200327
Andrey Andreev30d53242014-01-16 14:41:46 +0200328 if ( ! file_exists(APPPATH.'controllers/'.$test.'.php') && is_dir(APPPATH.'controllers/'.$this->directory.$segments[0]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 {
Andrey Andreev30d53242014-01-16 14:41:46 +0200330 $this->set_directory(array_shift($segments), TRUE);
331 continue;
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 }
Barry Mienydd671972010-10-04 16:33:58 +0200333
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 return $segments;
335 }
Barry Mienydd671972010-10-04 16:33:58 +0200336
Andrey Andreev30d53242014-01-16 14:41:46 +0200337 // This means that all segments were actually directories
338 return $segments;
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 }
Barry Mienydd671972010-10-04 16:33:58 +0200340
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200342
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 /**
Andrey Andreev7b53d042012-03-26 23:02:32 +0300344 * Parse Routes
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300346 * Matches any routes that may exist in the config/routes.php file
347 * against the URI to determine if the class/method need to be remapped.
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 * @return void
350 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200351 protected function _parse_routes()
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100352 {
353 // Turn the segment array into a URI string
354 $uri = implode('/', $this->uri->segments);
Barry Mienydd671972010-10-04 16:33:58 +0200355
Fatih Kalifa442b50b2013-11-01 02:44:56 +0700356 // Get HTTP verb
Andrey Andreevc761a202013-11-11 14:02:15 +0200357 $http_verb = isset($_SERVER['REQUEST_METHOD']) ? strtolower($_SERVER['REQUEST_METHOD']) : 'cli';
Fatih Kalifa442b50b2013-11-01 02:44:56 +0700358
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100359 // Is there a literal match? If so we're done
Fatih Kalifa0b58b3c2013-11-05 15:36:40 +0700360 if (isset($this->routes[$uri]))
Fatih Kalifa442b50b2013-11-01 02:44:56 +0700361 {
Fatih Kalifa0b58b3c2013-11-05 15:36:40 +0700362 // Check default routes format
363 if (is_string($this->routes[$uri]))
364 {
Andrey Andreev30d53242014-01-16 14:41:46 +0200365 $this->_set_request(explode('/', $this->routes[$uri]));
366 return;
Fatih Kalifa0b58b3c2013-11-05 15:36:40 +0700367 }
Andrey Andreevc761a202013-11-11 14:02:15 +0200368 // Is there a matching http verb?
Fatih Kalifa0b58b3c2013-11-05 15:36:40 +0700369 elseif (is_array($this->routes[$uri]) && isset($this->routes[$uri][$http_verb]))
370 {
Andrey Andreev30d53242014-01-16 14:41:46 +0200371 $this->_set_request(explode('/', $this->routes[$uri][$http_verb]));
372 return;
Fatih Kalifa0b58b3c2013-11-05 15:36:40 +0700373 }
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100374 }
Barry Mienydd671972010-10-04 16:33:58 +0200375
vlakoffc941d852013-08-06 14:44:40 +0200376 // Loop through the route array looking for wildcards
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100377 foreach ($this->routes as $key => $val)
378 {
Fatih Kalifa0b58b3c2013-11-05 15:36:40 +0700379 // Check if route format is using http verb
Fatih Kalifa442b50b2013-11-01 02:44:56 +0700380 if (is_array($val))
381 {
Fatih Kalifa442b50b2013-11-01 02:44:56 +0700382 if (isset($val[$http_verb]))
383 {
384 $val = $val[$http_verb];
385 }
Fatih Kalifa442b50b2013-11-01 02:44:56 +0700386 else
387 {
Fatih Kalifa442b50b2013-11-01 02:44:56 +0700388 continue;
389 }
390 }
391
vlakoffc941d852013-08-06 14:44:40 +0200392 // Convert wildcards to RegEx
Andrey Andreev7676c2d2012-10-30 13:42:01 +0200393 $key = str_replace(array(':any', ':num'), array('[^/]+', '[0-9]+'), $key);
Derek Jonesc7738402010-03-02 13:55:13 -0600394
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100395 // Does the RegEx match?
396 if (preg_match('#^'.$key.'$#', $uri, $matches))
397 {
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100398 // Are we using callbacks to process back-references?
Andrey Andreevda5562a2012-11-08 12:34:38 +0200399 if ( ! is_string($val) && is_callable($val))
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100400 {
401 // Remove the original string from the matches array.
402 array_shift($matches);
Derek Allard2067d1a2008-11-13 22:59:24 +0000403
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100404 // Get the match count.
405 $match_count = count($matches);
Jonatas Miguelc0d98b22012-08-06 15:42:50 +0100406
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100407 // Determine how many parameters the callback has.
408 $reflection = new ReflectionFunction($val);
409 $param_count = $reflection->getNumberOfParameters();
Jonatas Miguelc0d98b22012-08-06 15:42:50 +0100410
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100411 // Are there more parameters than matches?
Jonatas Miguel24296ca2012-09-27 12:10:01 +0100412 if ($param_count > $match_count)
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100413 {
414 // Any params without matches will be set to an empty string.
415 $matches = array_merge($matches, array_fill($match_count, $param_count - $match_count, ''));
Jonatas Miguelefb81662012-10-23 19:52:46 +0100416
417 $match_count = $param_count;
418 }
419
420 // Get the parameters so we can use their default values.
421 $params = $reflection->getParameters();
422
423 for ($m = 0; $m < $match_count; $m++)
424 {
425 // Is the match empty and does a default value exist?
426 if (empty($matches[$m]) && $params[$m]->isDefaultValueAvailable())
427 {
428 // Substitute the empty match for the default value.
429 $matches[$m] = $params[$m]->getDefaultValue();
430 }
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100431 }
Jonatas Miguelc0d98b22012-08-06 15:42:50 +0100432
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100433 // Execute the callback using the values in matches as its parameters.
434 $val = call_user_func_array($val, $matches);
435 }
Andrey Andreevda5562a2012-11-08 12:34:38 +0200436 // Are we using the default routing method for back-references?
437 elseif (strpos($val, '$') !== FALSE && strpos($key, '(') !== FALSE)
438 {
439 $val = preg_replace('#^'.$key.'$#', $val, $uri);
440 }
Jonatas Miguelc0d98b22012-08-06 15:42:50 +0100441
Andrey Andreev30d53242014-01-16 14:41:46 +0200442 $this->_set_request(explode('/', $val));
443 return;
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100444 }
445 }
Jonatas Miguelc0d98b22012-08-06 15:42:50 +0100446
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100447 // If we got this far it means we didn't encounter a
448 // matching route so we'll set the site default route
449 $this->_set_request($this->uri->segments);
450 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000451
452 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200453
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300455 * Set class name
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300457 * @param string $class Class name
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200459 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200460 public function set_class($class)
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 {
Derek Jones2615e412010-10-06 17:51:16 -0500462 $this->class = str_replace(array('/', '.'), '', $class);
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 }
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 /**
468 * Fetch the current class
469 *
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300470 * @deprecated 3.0.0 Read the 'class' property instead
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200472 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200473 public function fetch_class()
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 {
475 return $this->class;
476 }
Barry Mienydd671972010-10-04 16:33:58 +0200477
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200479
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300481 * Set method name
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300483 * @param string $method Method name
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200485 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200486 public function set_method($method)
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 {
488 $this->method = $method;
489 }
490
491 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200492
Derek Allard2067d1a2008-11-13 22:59:24 +0000493 /**
Andrey Andreev7b53d042012-03-26 23:02:32 +0300494 * Fetch the current method
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 *
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300496 * @deprecated 3.0.0 Read the 'method' property instead
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200498 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200499 public function fetch_method()
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 {
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300501 return $this->method;
Derek Allard2067d1a2008-11-13 22:59:24 +0000502 }
503
504 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200505
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300507 * Set directory name
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300509 * @param string $dir Directory name
Andrey Andreev30d53242014-01-16 14:41:46 +0200510 * @param bool $appent Whether we're appending rather then setting the full value
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200512 */
Andrey Andreev30d53242014-01-16 14:41:46 +0200513 public function set_directory($dir, $append = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000514 {
Andrey Andreev30d53242014-01-16 14:41:46 +0200515 if ($append !== TRUE OR empty($this->directory))
516 {
517 $this->directory = str_replace('.', '', trim($dir, '/')).'/';
518 }
519 else
520 {
521 $this->directory .= str_replace('.', '', trim($dir, '/')).'/';
522 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 }
524
525 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200526
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300528 * Fetch directory
529 *
530 * Feches the sub-directory (if any) that contains the requested
531 * controller class.
Derek Allard2067d1a2008-11-13 22:59:24 +0000532 *
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300533 * @deprecated 3.0.0 Read the 'directory' property instead
Derek Allard2067d1a2008-11-13 22:59:24 +0000534 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200535 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200536 public function fetch_directory()
Derek Allard2067d1a2008-11-13 22:59:24 +0000537 {
538 return $this->directory;
539 }
540
541}
Derek Allard2067d1a2008-11-13 22:59:24 +0000542
543/* End of file Router.php */
Andrey Andreev7b53d042012-03-26 23:02:32 +0300544/* Location: ./system/core/Router.php */