blob: 48c157f1880af04aeffe3ad410418fef8dd40ba2 [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 /**
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 Andreevb9fe7e92012-10-27 18:45:59 +030085 * Class constructor
Derek Allard2067d1a2008-11-13 22:59:24 +000086 *
87 * Runs the route mapping function.
Andrey Andreev92ebfb62012-05-17 12:49:24 +030088 *
89 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +000090 */
Andrey Andreevba6c0412012-01-07 21:10:09 +020091 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000092 {
Derek Jonesc7738402010-03-02 13:55:13 -060093 $this->config =& load_class('Config', 'core');
94 $this->uri =& load_class('URI', 'core');
Andrey Andreevba6c0412012-01-07 21:10:09 +020095 log_message('debug', 'Router Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +000096 }
Barry Mienydd671972010-10-04 16:33:58 +020097
Derek Allard2067d1a2008-11-13 22:59:24 +000098 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020099
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300101 * Set route mapping
Derek Allard2067d1a2008-11-13 22:59:24 +0000102 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300103 * Determines what should be served based on the URI request,
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 * as well as any "routes" that have been set in the routing config file.
105 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000106 * @return void
107 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200108 public function _set_routing()
Barry Mienydd671972010-10-04 16:33:58 +0200109 {
Andrey Andreevba6c0412012-01-07 21:10:09 +0200110 // Are query strings enabled in the config file? Normally CI doesn't utilize query strings
Barry Mienydd671972010-10-04 16:33:58 +0200111 // since URI segments are more search-engine friendly, but they can optionally be used.
Derek Jonesc7738402010-03-02 13:55:13 -0600112 // If this feature is enabled, we will gather the directory/class/method a little differently
113 $segments = array();
Andrey Andreev7b53d042012-03-26 23:02:32 +0300114 if ($this->config->item('enable_query_strings') === TRUE && isset($_GET[$this->config->item('controller_trigger')]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 {
Derek Jonesc7738402010-03-02 13:55:13 -0600116 if (isset($_GET[$this->config->item('directory_trigger')]))
117 {
118 $this->set_directory(trim($this->uri->_filter_uri($_GET[$this->config->item('directory_trigger')])));
119 $segments[] = $this->fetch_directory();
120 }
Barry Mienydd671972010-10-04 16:33:58 +0200121
Derek Jonesc7738402010-03-02 13:55:13 -0600122 if (isset($_GET[$this->config->item('controller_trigger')]))
123 {
124 $this->set_class(trim($this->uri->_filter_uri($_GET[$this->config->item('controller_trigger')])));
125 $segments[] = $this->fetch_class();
126 }
Barry Mienydd671972010-10-04 16:33:58 +0200127
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 if (isset($_GET[$this->config->item('function_trigger')]))
129 {
130 $this->set_method(trim($this->uri->_filter_uri($_GET[$this->config->item('function_trigger')])));
Derek Jonesc7738402010-03-02 13:55:13 -0600131 $segments[] = $this->fetch_method();
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 }
Barry Mienydd671972010-10-04 16:33:58 +0200134
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 // Load the routes.php file.
Andrey Andreev7b53d042012-03-26 23:02:32 +0300136 if (defined('ENVIRONMENT') && is_file(APPPATH.'config/'.ENVIRONMENT.'/routes.php'))
Greg Akerd96f8822011-12-27 16:23:47 -0600137 {
138 include(APPPATH.'config/'.ENVIRONMENT.'/routes.php');
139 }
140 elseif (is_file(APPPATH.'config/routes.php'))
141 {
142 include(APPPATH.'config/routes.php');
143 }
David Behler07b53422011-08-15 00:25:06 +0200144
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 $this->routes = ( ! isset($route) OR ! is_array($route)) ? array() : $route;
146 unset($route);
Barry Mienydd671972010-10-04 16:33:58 +0200147
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 // Set the default controller so we can display it in the event
149 // the URI doesn't correlated to a valid controller.
Andrey Andreev7b53d042012-03-26 23:02:32 +0300150 $this->default_controller = empty($this->routes['default_controller']) ? FALSE : strtolower($this->routes['default_controller']);
Barry Mienydd671972010-10-04 16:33:58 +0200151
Andrey Andreevba6c0412012-01-07 21:10:09 +0200152 // 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 -0600153 if (count($segments) > 0)
154 {
155 return $this->_validate_request($segments);
156 }
Barry Mienydd671972010-10-04 16:33:58 +0200157
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 // Fetch the complete URI string
159 $this->uri->_fetch_uri_string();
Barry Mienydd671972010-10-04 16:33:58 +0200160
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 // 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 +0300162 if ($this->uri->uri_string == '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 {
Derek Jonesc7738402010-03-02 13:55:13 -0600164 return $this->_set_default_controller();
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 }
Barry Mienydd671972010-10-04 16:33:58 +0200166
Andrey Andreevba6c0412012-01-07 21:10:09 +0200167 $this->uri->_remove_url_suffix(); // Remove the URL suffix
168 $this->uri->_explode_segments(); // Compile the segments into an array
169 $this->_parse_routes(); // Parse any custom routing that may exist
170 $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 +0000171 }
Derek Jonesc7738402010-03-02 13:55:13 -0600172
173 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200174
Derek Jonesc7738402010-03-02 13:55:13 -0600175 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300176 * Set default controller
Derek Jonesc7738402010-03-02 13:55:13 -0600177 *
Derek Jonesc7738402010-03-02 13:55:13 -0600178 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200179 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200180 protected function _set_default_controller()
Derek Jonesc7738402010-03-02 13:55:13 -0600181 {
182 if ($this->default_controller === FALSE)
183 {
Andrey Andreevba6c0412012-01-07 21:10:09 +0200184 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 -0600185 }
Andrey Andreevd1097a12012-11-01 19:55:42 +0200186
Derek Jonesc7738402010-03-02 13:55:13 -0600187 // Is the method being specified?
188 if (strpos($this->default_controller, '/') !== FALSE)
189 {
190 $x = explode('/', $this->default_controller);
Derek Jonesc7738402010-03-02 13:55:13 -0600191 $this->set_class($x[0]);
192 $this->set_method($x[1]);
Pascal Kriete790ebf32010-12-15 10:53:35 -0500193 $this->_set_request($x);
Barry Mienydd671972010-10-04 16:33:58 +0200194 }
Derek Jonesc7738402010-03-02 13:55:13 -0600195 else
196 {
197 $this->set_class($this->default_controller);
198 $this->set_method('index');
199 $this->_set_request(array($this->default_controller, 'index'));
200 }
Barry Mienydd671972010-10-04 16:33:58 +0200201
Derek Jonesc7738402010-03-02 13:55:13 -0600202 // re-index the routed segments array so it starts with 1 rather than 0
203 $this->uri->_reindex_segments();
Barry Mienydd671972010-10-04 16:33:58 +0200204
Andrey Andreevba6c0412012-01-07 21:10:09 +0200205 log_message('debug', 'No URI present. Default controller set.');
Derek Jonesc7738402010-03-02 13:55:13 -0600206 }
Barry Mienydd671972010-10-04 16:33:58 +0200207
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200209
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300211 * Set request route
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300213 * Takes an array of URI segments as input and sets the class/method
214 * to be called.
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300216 * @param array $segments URI segments
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 * @return void
218 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200219 protected function _set_request($segments = array())
Barry Mienydd671972010-10-04 16:33:58 +0200220 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 $segments = $this->_validate_request($segments);
Barry Mienydd671972010-10-04 16:33:58 +0200222
Andrey Andreevba6c0412012-01-07 21:10:09 +0200223 if (count($segments) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 {
Derek Jonesc7738402010-03-02 13:55:13 -0600225 return $this->_set_default_controller();
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 }
Barry Mienydd671972010-10-04 16:33:58 +0200227
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 $this->set_class($segments[0]);
Barry Mienydd671972010-10-04 16:33:58 +0200229
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 if (isset($segments[1]))
231 {
Derek Jonesc7738402010-03-02 13:55:13 -0600232 // A standard method request
233 $this->set_method($segments[1]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 }
235 else
236 {
237 // This lets the "routed" segment array identify that the default
238 // index method is being used.
239 $segments[1] = 'index';
240 }
Barry Mienydd671972010-10-04 16:33:58 +0200241
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 // Update our "routed" segment array to contain the segments.
243 // Note: If there is no custom routing, this array will be
Phil Sturgeon81aa94b2012-05-02 11:40:46 +0100244 // identical to $this->uri->segments
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 $this->uri->rsegments = $segments;
246 }
Barry Mienydd671972010-10-04 16:33:58 +0200247
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200249
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300251 * Validate request
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300253 * Attempts validate the URI request and determine the controller path.
254 *
255 * @param array $segments URI segments
256 * @return array URI segments
Barry Mienydd671972010-10-04 16:33:58 +0200257 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200258 protected function _validate_request($segments)
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 {
Andrey Andreevba6c0412012-01-07 21:10:09 +0200260 if (count($segments) === 0)
Derek Jonesc7738402010-03-02 13:55:13 -0600261 {
262 return $segments;
263 }
Barry Mienydd671972010-10-04 16:33:58 +0200264
Andrey Andreevd1097a12012-11-01 19:55:42 +0200265 $temp = str_replace('-', '_', $segments[0]);
266
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 // Does the requested controller exist in the root folder?
Andrey Andreevd1097a12012-11-01 19:55:42 +0200268 if (file_exists(APPPATH.'controllers/'.$temp.'.php'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 {
Andrey Andreevd1097a12012-11-01 19:55:42 +0200270 $segments[0] = $temp;
271 empty($segments[1]) OR $segments[1] = str_replace('-', '_', $segments[1]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 return $segments;
273 }
Barry Mienydd671972010-10-04 16:33:58 +0200274
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 // Is the controller in a sub-folder?
276 if (is_dir(APPPATH.'controllers/'.$segments[0]))
Derek Jonesc7738402010-03-02 13:55:13 -0600277 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 // Set the directory and remove it from the segment array
279 $this->set_directory($segments[0]);
280 $segments = array_slice($segments, 1);
Barry Mienydd671972010-10-04 16:33:58 +0200281
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 if (count($segments) > 0)
283 {
Andrey Andreevd1097a12012-11-01 19:55:42 +0200284 $segments[0] = str_replace('-', '_', $segments[0]);
285 empty($segments[1]) OR $segments[1] = str_replace('-', '_', $segments[1]);
286
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 // Does the requested controller exist in the sub-folder?
Greg Aker3a746652011-04-19 10:59:47 -0500288 if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].'.php'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 {
Shane Pearson664a9352011-08-10 16:02:32 -0500290 if ( ! empty($this->routes['404_override']))
291 {
292 $x = explode('/', $this->routes['404_override']);
Shane Pearson664a9352011-08-10 16:02:32 -0500293 $this->set_directory('');
294 $this->set_class($x[0]);
295 $this->set_method(isset($x[1]) ? $x[1] : 'index');
David Behler07b53422011-08-15 00:25:06 +0200296
Shane Pearson664a9352011-08-10 16:02:32 -0500297 return $x;
298 }
299 else
300 {
301 show_404($this->fetch_directory().$segments[0]);
302 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 }
304 }
305 else
306 {
Derek Jonesc7738402010-03-02 13:55:13 -0600307 // Is the method being specified in the route?
308 if (strpos($this->default_controller, '/') !== FALSE)
309 {
310 $x = explode('/', $this->default_controller);
Derek Jonesc7738402010-03-02 13:55:13 -0600311 $this->set_class($x[0]);
312 $this->set_method($x[1]);
Barry Mienydd671972010-10-04 16:33:58 +0200313 }
Derek Jonesc7738402010-03-02 13:55:13 -0600314 else
315 {
316 $this->set_class($this->default_controller);
317 $this->set_method('index');
318 }
Barry Mienydd671972010-10-04 16:33:58 +0200319
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 // Does the default controller exist in the sub-folder?
Greg Aker3a746652011-04-19 10:59:47 -0500321 if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.'.php'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 {
323 $this->directory = '';
324 return array();
325 }
Barry Mienydd671972010-10-04 16:33:58 +0200326
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 }
Barry Mienydd671972010-10-04 16:33:58 +0200328
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 return $segments;
330 }
Barry Mienydd671972010-10-04 16:33:58 +0200331
332
Derek Jonesc7738402010-03-02 13:55:13 -0600333 // If we've gotten this far it means that the URI does not correlate to a valid
Andrey Andreevba6c0412012-01-07 21:10:09 +0200334 // controller class. We will now see if there is an override
Eric Barnesc5bf6162011-01-30 21:17:11 -0500335 if ( ! empty($this->routes['404_override']))
Derek Jonesc7738402010-03-02 13:55:13 -0600336 {
Phil Sturgeon23174a62010-12-15 15:18:16 +0000337 $x = explode('/', $this->routes['404_override']);
Phil Sturgeon23174a62010-12-15 15:18:16 +0000338 $this->set_class($x[0]);
339 $this->set_method(isset($x[1]) ? $x[1] : 'index');
Barry Mienydd671972010-10-04 16:33:58 +0200340
Phil Sturgeon23174a62010-12-15 15:18:16 +0000341 return $x;
Derek Jonesc7738402010-03-02 13:55:13 -0600342 }
Barry Mienydd671972010-10-04 16:33:58 +0200343
Derek Jonesc7738402010-03-02 13:55:13 -0600344 // Nothing else to do at this point but show a 404
Barry Mienydd671972010-10-04 16:33:58 +0200345 show_404($segments[0]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 }
Barry Mienydd671972010-10-04 16:33:58 +0200347
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200349
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 /**
Andrey Andreev7b53d042012-03-26 23:02:32 +0300351 * Parse Routes
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300353 * Matches any routes that may exist in the config/routes.php file
354 * against the URI to determine if the class/method need to be remapped.
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 * @return void
357 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200358 protected function _parse_routes()
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100359 {
360 // Turn the segment array into a URI string
361 $uri = implode('/', $this->uri->segments);
Barry Mienydd671972010-10-04 16:33:58 +0200362
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100363 // Is there a literal match? If so we're done
364 if (isset($this->routes[$uri]))
365 {
366 return $this->_set_request(explode('/', $this->routes[$uri]));
367 }
Barry Mienydd671972010-10-04 16:33:58 +0200368
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100369 // Loop through the route array looking for wild-cards
370 foreach ($this->routes as $key => $val)
371 {
372 // Convert wild-cards to RegEx
Andrey Andreev7676c2d2012-10-30 13:42:01 +0200373 $key = str_replace(array(':any', ':num'), array('[^/]+', '[0-9]+'), $key);
Derek Jonesc7738402010-03-02 13:55:13 -0600374
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100375 // Does the RegEx match?
376 if (preg_match('#^'.$key.'$#', $uri, $matches))
377 {
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100378 // Are we using callbacks to process back-references?
Jonatas Miguel24296ca2012-09-27 12:10:01 +0100379 if ( ! is_string($val) && is_callable($val))
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100380 {
381 // Remove the original string from the matches array.
382 array_shift($matches);
Derek Allard2067d1a2008-11-13 22:59:24 +0000383
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100384 // Get the match count.
385 $match_count = count($matches);
Jonatas Miguelc0d98b22012-08-06 15:42:50 +0100386
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100387 // Determine how many parameters the callback has.
388 $reflection = new ReflectionFunction($val);
389 $param_count = $reflection->getNumberOfParameters();
Jonatas Miguelc0d98b22012-08-06 15:42:50 +0100390
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100391 // Are there more parameters than matches?
Jonatas Miguel24296ca2012-09-27 12:10:01 +0100392 if ($param_count > $match_count)
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100393 {
394 // Any params without matches will be set to an empty string.
395 $matches = array_merge($matches, array_fill($match_count, $param_count - $match_count, ''));
Jonatas Miguelefb81662012-10-23 19:52:46 +0100396
397 $match_count = $param_count;
398 }
399
400 // Get the parameters so we can use their default values.
401 $params = $reflection->getParameters();
402
403 for ($m = 0; $m < $match_count; $m++)
404 {
405 // Is the match empty and does a default value exist?
406 if (empty($matches[$m]) && $params[$m]->isDefaultValueAvailable())
407 {
408 // Substitute the empty match for the default value.
409 $matches[$m] = $params[$m]->getDefaultValue();
410 }
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100411 }
Jonatas Miguelc0d98b22012-08-06 15:42:50 +0100412
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100413 // Execute the callback using the values in matches as its parameters.
414 $val = call_user_func_array($val, $matches);
415 }
416 // Are we using the default routing method for back-references?
Jonatas Miguelf950e5c2012-09-27 11:39:33 +0100417 elseif (strpos($val, '$') !== FALSE && strpos($key, '(') !== FALSE)
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100418 {
419 $val = preg_replace('#^'.$key.'$#', $val, $uri);
420 }
Jonatas Miguelc0d98b22012-08-06 15:42:50 +0100421
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100422 return $this->_set_request(explode('/', $val));
423 }
424 }
Jonatas Miguelc0d98b22012-08-06 15:42:50 +0100425
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100426 // If we got this far it means we didn't encounter a
427 // matching route so we'll set the site default route
428 $this->_set_request($this->uri->segments);
429 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000430
431 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200432
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300434 * Set class name
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300436 * @param string $class Class name
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200438 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200439 public function set_class($class)
Derek Allard2067d1a2008-11-13 22:59:24 +0000440 {
Derek Jones2615e412010-10-06 17:51:16 -0500441 $this->class = str_replace(array('/', '.'), '', $class);
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 }
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 /**
447 * Fetch the current class
448 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200450 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200451 public function fetch_class()
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 {
453 return $this->class;
454 }
Barry Mienydd671972010-10-04 16:33:58 +0200455
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200457
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300459 * Set method name
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300461 * @param string $method Method name
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200463 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200464 public function set_method($method)
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 {
466 $this->method = $method;
467 }
468
469 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200470
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 /**
Andrey Andreev7b53d042012-03-26 23:02:32 +0300472 * Fetch the current method
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200475 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200476 public function fetch_method()
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 {
Alex Bilbieed944a32012-06-02 11:07:47 +0100478 return ($this->method === $this->fetch_class()) ? 'index' : $this->method;
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 }
480
481 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200482
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300484 * Set directory name
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300486 * @param string $dir Directory name
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200488 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200489 public function set_directory($dir)
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 {
Derek Jones2615e412010-10-06 17:51:16 -0500491 $this->directory = str_replace(array('/', '.'), '', $dir).'/';
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 }
493
494 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200495
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300497 * Fetch directory
498 *
499 * Feches the sub-directory (if any) that contains the requested
500 * controller class.
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000502 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200503 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200504 public function fetch_directory()
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 {
506 return $this->directory;
507 }
508
Derek Jonesc7738402010-03-02 13:55:13 -0600509 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200510
Derek Jonesc7738402010-03-02 13:55:13 -0600511 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300512 * Set controller overrides
Derek Jonesc7738402010-03-02 13:55:13 -0600513 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300514 * @param array $routing Route overrides
Andrey Andreev7b53d042012-03-26 23:02:32 +0300515 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200516 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200517 public function _set_overrides($routing)
Derek Jonesc7738402010-03-02 13:55:13 -0600518 {
519 if ( ! is_array($routing))
520 {
521 return;
522 }
Barry Mienydd671972010-10-04 16:33:58 +0200523
Derek Jonesc7738402010-03-02 13:55:13 -0600524 if (isset($routing['directory']))
525 {
526 $this->set_directory($routing['directory']);
527 }
Barry Mienydd671972010-10-04 16:33:58 +0200528
Andrey Andreev9ba661b2012-06-04 14:44:34 +0300529 if ( ! empty($routing['controller']))
Derek Jonesc7738402010-03-02 13:55:13 -0600530 {
531 $this->set_class($routing['controller']);
532 }
Barry Mienydd671972010-10-04 16:33:58 +0200533
Derek Jonesc7738402010-03-02 13:55:13 -0600534 if (isset($routing['function']))
535 {
Andrey Andreev9ba661b2012-06-04 14:44:34 +0300536 $routing['function'] = ($routing['function'] == '') ? 'index' : $routing['function'];
Derek Jonesc7738402010-03-02 13:55:13 -0600537 $this->set_method($routing['function']);
538 }
539 }
540
Derek Allard2067d1a2008-11-13 22:59:24 +0000541}
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 */