blob: 89fb74f2f8b219218d9ec6484b850c59cedacf07 [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
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, 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 /**
57 * List of error routes
58 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +030059 * @var array
David Behler07b53422011-08-15 00:25:06 +020060 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040061 public $error_routes = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +030062
David Behler07b53422011-08-15 00:25:06 +020063 /**
64 * Current class name
65 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +030066 * @var string
David Behler07b53422011-08-15 00:25:06 +020067 */
Andrey Andreev92ebfb62012-05-17 12:49:24 +030068 public $class = '';
69
David Behler07b53422011-08-15 00:25:06 +020070 /**
71 * Current method name
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 $method = 'index';
Andrey Andreev92ebfb62012-05-17 12:49:24 +030076
David Behler07b53422011-08-15 00:25:06 +020077 /**
78 * Sub-directory that contains the requested controller class
79 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +030080 * @var string
David Behler07b53422011-08-15 00:25:06 +020081 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040082 public $directory = '';
Andrey Andreev92ebfb62012-05-17 12:49:24 +030083
David Behler07b53422011-08-15 00:25:06 +020084 /**
85 * Default controller (and method if specific)
86 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +030087 * @var string
David Behler07b53422011-08-15 00:25:06 +020088 */
Andrey Andreevba6c0412012-01-07 21:10:09 +020089 public $default_controller;
Barry Mienydd671972010-10-04 16:33:58 +020090
Derek Allard2067d1a2008-11-13 22:59:24 +000091 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +030092 * Class constructor
Derek Allard2067d1a2008-11-13 22:59:24 +000093 *
94 * Runs the route mapping function.
Andrey Andreev92ebfb62012-05-17 12:49:24 +030095 *
96 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +000097 */
Andrey Andreevba6c0412012-01-07 21:10:09 +020098 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000099 {
Derek Jonesc7738402010-03-02 13:55:13 -0600100 $this->config =& load_class('Config', 'core');
101 $this->uri =& load_class('URI', 'core');
Andrey Andreevba6c0412012-01-07 21:10:09 +0200102 log_message('debug', 'Router Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000103 }
Barry Mienydd671972010-10-04 16:33:58 +0200104
Derek Allard2067d1a2008-11-13 22:59:24 +0000105 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200106
Derek Allard2067d1a2008-11-13 22:59:24 +0000107 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300108 * Set route mapping
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300110 * Determines what should be served based on the URI request,
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 * as well as any "routes" that have been set in the routing config file.
112 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 * @return void
114 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200115 public function _set_routing()
Barry Mienydd671972010-10-04 16:33:58 +0200116 {
Andrey Andreevba6c0412012-01-07 21:10:09 +0200117 // Are query strings enabled in the config file? Normally CI doesn't utilize query strings
Barry Mienydd671972010-10-04 16:33:58 +0200118 // since URI segments are more search-engine friendly, but they can optionally be used.
Derek Jonesc7738402010-03-02 13:55:13 -0600119 // If this feature is enabled, we will gather the directory/class/method a little differently
120 $segments = array();
Andrey Andreev7b53d042012-03-26 23:02:32 +0300121 if ($this->config->item('enable_query_strings') === TRUE && isset($_GET[$this->config->item('controller_trigger')]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 {
Derek Jonesc7738402010-03-02 13:55:13 -0600123 if (isset($_GET[$this->config->item('directory_trigger')]))
124 {
125 $this->set_directory(trim($this->uri->_filter_uri($_GET[$this->config->item('directory_trigger')])));
126 $segments[] = $this->fetch_directory();
127 }
Barry Mienydd671972010-10-04 16:33:58 +0200128
Derek Jonesc7738402010-03-02 13:55:13 -0600129 if (isset($_GET[$this->config->item('controller_trigger')]))
130 {
131 $this->set_class(trim($this->uri->_filter_uri($_GET[$this->config->item('controller_trigger')])));
132 $segments[] = $this->fetch_class();
133 }
Barry Mienydd671972010-10-04 16:33:58 +0200134
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 if (isset($_GET[$this->config->item('function_trigger')]))
136 {
137 $this->set_method(trim($this->uri->_filter_uri($_GET[$this->config->item('function_trigger')])));
Derek Jonesc7738402010-03-02 13:55:13 -0600138 $segments[] = $this->fetch_method();
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 }
Barry Mienydd671972010-10-04 16:33:58 +0200141
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 // Load the routes.php file.
Andrey Andreev7b53d042012-03-26 23:02:32 +0300143 if (defined('ENVIRONMENT') && is_file(APPPATH.'config/'.ENVIRONMENT.'/routes.php'))
Greg Akerd96f8822011-12-27 16:23:47 -0600144 {
145 include(APPPATH.'config/'.ENVIRONMENT.'/routes.php');
146 }
147 elseif (is_file(APPPATH.'config/routes.php'))
148 {
149 include(APPPATH.'config/routes.php');
150 }
David Behler07b53422011-08-15 00:25:06 +0200151
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 $this->routes = ( ! isset($route) OR ! is_array($route)) ? array() : $route;
153 unset($route);
Barry Mienydd671972010-10-04 16:33:58 +0200154
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 // Set the default controller so we can display it in the event
156 // the URI doesn't correlated to a valid controller.
Andrey Andreev7b53d042012-03-26 23:02:32 +0300157 $this->default_controller = empty($this->routes['default_controller']) ? FALSE : strtolower($this->routes['default_controller']);
Barry Mienydd671972010-10-04 16:33:58 +0200158
Andrey Andreevba6c0412012-01-07 21:10:09 +0200159 // 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 -0600160 if (count($segments) > 0)
161 {
162 return $this->_validate_request($segments);
163 }
Barry Mienydd671972010-10-04 16:33:58 +0200164
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 // Fetch the complete URI string
166 $this->uri->_fetch_uri_string();
Barry Mienydd671972010-10-04 16:33:58 +0200167
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 // 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 +0300169 if ($this->uri->uri_string == '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 {
Derek Jonesc7738402010-03-02 13:55:13 -0600171 return $this->_set_default_controller();
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 }
Barry Mienydd671972010-10-04 16:33:58 +0200173
Andrey Andreevba6c0412012-01-07 21:10:09 +0200174 $this->uri->_remove_url_suffix(); // Remove the URL suffix
175 $this->uri->_explode_segments(); // Compile the segments into an array
176 $this->_parse_routes(); // Parse any custom routing that may exist
177 $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 +0000178 }
Derek Jonesc7738402010-03-02 13:55:13 -0600179
180 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200181
Derek Jonesc7738402010-03-02 13:55:13 -0600182 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300183 * Set default controller
Derek Jonesc7738402010-03-02 13:55:13 -0600184 *
Derek Jonesc7738402010-03-02 13:55:13 -0600185 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200186 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200187 protected function _set_default_controller()
Derek Jonesc7738402010-03-02 13:55:13 -0600188 {
189 if ($this->default_controller === FALSE)
190 {
Andrey Andreevba6c0412012-01-07 21:10:09 +0200191 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 -0600192 }
Andrey Andreevd1097a12012-11-01 19:55:42 +0200193
Derek Jonesc7738402010-03-02 13:55:13 -0600194 // Is the method being specified?
195 if (strpos($this->default_controller, '/') !== FALSE)
196 {
197 $x = explode('/', $this->default_controller);
Derek Jonesc7738402010-03-02 13:55:13 -0600198 $this->set_class($x[0]);
199 $this->set_method($x[1]);
Pascal Kriete790ebf32010-12-15 10:53:35 -0500200 $this->_set_request($x);
Barry Mienydd671972010-10-04 16:33:58 +0200201 }
Derek Jonesc7738402010-03-02 13:55:13 -0600202 else
203 {
204 $this->set_class($this->default_controller);
205 $this->set_method('index');
206 $this->_set_request(array($this->default_controller, 'index'));
207 }
Barry Mienydd671972010-10-04 16:33:58 +0200208
Derek Jonesc7738402010-03-02 13:55:13 -0600209 // re-index the routed segments array so it starts with 1 rather than 0
210 $this->uri->_reindex_segments();
Barry Mienydd671972010-10-04 16:33:58 +0200211
Andrey Andreevba6c0412012-01-07 21:10:09 +0200212 log_message('debug', 'No URI present. Default controller set.');
Derek Jonesc7738402010-03-02 13:55:13 -0600213 }
Barry Mienydd671972010-10-04 16:33:58 +0200214
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200216
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300218 * Set request route
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300220 * Takes an array of URI segments as input and sets the class/method
221 * to be called.
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300223 * @param array $segments URI segments
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 * @return void
225 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200226 protected function _set_request($segments = array())
Barry Mienydd671972010-10-04 16:33:58 +0200227 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 $segments = $this->_validate_request($segments);
Barry Mienydd671972010-10-04 16:33:58 +0200229
Andrey Andreevba6c0412012-01-07 21:10:09 +0200230 if (count($segments) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 {
Derek Jonesc7738402010-03-02 13:55:13 -0600232 return $this->_set_default_controller();
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 }
Barry Mienydd671972010-10-04 16:33:58 +0200234
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 $this->set_class($segments[0]);
Barry Mienydd671972010-10-04 16:33:58 +0200236
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 if (isset($segments[1]))
238 {
Derek Jonesc7738402010-03-02 13:55:13 -0600239 // A standard method request
240 $this->set_method($segments[1]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 }
242 else
243 {
244 // This lets the "routed" segment array identify that the default
245 // index method is being used.
246 $segments[1] = 'index';
247 }
Barry Mienydd671972010-10-04 16:33:58 +0200248
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 // Update our "routed" segment array to contain the segments.
250 // Note: If there is no custom routing, this array will be
Phil Sturgeon81aa94b2012-05-02 11:40:46 +0100251 // identical to $this->uri->segments
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 $this->uri->rsegments = $segments;
253 }
Barry Mienydd671972010-10-04 16:33:58 +0200254
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200256
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300258 * Validate request
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300260 * Attempts validate the URI request and determine the controller path.
261 *
262 * @param array $segments URI segments
263 * @return array URI segments
Barry Mienydd671972010-10-04 16:33:58 +0200264 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200265 protected function _validate_request($segments)
Derek Allard2067d1a2008-11-13 22:59:24 +0000266 {
Andrey Andreevba6c0412012-01-07 21:10:09 +0200267 if (count($segments) === 0)
Derek Jonesc7738402010-03-02 13:55:13 -0600268 {
269 return $segments;
270 }
Barry Mienydd671972010-10-04 16:33:58 +0200271
Andrey Andreevd1097a12012-11-01 19:55:42 +0200272 $temp = str_replace('-', '_', $segments[0]);
273
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 // Does the requested controller exist in the root folder?
Andrey Andreevd1097a12012-11-01 19:55:42 +0200275 if (file_exists(APPPATH.'controllers/'.$temp.'.php'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 {
Andrey Andreevd1097a12012-11-01 19:55:42 +0200277 $segments[0] = $temp;
278 empty($segments[1]) OR $segments[1] = str_replace('-', '_', $segments[1]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 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
286 $this->set_directory($segments[0]);
287 $segments = array_slice($segments, 1);
Barry Mienydd671972010-10-04 16:33:58 +0200288
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 if (count($segments) > 0)
290 {
Andrey Andreevd1097a12012-11-01 19:55:42 +0200291 $segments[0] = str_replace('-', '_', $segments[0]);
292 empty($segments[1]) OR $segments[1] = str_replace('-', '_', $segments[1]);
293
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 // Does the requested controller exist in the sub-folder?
Greg Aker3a746652011-04-19 10:59:47 -0500295 if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].'.php'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 {
Shane Pearson664a9352011-08-10 16:02:32 -0500297 if ( ! empty($this->routes['404_override']))
298 {
299 $x = explode('/', $this->routes['404_override']);
Shane Pearson664a9352011-08-10 16:02:32 -0500300 $this->set_directory('');
301 $this->set_class($x[0]);
302 $this->set_method(isset($x[1]) ? $x[1] : 'index');
David Behler07b53422011-08-15 00:25:06 +0200303
Shane Pearson664a9352011-08-10 16:02:32 -0500304 return $x;
305 }
306 else
307 {
308 show_404($this->fetch_directory().$segments[0]);
309 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 }
311 }
312 else
313 {
Derek Jonesc7738402010-03-02 13:55:13 -0600314 // Is the method being specified in the route?
315 if (strpos($this->default_controller, '/') !== FALSE)
316 {
317 $x = explode('/', $this->default_controller);
Derek Jonesc7738402010-03-02 13:55:13 -0600318 $this->set_class($x[0]);
319 $this->set_method($x[1]);
Barry Mienydd671972010-10-04 16:33:58 +0200320 }
Derek Jonesc7738402010-03-02 13:55:13 -0600321 else
322 {
323 $this->set_class($this->default_controller);
324 $this->set_method('index');
325 }
Barry Mienydd671972010-10-04 16:33:58 +0200326
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 // Does the default controller exist in the sub-folder?
Greg Aker3a746652011-04-19 10:59:47 -0500328 if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.'.php'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 {
330 $this->directory = '';
331 return array();
332 }
Barry Mienydd671972010-10-04 16:33:58 +0200333
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 }
Barry Mienydd671972010-10-04 16:33:58 +0200335
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 return $segments;
337 }
Barry Mienydd671972010-10-04 16:33:58 +0200338
339
Derek Jonesc7738402010-03-02 13:55:13 -0600340 // If we've gotten this far it means that the URI does not correlate to a valid
Andrey Andreevba6c0412012-01-07 21:10:09 +0200341 // controller class. We will now see if there is an override
Eric Barnesc5bf6162011-01-30 21:17:11 -0500342 if ( ! empty($this->routes['404_override']))
Derek Jonesc7738402010-03-02 13:55:13 -0600343 {
Phil Sturgeon23174a62010-12-15 15:18:16 +0000344 $x = explode('/', $this->routes['404_override']);
Phil Sturgeon23174a62010-12-15 15:18:16 +0000345 $this->set_class($x[0]);
346 $this->set_method(isset($x[1]) ? $x[1] : 'index');
Barry Mienydd671972010-10-04 16:33:58 +0200347
Phil Sturgeon23174a62010-12-15 15:18:16 +0000348 return $x;
Derek Jonesc7738402010-03-02 13:55:13 -0600349 }
Barry Mienydd671972010-10-04 16:33:58 +0200350
Derek Jonesc7738402010-03-02 13:55:13 -0600351 // Nothing else to do at this point but show a 404
Barry Mienydd671972010-10-04 16:33:58 +0200352 show_404($segments[0]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 }
Barry Mienydd671972010-10-04 16:33:58 +0200354
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200356
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 /**
Andrey Andreev7b53d042012-03-26 23:02:32 +0300358 * Parse Routes
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300360 * Matches any routes that may exist in the config/routes.php file
361 * against the URI to determine if the class/method need to be remapped.
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 * @return void
364 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200365 protected function _parse_routes()
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100366 {
367 // Turn the segment array into a URI string
368 $uri = implode('/', $this->uri->segments);
Barry Mienydd671972010-10-04 16:33:58 +0200369
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100370 // Is there a literal match? If so we're done
371 if (isset($this->routes[$uri]))
372 {
373 return $this->_set_request(explode('/', $this->routes[$uri]));
374 }
Barry Mienydd671972010-10-04 16:33:58 +0200375
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100376 // Loop through the route array looking for wild-cards
377 foreach ($this->routes as $key => $val)
378 {
379 // Convert wild-cards to RegEx
Andrey Andreev7676c2d2012-10-30 13:42:01 +0200380 $key = str_replace(array(':any', ':num'), array('[^/]+', '[0-9]+'), $key);
Derek Jonesc7738402010-03-02 13:55:13 -0600381
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100382 // Does the RegEx match?
383 if (preg_match('#^'.$key.'$#', $uri, $matches))
384 {
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100385 // Are we using callbacks to process back-references?
Jonatas Miguel24296ca2012-09-27 12:10:01 +0100386 if ( ! is_string($val) && is_callable($val))
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100387 {
388 // Remove the original string from the matches array.
389 array_shift($matches);
Derek Allard2067d1a2008-11-13 22:59:24 +0000390
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100391 // Get the match count.
392 $match_count = count($matches);
Jonatas Miguelc0d98b22012-08-06 15:42:50 +0100393
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100394 // Determine how many parameters the callback has.
395 $reflection = new ReflectionFunction($val);
396 $param_count = $reflection->getNumberOfParameters();
Jonatas Miguelc0d98b22012-08-06 15:42:50 +0100397
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100398 // Are there more parameters than matches?
Jonatas Miguel24296ca2012-09-27 12:10:01 +0100399 if ($param_count > $match_count)
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100400 {
401 // Any params without matches will be set to an empty string.
402 $matches = array_merge($matches, array_fill($match_count, $param_count - $match_count, ''));
Jonatas Miguelefb81662012-10-23 19:52:46 +0100403
404 $match_count = $param_count;
405 }
406
407 // Get the parameters so we can use their default values.
408 $params = $reflection->getParameters();
409
410 for ($m = 0; $m < $match_count; $m++)
411 {
412 // Is the match empty and does a default value exist?
413 if (empty($matches[$m]) && $params[$m]->isDefaultValueAvailable())
414 {
415 // Substitute the empty match for the default value.
416 $matches[$m] = $params[$m]->getDefaultValue();
417 }
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100418 }
Jonatas Miguelc0d98b22012-08-06 15:42:50 +0100419
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100420 // Execute the callback using the values in matches as its parameters.
421 $val = call_user_func_array($val, $matches);
422 }
423 // Are we using the default routing method for back-references?
Jonatas Miguelf950e5c2012-09-27 11:39:33 +0100424 elseif (strpos($val, '$') !== FALSE && strpos($key, '(') !== FALSE)
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100425 {
426 $val = preg_replace('#^'.$key.'$#', $val, $uri);
427 }
Jonatas Miguelc0d98b22012-08-06 15:42:50 +0100428
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100429 return $this->_set_request(explode('/', $val));
430 }
431 }
Jonatas Miguelc0d98b22012-08-06 15:42:50 +0100432
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100433 // If we got this far it means we didn't encounter a
434 // matching route so we'll set the site default route
435 $this->_set_request($this->uri->segments);
436 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000437
438 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200439
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300441 * Set class name
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300443 * @param string $class Class name
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200445 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200446 public function set_class($class)
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 {
Derek Jones2615e412010-10-06 17:51:16 -0500448 $this->class = str_replace(array('/', '.'), '', $class);
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 }
Barry Mienydd671972010-10-04 16:33:58 +0200450
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200452
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 /**
454 * Fetch the current class
455 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200457 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200458 public function fetch_class()
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 {
460 return $this->class;
461 }
Barry Mienydd671972010-10-04 16:33:58 +0200462
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200464
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300466 * Set method name
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300468 * @param string $method Method name
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200470 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200471 public function set_method($method)
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 {
473 $this->method = $method;
474 }
475
476 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200477
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 /**
Andrey Andreev7b53d042012-03-26 23:02:32 +0300479 * Fetch the current method
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200482 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200483 public function fetch_method()
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 {
Alex Bilbieed944a32012-06-02 11:07:47 +0100485 return ($this->method === $this->fetch_class()) ? 'index' : $this->method;
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 }
487
488 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200489
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300491 * Set directory name
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300493 * @param string $dir Directory name
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200495 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200496 public function set_directory($dir)
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 {
Derek Jones2615e412010-10-06 17:51:16 -0500498 $this->directory = str_replace(array('/', '.'), '', $dir).'/';
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 }
500
501 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200502
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300504 * Fetch directory
505 *
506 * Feches the sub-directory (if any) that contains the requested
507 * controller class.
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000509 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200510 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200511 public function fetch_directory()
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 {
513 return $this->directory;
514 }
515
Derek Jonesc7738402010-03-02 13:55:13 -0600516 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200517
Derek Jonesc7738402010-03-02 13:55:13 -0600518 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300519 * Set controller overrides
Derek Jonesc7738402010-03-02 13:55:13 -0600520 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300521 * @param array $routing Route overrides
Andrey Andreev7b53d042012-03-26 23:02:32 +0300522 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200523 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200524 public function _set_overrides($routing)
Derek Jonesc7738402010-03-02 13:55:13 -0600525 {
526 if ( ! is_array($routing))
527 {
528 return;
529 }
Barry Mienydd671972010-10-04 16:33:58 +0200530
Derek Jonesc7738402010-03-02 13:55:13 -0600531 if (isset($routing['directory']))
532 {
533 $this->set_directory($routing['directory']);
534 }
Barry Mienydd671972010-10-04 16:33:58 +0200535
Andrey Andreev9ba661b2012-06-04 14:44:34 +0300536 if ( ! empty($routing['controller']))
Derek Jonesc7738402010-03-02 13:55:13 -0600537 {
538 $this->set_class($routing['controller']);
539 }
Barry Mienydd671972010-10-04 16:33:58 +0200540
Derek Jonesc7738402010-03-02 13:55:13 -0600541 if (isset($routing['function']))
542 {
Andrey Andreev9ba661b2012-06-04 14:44:34 +0300543 $routing['function'] = ($routing['function'] == '') ? 'index' : $routing['function'];
Derek Jonesc7738402010-03-02 13:55:13 -0600544 $this->set_method($routing['function']);
545 }
546 }
547
Derek Allard2067d1a2008-11-13 22:59:24 +0000548}
Derek Allard2067d1a2008-11-13 22:59:24 +0000549
550/* End of file Router.php */
Andrey Andreev7b53d042012-03-26 23:02:32 +0300551/* Location: ./system/core/Router.php */