blob: cc3916f8663fc736001e5717af7a2f7e12a9a680 [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
94 // --------------------------------------------------------------------
95
96 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +030097 * Class constructor
Derek Allard2067d1a2008-11-13 22:59:24 +000098 *
99 * Runs the route mapping function.
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300100 *
101 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000102 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200103 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 {
Derek Jonesc7738402010-03-02 13:55:13 -0600105 $this->config =& load_class('Config', 'core');
106 $this->uri =& load_class('URI', 'core');
Andrey Andreev08fec7b2013-07-19 16:25:51 +0300107 $this->_set_routing();
Andrey Andreevba6c0412012-01-07 21:10:09 +0200108 log_message('debug', 'Router Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 }
Barry Mienydd671972010-10-04 16:33:58 +0200110
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200112
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300114 * Set route mapping
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300116 * Determines what should be served based on the URI request,
Derek Allard2067d1a2008-11-13 22:59:24 +0000117 * as well as any "routes" that have been set in the routing config file.
118 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 * @return void
120 */
Andrey Andreev08fec7b2013-07-19 16:25:51 +0300121 protected function _set_routing()
Barry Mienydd671972010-10-04 16:33:58 +0200122 {
Andrey Andreevba6c0412012-01-07 21:10:09 +0200123 // Are query strings enabled in the config file? Normally CI doesn't utilize query strings
Barry Mienydd671972010-10-04 16:33:58 +0200124 // since URI segments are more search-engine friendly, but they can optionally be used.
Derek Jonesc7738402010-03-02 13:55:13 -0600125 // If this feature is enabled, we will gather the directory/class/method a little differently
126 $segments = array();
Andrey Andreev9515dd32012-12-07 15:15:15 +0200127 if ($this->config->item('enable_query_strings') === TRUE
128 && ! empty($_GET[$this->config->item('controller_trigger')])
129 && is_string($_GET[$this->config->item('controller_trigger')])
130 )
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 {
Andrey Andreev9515dd32012-12-07 15:15:15 +0200132 if (isset($_GET[$this->config->item('directory_trigger')]) && is_string($_GET[$this->config->item('directory_trigger')]))
Derek Jonesc7738402010-03-02 13:55:13 -0600133 {
134 $this->set_directory(trim($this->uri->_filter_uri($_GET[$this->config->item('directory_trigger')])));
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300135 $segments[] = $this->directory;
Derek Jonesc7738402010-03-02 13:55:13 -0600136 }
Barry Mienydd671972010-10-04 16:33:58 +0200137
Andrey Andreev9515dd32012-12-07 15:15:15 +0200138 $this->set_class(trim($this->uri->_filter_uri($_GET[$this->config->item('controller_trigger')])));
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300139 $segments[] = $this->class;
Barry Mienydd671972010-10-04 16:33:58 +0200140
Andrey Andreev9515dd32012-12-07 15:15:15 +0200141 if ( ! empty($_GET[$this->config->item('function_trigger')]) && is_string($_GET[$this->config->item('function_trigger')]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 {
143 $this->set_method(trim($this->uri->_filter_uri($_GET[$this->config->item('function_trigger')])));
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300144 $segments[] = $this->method;
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 }
Barry Mienydd671972010-10-04 16:33:58 +0200147
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 // Load the routes.php file.
Andrey Andreev06879112013-01-29 15:05:02 +0200149 if (file_exists(APPPATH.'config/routes.php'))
Greg Akerd96f8822011-12-27 16:23:47 -0600150 {
151 include(APPPATH.'config/routes.php');
152 }
David Behler07b53422011-08-15 00:25:06 +0200153
Andrey Andreev06879112013-01-29 15:05:02 +0200154 if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/routes.php'))
155 {
156 include(APPPATH.'config/'.ENVIRONMENT.'/routes.php');
157 }
158
Andrey Andreev08fec7b2013-07-19 16:25:51 +0300159 // Validate & get reserved routes
160 if (isset($route) && is_array($route))
161 {
162 isset($route['default_controller']) && $this->default_controller = $route['default_controller'];
163 isset($route['translate_uri_dashes']) && $this->translate_uri_dashes = $route['translate_uri_dashes'];
164 unset($route['default_controller'], $route['translate_uri_dashes']);
165 $this->routes = $route;
166 }
Barry Mienydd671972010-10-04 16:33:58 +0200167
Andrey Andreevba6c0412012-01-07 21:10:09 +0200168 // Were there any query string segments? If so, we'll validate them and bail out since we're done.
Derek Jonesc7738402010-03-02 13:55:13 -0600169 if (count($segments) > 0)
170 {
171 return $this->_validate_request($segments);
172 }
Barry Mienydd671972010-10-04 16:33:58 +0200173
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 // Fetch the complete URI string
175 $this->uri->_fetch_uri_string();
Barry Mienydd671972010-10-04 16:33:58 +0200176
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 // Is there a URI string? If not, the default controller specified in the "routes" file will be shown.
Andrey Andreev9ba661b2012-06-04 14:44:34 +0300178 if ($this->uri->uri_string == '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 {
Derek Jonesc7738402010-03-02 13:55:13 -0600180 return $this->_set_default_controller();
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 }
Barry Mienydd671972010-10-04 16:33:58 +0200182
Andrey Andreevba6c0412012-01-07 21:10:09 +0200183 $this->uri->_remove_url_suffix(); // Remove the URL suffix
184 $this->uri->_explode_segments(); // Compile the segments into an array
185 $this->_parse_routes(); // Parse any custom routing that may exist
186 $this->uri->_reindex_segments(); // Re-index the segment array so that it starts with 1 rather than 0
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 }
Derek Jonesc7738402010-03-02 13:55:13 -0600188
189 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200190
Derek Jonesc7738402010-03-02 13:55:13 -0600191 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300192 * Set default controller
Derek Jonesc7738402010-03-02 13:55:13 -0600193 *
Derek Jonesc7738402010-03-02 13:55:13 -0600194 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200195 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200196 protected function _set_default_controller()
Derek Jonesc7738402010-03-02 13:55:13 -0600197 {
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200198 if (empty($this->default_controller))
Derek Jonesc7738402010-03-02 13:55:13 -0600199 {
Andrey Andreevba6c0412012-01-07 21:10:09 +0200200 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 -0600201 }
Andrey Andreevd1097a12012-11-01 19:55:42 +0200202
Derek Jonesc7738402010-03-02 13:55:13 -0600203 // Is the method being specified?
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200204 if (sscanf($this->default_controller, '%[^/]/%s', $class, $method) !== 2)
Derek Jonesc7738402010-03-02 13:55:13 -0600205 {
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200206 $method = 'index';
Barry Mienydd671972010-10-04 16:33:58 +0200207 }
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200208
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200209 $this->_set_request(array($class, $method));
Barry Mienydd671972010-10-04 16:33:58 +0200210
Derek Jonesc7738402010-03-02 13:55:13 -0600211 // re-index the routed segments array so it starts with 1 rather than 0
212 $this->uri->_reindex_segments();
Barry Mienydd671972010-10-04 16:33:58 +0200213
Andrey Andreevba6c0412012-01-07 21:10:09 +0200214 log_message('debug', 'No URI present. Default controller set.');
Derek Jonesc7738402010-03-02 13:55:13 -0600215 }
Barry Mienydd671972010-10-04 16:33:58 +0200216
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200218
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300220 * Set request route
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300222 * Takes an array of URI segments as input and sets the class/method
223 * to be called.
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300225 * @param array $segments URI segments
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 * @return void
227 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200228 protected function _set_request($segments = array())
Barry Mienydd671972010-10-04 16:33:58 +0200229 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 $segments = $this->_validate_request($segments);
Barry Mienydd671972010-10-04 16:33:58 +0200231
Andrey Andreevba6c0412012-01-07 21:10:09 +0200232 if (count($segments) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 {
Derek Jonesc7738402010-03-02 13:55:13 -0600234 return $this->_set_default_controller();
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 }
Barry Mienydd671972010-10-04 16:33:58 +0200236
Andrey Andreev08fec7b2013-07-19 16:25:51 +0300237 if ($this->translate_uri_dashes === TRUE)
238 {
239 $segments[0] = str_replace('-', '_', $segments[0]);
240 if (isset($segments[1]))
241 {
242 $segments[1] = str_replace('-', '_', $segments[1]);
243 }
244 }
Barry Mienydd671972010-10-04 16:33:58 +0200245
Andrey Andreev08fec7b2013-07-19 16:25:51 +0300246 $this->set_class($segments[0]);
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200247 isset($segments[1]) OR $segments[1] = 'index';
248 $this->set_method($segments[1]);
Barry Mienydd671972010-10-04 16:33:58 +0200249
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 // Update our "routed" segment array to contain the segments.
251 // Note: If there is no custom routing, this array will be
Phil Sturgeon81aa94b2012-05-02 11:40:46 +0100252 // identical to $this->uri->segments
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 $this->uri->rsegments = $segments;
254 }
Barry Mienydd671972010-10-04 16:33:58 +0200255
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200257
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300259 * Validate request
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300261 * Attempts validate the URI request and determine the controller path.
262 *
263 * @param array $segments URI segments
264 * @return array URI segments
Barry Mienydd671972010-10-04 16:33:58 +0200265 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200266 protected function _validate_request($segments)
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 {
Andrey Andreevba6c0412012-01-07 21:10:09 +0200268 if (count($segments) === 0)
Derek Jonesc7738402010-03-02 13:55:13 -0600269 {
270 return $segments;
271 }
Barry Mienydd671972010-10-04 16:33:58 +0200272
Andrey Andreev08fec7b2013-07-19 16:25:51 +0300273 $test = ($this->translate_uri_dashes === TRUE)
274 ? str_replace('-', '_', $segments[0]) : $segments[0];
Andrey Andreevd1097a12012-11-01 19:55:42 +0200275
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 // Does the requested controller exist in the root folder?
Andrey Andreev08fec7b2013-07-19 16:25:51 +0300277 if (file_exists(APPPATH.'controllers/'.$test.'.php'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 {
279 return $segments;
280 }
Barry Mienydd671972010-10-04 16:33:58 +0200281
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 // Is the controller in a sub-folder?
283 if (is_dir(APPPATH.'controllers/'.$segments[0]))
Derek Jonesc7738402010-03-02 13:55:13 -0600284 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 // Set the directory and remove it from the segment array
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200286 $this->set_directory(array_shift($segments));
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 if (count($segments) > 0)
288 {
Andrey Andreev08fec7b2013-07-19 16:25:51 +0300289 $test = ($this->translate_uri_dashes === TRUE)
290 ? str_replace('-', '_', $segments[0]) : $segments[0];
Andrey Andreevd1097a12012-11-01 19:55:42 +0200291
Andrey Andreev08fec7b2013-07-19 16:25:51 +0300292 // Does the requested controller exist in the sub-directory?
293 if ( ! file_exists(APPPATH.'controllers/'.$this->directory.$test.'.php'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 {
Shane Pearson664a9352011-08-10 16:02:32 -0500295 if ( ! empty($this->routes['404_override']))
296 {
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200297 $this->directory = '';
298 return explode('/', $this->routes['404_override'], 2);
Shane Pearson664a9352011-08-10 16:02:32 -0500299 }
300 else
301 {
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300302 show_404($this->directory.$segments[0]);
Shane Pearson664a9352011-08-10 16:02:32 -0500303 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 }
305 }
306 else
307 {
Derek Jonesc7738402010-03-02 13:55:13 -0600308 // Is the method being specified in the route?
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200309 $segments = explode('/', $this->default_controller);
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300310 if ( ! file_exists(APPPATH.'controllers/'.$this->directory.$segments[0].'.php'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 {
312 $this->directory = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 }
Barry Mienydd671972010-10-04 16:33:58 +0200315
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 return $segments;
317 }
Barry Mienydd671972010-10-04 16:33:58 +0200318
Derek Jonesc7738402010-03-02 13:55:13 -0600319 // If we've gotten this far it means that the URI does not correlate to a valid
Andrey Andreevba6c0412012-01-07 21:10:09 +0200320 // controller class. We will now see if there is an override
Eric Barnesc5bf6162011-01-30 21:17:11 -0500321 if ( ! empty($this->routes['404_override']))
Derek Jonesc7738402010-03-02 13:55:13 -0600322 {
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200323 if (sscanf($this->routes['404_override'], '%[^/]/%s', $class, $method) !== 2)
324 {
325 $method = 'index';
326 }
Barry Mienydd671972010-10-04 16:33:58 +0200327
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200328 return array($class, $method);
Derek Jonesc7738402010-03-02 13:55:13 -0600329 }
Barry Mienydd671972010-10-04 16:33:58 +0200330
Derek Jonesc7738402010-03-02 13:55:13 -0600331 // Nothing else to do at this point but show a 404
Barry Mienydd671972010-10-04 16:33:58 +0200332 show_404($segments[0]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 }
Barry Mienydd671972010-10-04 16:33:58 +0200334
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200336
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 /**
Andrey Andreev7b53d042012-03-26 23:02:32 +0300338 * Parse Routes
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300340 * Matches any routes that may exist in the config/routes.php file
341 * against the URI to determine if the class/method need to be remapped.
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 * @return void
344 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200345 protected function _parse_routes()
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100346 {
347 // Turn the segment array into a URI string
348 $uri = implode('/', $this->uri->segments);
Barry Mienydd671972010-10-04 16:33:58 +0200349
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100350 // Is there a literal match? If so we're done
Andrey Andreeve2b07542012-11-08 12:37:40 +0200351 if (isset($this->routes[$uri]) && is_string($this->routes[$uri]))
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100352 {
353 return $this->_set_request(explode('/', $this->routes[$uri]));
354 }
Barry Mienydd671972010-10-04 16:33:58 +0200355
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100356 // Loop through the route array looking for wild-cards
357 foreach ($this->routes as $key => $val)
358 {
359 // Convert wild-cards to RegEx
Andrey Andreev7676c2d2012-10-30 13:42:01 +0200360 $key = str_replace(array(':any', ':num'), array('[^/]+', '[0-9]+'), $key);
Derek Jonesc7738402010-03-02 13:55:13 -0600361
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100362 // Does the RegEx match?
363 if (preg_match('#^'.$key.'$#', $uri, $matches))
364 {
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100365 // Are we using callbacks to process back-references?
Andrey Andreevda5562a2012-11-08 12:34:38 +0200366 if ( ! is_string($val) && is_callable($val))
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100367 {
368 // Remove the original string from the matches array.
369 array_shift($matches);
Derek Allard2067d1a2008-11-13 22:59:24 +0000370
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100371 // Get the match count.
372 $match_count = count($matches);
Jonatas Miguelc0d98b22012-08-06 15:42:50 +0100373
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100374 // Determine how many parameters the callback has.
375 $reflection = new ReflectionFunction($val);
376 $param_count = $reflection->getNumberOfParameters();
Jonatas Miguelc0d98b22012-08-06 15:42:50 +0100377
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100378 // Are there more parameters than matches?
Jonatas Miguel24296ca2012-09-27 12:10:01 +0100379 if ($param_count > $match_count)
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100380 {
381 // Any params without matches will be set to an empty string.
382 $matches = array_merge($matches, array_fill($match_count, $param_count - $match_count, ''));
Jonatas Miguelefb81662012-10-23 19:52:46 +0100383
384 $match_count = $param_count;
385 }
386
387 // Get the parameters so we can use their default values.
388 $params = $reflection->getParameters();
389
390 for ($m = 0; $m < $match_count; $m++)
391 {
392 // Is the match empty and does a default value exist?
393 if (empty($matches[$m]) && $params[$m]->isDefaultValueAvailable())
394 {
395 // Substitute the empty match for the default value.
396 $matches[$m] = $params[$m]->getDefaultValue();
397 }
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100398 }
Jonatas Miguelc0d98b22012-08-06 15:42:50 +0100399
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100400 // Execute the callback using the values in matches as its parameters.
401 $val = call_user_func_array($val, $matches);
402 }
Andrey Andreevda5562a2012-11-08 12:34:38 +0200403 // Are we using the default routing method for back-references?
404 elseif (strpos($val, '$') !== FALSE && strpos($key, '(') !== FALSE)
405 {
406 $val = preg_replace('#^'.$key.'$#', $val, $uri);
407 }
Jonatas Miguelc0d98b22012-08-06 15:42:50 +0100408
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100409 return $this->_set_request(explode('/', $val));
410 }
411 }
Jonatas Miguelc0d98b22012-08-06 15:42:50 +0100412
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100413 // If we got this far it means we didn't encounter a
414 // matching route so we'll set the site default route
415 $this->_set_request($this->uri->segments);
416 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000417
418 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200419
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300421 * Set class name
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300423 * @param string $class Class name
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200425 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200426 public function set_class($class)
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 {
Derek Jones2615e412010-10-06 17:51:16 -0500428 $this->class = str_replace(array('/', '.'), '', $class);
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 }
Barry Mienydd671972010-10-04 16:33:58 +0200430
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200432
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 /**
434 * Fetch the current class
435 *
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300436 * @deprecated 3.0.0 Read the 'class' property instead
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200438 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200439 public function fetch_class()
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 {
441 return $this->class;
442 }
Barry Mienydd671972010-10-04 16:33:58 +0200443
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200445
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300447 * Set method name
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300449 * @param string $method Method name
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200451 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200452 public function set_method($method)
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 {
454 $this->method = $method;
455 }
456
457 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200458
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 /**
Andrey Andreev7b53d042012-03-26 23:02:32 +0300460 * Fetch the current method
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 *
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300462 * @deprecated 3.0.0 Read the 'method' property instead
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200464 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200465 public function fetch_method()
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 {
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300467 return $this->method;
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 }
469
470 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200471
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300473 * Set directory name
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300475 * @param string $dir Directory name
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200477 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200478 public function set_directory($dir)
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 {
Derek Jones2615e412010-10-06 17:51:16 -0500480 $this->directory = str_replace(array('/', '.'), '', $dir).'/';
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 }
482
483 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200484
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300486 * Fetch directory
487 *
488 * Feches the sub-directory (if any) that contains the requested
489 * controller class.
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 *
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300491 * @deprecated 3.0.0 Read the 'directory' property instead
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200493 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200494 public function fetch_directory()
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 {
496 return $this->directory;
497 }
498
Derek Jonesc7738402010-03-02 13:55:13 -0600499 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200500
Derek Jonesc7738402010-03-02 13:55:13 -0600501 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300502 * Set controller overrides
Derek Jonesc7738402010-03-02 13:55:13 -0600503 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300504 * @param array $routing Route overrides
Andrey Andreev7b53d042012-03-26 23:02:32 +0300505 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200506 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200507 public function _set_overrides($routing)
Derek Jonesc7738402010-03-02 13:55:13 -0600508 {
509 if ( ! is_array($routing))
510 {
511 return;
512 }
Barry Mienydd671972010-10-04 16:33:58 +0200513
Derek Jonesc7738402010-03-02 13:55:13 -0600514 if (isset($routing['directory']))
515 {
516 $this->set_directory($routing['directory']);
517 }
Barry Mienydd671972010-10-04 16:33:58 +0200518
Andrey Andreev9ba661b2012-06-04 14:44:34 +0300519 if ( ! empty($routing['controller']))
Derek Jonesc7738402010-03-02 13:55:13 -0600520 {
521 $this->set_class($routing['controller']);
522 }
Barry Mienydd671972010-10-04 16:33:58 +0200523
Derek Jonesc7738402010-03-02 13:55:13 -0600524 if (isset($routing['function']))
525 {
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200526 $routing['function'] = empty($routing['function']) ? 'index' : $routing['function'];
Derek Jonesc7738402010-03-02 13:55:13 -0600527 $this->set_method($routing['function']);
528 }
529 }
530
Derek Allard2067d1a2008-11-13 22:59:24 +0000531}
Derek Allard2067d1a2008-11-13 22:59:24 +0000532
533/* End of file Router.php */
Andrey Andreev7b53d042012-03-26 23:02:32 +0300534/* Location: ./system/core/Router.php */