blob: 87f3e9e638712b7bcd798ddc9d7efb4502ea06cb [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 }
193 // Is the method being specified?
194 if (strpos($this->default_controller, '/') !== FALSE)
195 {
196 $x = explode('/', $this->default_controller);
Derek Jonesc7738402010-03-02 13:55:13 -0600197 $this->set_class($x[0]);
198 $this->set_method($x[1]);
Pascal Kriete790ebf32010-12-15 10:53:35 -0500199 $this->_set_request($x);
Barry Mienydd671972010-10-04 16:33:58 +0200200 }
Derek Jonesc7738402010-03-02 13:55:13 -0600201 else
202 {
203 $this->set_class($this->default_controller);
204 $this->set_method('index');
205 $this->_set_request(array($this->default_controller, 'index'));
206 }
Barry Mienydd671972010-10-04 16:33:58 +0200207
Derek Jonesc7738402010-03-02 13:55:13 -0600208 // re-index the routed segments array so it starts with 1 rather than 0
209 $this->uri->_reindex_segments();
Barry Mienydd671972010-10-04 16:33:58 +0200210
Andrey Andreevba6c0412012-01-07 21:10:09 +0200211 log_message('debug', 'No URI present. Default controller set.');
Derek Jonesc7738402010-03-02 13:55:13 -0600212 }
Barry Mienydd671972010-10-04 16:33:58 +0200213
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200215
Derek Allard2067d1a2008-11-13 22:59:24 +0000216 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300217 * Set request route
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300219 * Takes an array of URI segments as input and sets the class/method
220 * to be called.
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300222 * @param array $segments URI segments
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 * @return void
224 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200225 protected function _set_request($segments = array())
Barry Mienydd671972010-10-04 16:33:58 +0200226 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 $segments = $this->_validate_request($segments);
Barry Mienydd671972010-10-04 16:33:58 +0200228
Andrey Andreevba6c0412012-01-07 21:10:09 +0200229 if (count($segments) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 {
Derek Jonesc7738402010-03-02 13:55:13 -0600231 return $this->_set_default_controller();
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 }
Barry Mienydd671972010-10-04 16:33:58 +0200233
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 $this->set_class($segments[0]);
Barry Mienydd671972010-10-04 16:33:58 +0200235
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 if (isset($segments[1]))
237 {
Derek Jonesc7738402010-03-02 13:55:13 -0600238 // A standard method request
239 $this->set_method($segments[1]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 }
241 else
242 {
243 // This lets the "routed" segment array identify that the default
244 // index method is being used.
245 $segments[1] = 'index';
246 }
Barry Mienydd671972010-10-04 16:33:58 +0200247
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 // Update our "routed" segment array to contain the segments.
249 // Note: If there is no custom routing, this array will be
Phil Sturgeon81aa94b2012-05-02 11:40:46 +0100250 // identical to $this->uri->segments
Derek Allard2067d1a2008-11-13 22:59:24 +0000251 $this->uri->rsegments = $segments;
252 }
Barry Mienydd671972010-10-04 16:33:58 +0200253
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200255
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300257 * Validate request
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300259 * Attempts validate the URI request and determine the controller path.
260 *
261 * @param array $segments URI segments
262 * @return array URI segments
Barry Mienydd671972010-10-04 16:33:58 +0200263 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200264 protected function _validate_request($segments)
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 {
Andrey Andreevba6c0412012-01-07 21:10:09 +0200266 if (count($segments) === 0)
Derek Jonesc7738402010-03-02 13:55:13 -0600267 {
268 return $segments;
269 }
Barry Mienydd671972010-10-04 16:33:58 +0200270
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 // Does the requested controller exist in the root folder?
Greg Aker3a746652011-04-19 10:59:47 -0500272 if (file_exists(APPPATH.'controllers/'.$segments[0].'.php'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 {
274 return $segments;
275 }
Barry Mienydd671972010-10-04 16:33:58 +0200276
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 // Is the controller in a sub-folder?
278 if (is_dir(APPPATH.'controllers/'.$segments[0]))
Derek Jonesc7738402010-03-02 13:55:13 -0600279 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 // Set the directory and remove it from the segment array
281 $this->set_directory($segments[0]);
282 $segments = array_slice($segments, 1);
Barry Mienydd671972010-10-04 16:33:58 +0200283
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 if (count($segments) > 0)
285 {
286 // Does the requested controller exist in the sub-folder?
Greg Aker3a746652011-04-19 10:59:47 -0500287 if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].'.php'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 {
Shane Pearson664a9352011-08-10 16:02:32 -0500289 if ( ! empty($this->routes['404_override']))
290 {
291 $x = explode('/', $this->routes['404_override']);
Shane Pearson664a9352011-08-10 16:02:32 -0500292 $this->set_directory('');
293 $this->set_class($x[0]);
294 $this->set_method(isset($x[1]) ? $x[1] : 'index');
David Behler07b53422011-08-15 00:25:06 +0200295
Shane Pearson664a9352011-08-10 16:02:32 -0500296 return $x;
297 }
298 else
299 {
300 show_404($this->fetch_directory().$segments[0]);
301 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 }
303 }
304 else
305 {
Derek Jonesc7738402010-03-02 13:55:13 -0600306 // Is the method being specified in the route?
307 if (strpos($this->default_controller, '/') !== FALSE)
308 {
309 $x = explode('/', $this->default_controller);
Derek Jonesc7738402010-03-02 13:55:13 -0600310 $this->set_class($x[0]);
311 $this->set_method($x[1]);
Barry Mienydd671972010-10-04 16:33:58 +0200312 }
Derek Jonesc7738402010-03-02 13:55:13 -0600313 else
314 {
315 $this->set_class($this->default_controller);
316 $this->set_method('index');
317 }
Barry Mienydd671972010-10-04 16:33:58 +0200318
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 // Does the default controller exist in the sub-folder?
Greg Aker3a746652011-04-19 10:59:47 -0500320 if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.'.php'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 {
322 $this->directory = '';
323 return array();
324 }
Barry Mienydd671972010-10-04 16:33:58 +0200325
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 }
Barry Mienydd671972010-10-04 16:33:58 +0200327
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 return $segments;
329 }
Barry Mienydd671972010-10-04 16:33:58 +0200330
331
Derek Jonesc7738402010-03-02 13:55:13 -0600332 // If we've gotten this far it means that the URI does not correlate to a valid
Andrey Andreevba6c0412012-01-07 21:10:09 +0200333 // controller class. We will now see if there is an override
Eric Barnesc5bf6162011-01-30 21:17:11 -0500334 if ( ! empty($this->routes['404_override']))
Derek Jonesc7738402010-03-02 13:55:13 -0600335 {
Phil Sturgeon23174a62010-12-15 15:18:16 +0000336 $x = explode('/', $this->routes['404_override']);
Phil Sturgeon23174a62010-12-15 15:18:16 +0000337 $this->set_class($x[0]);
338 $this->set_method(isset($x[1]) ? $x[1] : 'index');
Barry Mienydd671972010-10-04 16:33:58 +0200339
Phil Sturgeon23174a62010-12-15 15:18:16 +0000340 return $x;
Derek Jonesc7738402010-03-02 13:55:13 -0600341 }
Barry Mienydd671972010-10-04 16:33:58 +0200342
Derek Jonesc7738402010-03-02 13:55:13 -0600343 // Nothing else to do at this point but show a 404
Barry Mienydd671972010-10-04 16:33:58 +0200344 show_404($segments[0]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 }
Barry Mienydd671972010-10-04 16:33:58 +0200346
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200348
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 /**
Andrey Andreev7b53d042012-03-26 23:02:32 +0300350 * Parse Routes
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300352 * Matches any routes that may exist in the config/routes.php file
353 * against the URI to determine if the class/method need to be remapped.
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 * @return void
356 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200357 protected function _parse_routes()
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100358 {
359 // Turn the segment array into a URI string
360 $uri = implode('/', $this->uri->segments);
Barry Mienydd671972010-10-04 16:33:58 +0200361
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100362 // Is there a literal match? If so we're done
363 if (isset($this->routes[$uri]))
364 {
365 return $this->_set_request(explode('/', $this->routes[$uri]));
366 }
Barry Mienydd671972010-10-04 16:33:58 +0200367
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100368 // Loop through the route array looking for wild-cards
369 foreach ($this->routes as $key => $val)
370 {
371 // Convert wild-cards to RegEx
Andrey Andreev7676c2d2012-10-30 13:42:01 +0200372 $key = str_replace(array(':any', ':num'), array('[^/]+', '[0-9]+'), $key);
Derek Jonesc7738402010-03-02 13:55:13 -0600373
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100374 // Does the RegEx match?
375 if (preg_match('#^'.$key.'$#', $uri, $matches))
376 {
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100377 // Are we using callbacks to process back-references?
Jonatas Miguel24296ca2012-09-27 12:10:01 +0100378 if ( ! is_string($val) && is_callable($val))
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100379 {
380 // Remove the original string from the matches array.
381 array_shift($matches);
Derek Allard2067d1a2008-11-13 22:59:24 +0000382
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100383 // Get the match count.
384 $match_count = count($matches);
Jonatas Miguelc0d98b22012-08-06 15:42:50 +0100385
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100386 // Determine how many parameters the callback has.
387 $reflection = new ReflectionFunction($val);
388 $param_count = $reflection->getNumberOfParameters();
Jonatas Miguelc0d98b22012-08-06 15:42:50 +0100389
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100390 // Are there more parameters than matches?
Jonatas Miguel24296ca2012-09-27 12:10:01 +0100391 if ($param_count > $match_count)
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100392 {
393 // Any params without matches will be set to an empty string.
394 $matches = array_merge($matches, array_fill($match_count, $param_count - $match_count, ''));
Jonatas Miguelefb81662012-10-23 19:52:46 +0100395
396 $match_count = $param_count;
397 }
398
399 // Get the parameters so we can use their default values.
400 $params = $reflection->getParameters();
401
402 for ($m = 0; $m < $match_count; $m++)
403 {
404 // Is the match empty and does a default value exist?
405 if (empty($matches[$m]) && $params[$m]->isDefaultValueAvailable())
406 {
407 // Substitute the empty match for the default value.
408 $matches[$m] = $params[$m]->getDefaultValue();
409 }
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100410 }
Jonatas Miguelc0d98b22012-08-06 15:42:50 +0100411
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100412 // Execute the callback using the values in matches as its parameters.
413 $val = call_user_func_array($val, $matches);
414 }
415 // Are we using the default routing method for back-references?
Jonatas Miguelf950e5c2012-09-27 11:39:33 +0100416 elseif (strpos($val, '$') !== FALSE && strpos($key, '(') !== FALSE)
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100417 {
418 $val = preg_replace('#^'.$key.'$#', $val, $uri);
419 }
Jonatas Miguelc0d98b22012-08-06 15:42:50 +0100420
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100421 return $this->_set_request(explode('/', $val));
422 }
423 }
Jonatas Miguelc0d98b22012-08-06 15:42:50 +0100424
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100425 // If we got this far it means we didn't encounter a
426 // matching route so we'll set the site default route
427 $this->_set_request($this->uri->segments);
428 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000429
430 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200431
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300433 * Set class name
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300435 * @param string $class Class name
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200437 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200438 public function set_class($class)
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 {
Derek Jones2615e412010-10-06 17:51:16 -0500440 $this->class = str_replace(array('/', '.'), '', $class);
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 }
Barry Mienydd671972010-10-04 16:33:58 +0200442
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200444
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 /**
446 * Fetch the current class
447 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200449 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200450 public function fetch_class()
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 {
452 return $this->class;
453 }
Barry Mienydd671972010-10-04 16:33:58 +0200454
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200456
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300458 * Set method name
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300460 * @param string $method Method name
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200462 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200463 public function set_method($method)
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 {
465 $this->method = $method;
466 }
467
468 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200469
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 /**
Andrey Andreev7b53d042012-03-26 23:02:32 +0300471 * Fetch the current method
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200474 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200475 public function fetch_method()
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 {
Alex Bilbieed944a32012-06-02 11:07:47 +0100477 return ($this->method === $this->fetch_class()) ? 'index' : $this->method;
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 }
479
480 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200481
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300483 * Set directory name
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300485 * @param string $dir Directory name
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200487 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200488 public function set_directory($dir)
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 {
Derek Jones2615e412010-10-06 17:51:16 -0500490 $this->directory = str_replace(array('/', '.'), '', $dir).'/';
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 }
492
493 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200494
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300496 * Fetch directory
497 *
498 * Feches the sub-directory (if any) that contains the requested
499 * controller class.
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200502 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200503 public function fetch_directory()
Derek Allard2067d1a2008-11-13 22:59:24 +0000504 {
505 return $this->directory;
506 }
507
Derek Jonesc7738402010-03-02 13:55:13 -0600508 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200509
Derek Jonesc7738402010-03-02 13:55:13 -0600510 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300511 * Set controller overrides
Derek Jonesc7738402010-03-02 13:55:13 -0600512 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300513 * @param array $routing Route overrides
Andrey Andreev7b53d042012-03-26 23:02:32 +0300514 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200515 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200516 public function _set_overrides($routing)
Derek Jonesc7738402010-03-02 13:55:13 -0600517 {
518 if ( ! is_array($routing))
519 {
520 return;
521 }
Barry Mienydd671972010-10-04 16:33:58 +0200522
Derek Jonesc7738402010-03-02 13:55:13 -0600523 if (isset($routing['directory']))
524 {
525 $this->set_directory($routing['directory']);
526 }
Barry Mienydd671972010-10-04 16:33:58 +0200527
Andrey Andreev9ba661b2012-06-04 14:44:34 +0300528 if ( ! empty($routing['controller']))
Derek Jonesc7738402010-03-02 13:55:13 -0600529 {
530 $this->set_class($routing['controller']);
531 }
Barry Mienydd671972010-10-04 16:33:58 +0200532
Derek Jonesc7738402010-03-02 13:55:13 -0600533 if (isset($routing['function']))
534 {
Andrey Andreev9ba661b2012-06-04 14:44:34 +0300535 $routing['function'] = ($routing['function'] == '') ? 'index' : $routing['function'];
Derek Jonesc7738402010-03-02 13:55:13 -0600536 $this->set_method($routing['function']);
537 }
538 }
539
Derek Allard2067d1a2008-11-13 22:59:24 +0000540}
Derek Allard2067d1a2008-11-13 22:59:24 +0000541
542/* End of file Router.php */
Andrey Andreev7b53d042012-03-26 23:02:32 +0300543/* Location: ./system/core/Router.php */