blob: 727e85f7ee549a12f0b84e12bd99480872a02144 [file] [log] [blame]
Andrey Andreevba6c0412012-01-07 21:10:09 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
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 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * Router Class
30 *
31 * Parses URIs and determines routing
32 *
33 * @package CodeIgniter
34 * @subpackage Libraries
Derek Allard2067d1a2008-11-13 22:59:24 +000035 * @category Libraries
Andrey Andreev92ebfb62012-05-17 12:49:24 +030036 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000037 * @link http://codeigniter.com/user_guide/general/routing.html
38 */
39class CI_Router {
40
David Behler07b53422011-08-15 00:25:06 +020041 /**
42 * Config class
43 *
44 * @var object
David Behler07b53422011-08-15 00:25:06 +020045 */
Andrey Andreevba6c0412012-01-07 21:10:09 +020046 public $config;
Andrey Andreev92ebfb62012-05-17 12:49:24 +030047
David Behler07b53422011-08-15 00:25:06 +020048 /**
49 * List of routes
50 *
51 * @var array
David Behler07b53422011-08-15 00:25:06 +020052 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040053 public $routes = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +030054
David Behler07b53422011-08-15 00:25:06 +020055 /**
56 * List of error routes
57 *
58 * @var array
David Behler07b53422011-08-15 00:25:06 +020059 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040060 public $error_routes = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +030061
David Behler07b53422011-08-15 00:25:06 +020062 /**
63 * Current class name
64 *
65 * @var string
David Behler07b53422011-08-15 00:25:06 +020066 */
Andrey Andreev92ebfb62012-05-17 12:49:24 +030067 public $class = '';
68
David Behler07b53422011-08-15 00:25:06 +020069 /**
70 * Current method name
71 *
72 * @var string
David Behler07b53422011-08-15 00:25:06 +020073 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040074 public $method = 'index';
Andrey Andreev92ebfb62012-05-17 12:49:24 +030075
David Behler07b53422011-08-15 00:25:06 +020076 /**
77 * Sub-directory that contains the requested controller class
78 *
79 * @var string
David Behler07b53422011-08-15 00:25:06 +020080 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040081 public $directory = '';
Andrey Andreev92ebfb62012-05-17 12:49:24 +030082
David Behler07b53422011-08-15 00:25:06 +020083 /**
84 * Default controller (and method if specific)
85 *
86 * @var string
David Behler07b53422011-08-15 00:25:06 +020087 */
Andrey Andreevba6c0412012-01-07 21:10:09 +020088 public $default_controller;
Jonatas Miguela101c932012-07-20 12:23:43 +020089
90 /**
91 * Prepend used in php processed routes
92 *
93 * @var string
94 */
95 public $route_prepend = 'php:';
Barry Mienydd671972010-10-04 16:33:58 +020096
Derek Allard2067d1a2008-11-13 22:59:24 +000097 /**
98 * Constructor
99 *
100 * Runs the route mapping function.
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300101 *
102 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000103 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200104 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +0000105 {
Derek Jonesc7738402010-03-02 13:55:13 -0600106 $this->config =& load_class('Config', 'core');
107 $this->uri =& load_class('URI', 'core');
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 /**
114 * Set the route mapping
115 *
116 * This function determines what should be served based on the URI request,
117 * 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 Andreevba6c0412012-01-07 21:10:09 +0200121 public 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 Andreev7b53d042012-03-26 23:02:32 +0300127 if ($this->config->item('enable_query_strings') === TRUE && isset($_GET[$this->config->item('controller_trigger')]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 {
Derek Jonesc7738402010-03-02 13:55:13 -0600129 if (isset($_GET[$this->config->item('directory_trigger')]))
130 {
131 $this->set_directory(trim($this->uri->_filter_uri($_GET[$this->config->item('directory_trigger')])));
132 $segments[] = $this->fetch_directory();
133 }
Barry Mienydd671972010-10-04 16:33:58 +0200134
Derek Jonesc7738402010-03-02 13:55:13 -0600135 if (isset($_GET[$this->config->item('controller_trigger')]))
136 {
137 $this->set_class(trim($this->uri->_filter_uri($_GET[$this->config->item('controller_trigger')])));
138 $segments[] = $this->fetch_class();
139 }
Barry Mienydd671972010-10-04 16:33:58 +0200140
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 if (isset($_GET[$this->config->item('function_trigger')]))
142 {
143 $this->set_method(trim($this->uri->_filter_uri($_GET[$this->config->item('function_trigger')])));
Derek Jonesc7738402010-03-02 13:55:13 -0600144 $segments[] = $this->fetch_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 Andreev7b53d042012-03-26 23:02:32 +0300149 if (defined('ENVIRONMENT') && is_file(APPPATH.'config/'.ENVIRONMENT.'/routes.php'))
Greg Akerd96f8822011-12-27 16:23:47 -0600150 {
151 include(APPPATH.'config/'.ENVIRONMENT.'/routes.php');
152 }
153 elseif (is_file(APPPATH.'config/routes.php'))
154 {
155 include(APPPATH.'config/routes.php');
156 }
David Behler07b53422011-08-15 00:25:06 +0200157
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 $this->routes = ( ! isset($route) OR ! is_array($route)) ? array() : $route;
159 unset($route);
Barry Mienydd671972010-10-04 16:33:58 +0200160
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 // Set the default controller so we can display it in the event
162 // the URI doesn't correlated to a valid controller.
Andrey Andreev7b53d042012-03-26 23:02:32 +0300163 $this->default_controller = empty($this->routes['default_controller']) ? FALSE : strtolower($this->routes['default_controller']);
Barry Mienydd671972010-10-04 16:33:58 +0200164
Andrey Andreevba6c0412012-01-07 21:10:09 +0200165 // 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 -0600166 if (count($segments) > 0)
167 {
168 return $this->_validate_request($segments);
169 }
Barry Mienydd671972010-10-04 16:33:58 +0200170
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 // Fetch the complete URI string
172 $this->uri->_fetch_uri_string();
Barry Mienydd671972010-10-04 16:33:58 +0200173
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 // 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 +0300175 if ($this->uri->uri_string == '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 {
Derek Jonesc7738402010-03-02 13:55:13 -0600177 return $this->_set_default_controller();
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 }
Barry Mienydd671972010-10-04 16:33:58 +0200179
Andrey Andreevba6c0412012-01-07 21:10:09 +0200180 $this->uri->_remove_url_suffix(); // Remove the URL suffix
181 $this->uri->_explode_segments(); // Compile the segments into an array
182 $this->_parse_routes(); // Parse any custom routing that may exist
183 $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 +0000184 }
Derek Jonesc7738402010-03-02 13:55:13 -0600185
186 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200187
Derek Jonesc7738402010-03-02 13:55:13 -0600188 /**
189 * Set the default controller
190 *
Derek Jonesc7738402010-03-02 13:55:13 -0600191 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200192 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200193 protected function _set_default_controller()
Derek Jonesc7738402010-03-02 13:55:13 -0600194 {
195 if ($this->default_controller === FALSE)
196 {
Andrey Andreevba6c0412012-01-07 21:10:09 +0200197 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 -0600198 }
199 // Is the method being specified?
200 if (strpos($this->default_controller, '/') !== FALSE)
201 {
202 $x = explode('/', $this->default_controller);
Derek Jonesc7738402010-03-02 13:55:13 -0600203 $this->set_class($x[0]);
204 $this->set_method($x[1]);
Pascal Kriete790ebf32010-12-15 10:53:35 -0500205 $this->_set_request($x);
Barry Mienydd671972010-10-04 16:33:58 +0200206 }
Derek Jonesc7738402010-03-02 13:55:13 -0600207 else
208 {
209 $this->set_class($this->default_controller);
210 $this->set_method('index');
211 $this->_set_request(array($this->default_controller, 'index'));
212 }
Barry Mienydd671972010-10-04 16:33:58 +0200213
Derek Jonesc7738402010-03-02 13:55:13 -0600214 // re-index the routed segments array so it starts with 1 rather than 0
215 $this->uri->_reindex_segments();
Barry Mienydd671972010-10-04 16:33:58 +0200216
Andrey Andreevba6c0412012-01-07 21:10:09 +0200217 log_message('debug', 'No URI present. Default controller set.');
Derek Jonesc7738402010-03-02 13:55:13 -0600218 }
Barry Mienydd671972010-10-04 16:33:58 +0200219
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200221
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 /**
223 * Set the Route
224 *
225 * This function takes an array of URI segments as
226 * input, and sets the current class/method
227 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 * @param array
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 * @return void
230 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200231 protected function _set_request($segments = array())
Barry Mienydd671972010-10-04 16:33:58 +0200232 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 $segments = $this->_validate_request($segments);
Barry Mienydd671972010-10-04 16:33:58 +0200234
Andrey Andreevba6c0412012-01-07 21:10:09 +0200235 if (count($segments) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 {
Derek Jonesc7738402010-03-02 13:55:13 -0600237 return $this->_set_default_controller();
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 }
Barry Mienydd671972010-10-04 16:33:58 +0200239
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 $this->set_class($segments[0]);
Barry Mienydd671972010-10-04 16:33:58 +0200241
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 if (isset($segments[1]))
243 {
Derek Jonesc7738402010-03-02 13:55:13 -0600244 // A standard method request
245 $this->set_method($segments[1]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 }
247 else
248 {
249 // This lets the "routed" segment array identify that the default
250 // index method is being used.
251 $segments[1] = 'index';
252 }
Barry Mienydd671972010-10-04 16:33:58 +0200253
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 // Update our "routed" segment array to contain the segments.
255 // Note: If there is no custom routing, this array will be
Phil Sturgeon81aa94b2012-05-02 11:40:46 +0100256 // identical to $this->uri->segments
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 $this->uri->rsegments = $segments;
258 }
Barry Mienydd671972010-10-04 16:33:58 +0200259
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200261
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 /**
Andrey Andreev7b53d042012-03-26 23:02:32 +0300263 * Validates the supplied segments.
264 * Attempts to determine the path to the controller.
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000266 * @param array
267 * @return array
Barry Mienydd671972010-10-04 16:33:58 +0200268 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200269 protected function _validate_request($segments)
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 {
Andrey Andreevba6c0412012-01-07 21:10:09 +0200271 if (count($segments) === 0)
Derek Jonesc7738402010-03-02 13:55:13 -0600272 {
273 return $segments;
274 }
Barry Mienydd671972010-10-04 16:33:58 +0200275
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 // Does the requested controller exist in the root folder?
Greg Aker3a746652011-04-19 10:59:47 -0500277 if (file_exists(APPPATH.'controllers/'.$segments[0].'.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
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 {
291 // Does the requested controller exist in the sub-folder?
Greg Aker3a746652011-04-19 10:59:47 -0500292 if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].'.php'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 {
Shane Pearson664a9352011-08-10 16:02:32 -0500294 if ( ! empty($this->routes['404_override']))
295 {
296 $x = explode('/', $this->routes['404_override']);
Shane Pearson664a9352011-08-10 16:02:32 -0500297 $this->set_directory('');
298 $this->set_class($x[0]);
299 $this->set_method(isset($x[1]) ? $x[1] : 'index');
David Behler07b53422011-08-15 00:25:06 +0200300
Shane Pearson664a9352011-08-10 16:02:32 -0500301 return $x;
302 }
303 else
304 {
305 show_404($this->fetch_directory().$segments[0]);
306 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 }
308 }
309 else
310 {
Derek Jonesc7738402010-03-02 13:55:13 -0600311 // Is the method being specified in the route?
312 if (strpos($this->default_controller, '/') !== FALSE)
313 {
314 $x = explode('/', $this->default_controller);
Derek Jonesc7738402010-03-02 13:55:13 -0600315 $this->set_class($x[0]);
316 $this->set_method($x[1]);
Barry Mienydd671972010-10-04 16:33:58 +0200317 }
Derek Jonesc7738402010-03-02 13:55:13 -0600318 else
319 {
320 $this->set_class($this->default_controller);
321 $this->set_method('index');
322 }
Barry Mienydd671972010-10-04 16:33:58 +0200323
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 // Does the default controller exist in the sub-folder?
Greg Aker3a746652011-04-19 10:59:47 -0500325 if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.'.php'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 {
327 $this->directory = '';
328 return array();
329 }
Barry Mienydd671972010-10-04 16:33:58 +0200330
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 }
Barry Mienydd671972010-10-04 16:33:58 +0200332
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 return $segments;
334 }
Barry Mienydd671972010-10-04 16:33:58 +0200335
336
Derek Jonesc7738402010-03-02 13:55:13 -0600337 // If we've gotten this far it means that the URI does not correlate to a valid
Andrey Andreevba6c0412012-01-07 21:10:09 +0200338 // controller class. We will now see if there is an override
Eric Barnesc5bf6162011-01-30 21:17:11 -0500339 if ( ! empty($this->routes['404_override']))
Derek Jonesc7738402010-03-02 13:55:13 -0600340 {
Phil Sturgeon23174a62010-12-15 15:18:16 +0000341 $x = explode('/', $this->routes['404_override']);
Phil Sturgeon23174a62010-12-15 15:18:16 +0000342 $this->set_class($x[0]);
343 $this->set_method(isset($x[1]) ? $x[1] : 'index');
Barry Mienydd671972010-10-04 16:33:58 +0200344
Phil Sturgeon23174a62010-12-15 15:18:16 +0000345 return $x;
Derek Jonesc7738402010-03-02 13:55:13 -0600346 }
Barry Mienydd671972010-10-04 16:33:58 +0200347
Derek Jonesc7738402010-03-02 13:55:13 -0600348 // Nothing else to do at this point but show a 404
Barry Mienydd671972010-10-04 16:33:58 +0200349 show_404($segments[0]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 }
Barry Mienydd671972010-10-04 16:33:58 +0200351
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200353
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 /**
Andrey Andreev7b53d042012-03-26 23:02:32 +0300355 * Parse Routes
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 *
357 * This function matches any routes that may exist in
358 * the config/routes.php file against the URI to
359 * determine if the class/method need to be remapped.
360 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 * @return void
362 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200363 protected function _parse_routes()
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 // Turn the segment array into a URI string
366 $uri = implode('/', $this->uri->segments);
Barry Mienydd671972010-10-04 16:33:58 +0200367
Derek Jones37f4b9c2011-07-01 17:56:50 -0500368 // Is there a literal match? If so we're done
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 if (isset($this->routes[$uri]))
370 {
Derek Jonesc7738402010-03-02 13:55:13 -0600371 return $this->_set_request(explode('/', $this->routes[$uri]));
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 }
Barry Mienydd671972010-10-04 16:33:58 +0200373
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 // Loop through the route array looking for wild-cards
375 foreach ($this->routes as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200376 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 // Convert wild-cards to RegEx
Andrey Andreevba6c0412012-01-07 21:10:09 +0200378 $key = str_replace(array(':any', ':num'), array('.+', '[0-9]+'), $key);
Derek Jonesc7738402010-03-02 13:55:13 -0600379
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 // Does the RegEx match?
381 if (preg_match('#^'.$key.'$#', $uri))
Derek Jonesc7738402010-03-02 13:55:13 -0600382 {
Jonatas Miguel80275c72012-08-01 19:15:48 +0200383 // Are we using a callback?
384 $callable = is_callable($val);
385
386 // Determine the appropriate preg_replace to use.
387 $preg_replace_type = $callable? 'preg_replace_callback': 'preg_replace';
388
389 // Are we using the route_prepend to change how we process the matches?
390 $modifier = (is_string($val) AND strpos($val, $this->route_prepend) === 0)? 'e': '';
Jonatas Miguela101c932012-07-20 12:23:43 +0200391
Jonatas Miguel80275c72012-08-01 19:15:48 +0200392 // Are we using callbacks to process the matches?
393 if($callable){
394 $val = function($matches)use($val){
395 // Remove the string we are matching against from the matches array.
396 array_shift($matches);
397
398 // Distribute the matches to the arguments of the user's callback.
399 return call_user_func_array($val, $matches);
400 };
401 }
402 else
403 {
404 // Remove the "php:" portion of the string if it exists.
405 $val = $modifier === 'e'? substr($val, strlen($this->route_prepend)): $val;
406 }
Jonatas Miguela101c932012-07-20 12:23:43 +0200407
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 // Do we have a back-reference?
Jonatas Miguel80275c72012-08-01 19:15:48 +0200409 if ($callable OR (strpos($val, '$') !== FALSE && strpos($key, '(') !== FALSE))
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 {
Jonatas Miguel80275c72012-08-01 19:15:48 +0200411 $val = call_user_func($preg_replace_type, '#^'.$key.'$#'.$modifier, $val, $uri);
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 }
Barry Mienydd671972010-10-04 16:33:58 +0200413
414 return $this->_set_request(explode('/', $val));
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 }
416 }
417
418 // If we got this far it means we didn't encounter a
419 // matching route so we'll set the site default route
420 $this->_set_request($this->uri->segments);
421 }
422
423 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200424
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 /**
426 * Set the class name
427 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 * @param string
429 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200430 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200431 public function set_class($class)
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 {
Derek Jones2615e412010-10-06 17:51:16 -0500433 $this->class = str_replace(array('/', '.'), '', $class);
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 }
Barry Mienydd671972010-10-04 16:33:58 +0200435
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200437
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 /**
439 * Fetch the current class
440 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200442 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200443 public function fetch_class()
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 {
445 return $this->class;
446 }
Barry Mienydd671972010-10-04 16:33:58 +0200447
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200449
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 /**
Andrey Andreev7b53d042012-03-26 23:02:32 +0300451 * Set the method name
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 * @param string
454 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200455 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200456 public function set_method($method)
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 {
458 $this->method = $method;
459 }
460
461 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200462
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 /**
Andrey Andreev7b53d042012-03-26 23:02:32 +0300464 * Fetch the current method
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200467 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200468 public function fetch_method()
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 {
Alex Bilbieed944a32012-06-02 11:07:47 +0100470 return ($this->method === $this->fetch_class()) ? 'index' : $this->method;
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 }
472
473 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200474
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 /**
Andrey Andreev7b53d042012-03-26 23:02:32 +0300476 * Set the directory name
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 * @param string
479 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200480 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200481 public function set_directory($dir)
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 {
Derek Jones2615e412010-10-06 17:51:16 -0500483 $this->directory = str_replace(array('/', '.'), '', $dir).'/';
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 }
485
486 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200487
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 /**
Andrey Andreev7b53d042012-03-26 23:02:32 +0300489 * Fetch the sub-directory (if any) that contains the requested controller class
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200492 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200493 public function fetch_directory()
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 {
495 return $this->directory;
496 }
497
Derek Jonesc7738402010-03-02 13:55:13 -0600498 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200499
Derek Jonesc7738402010-03-02 13:55:13 -0600500 /**
Andrey Andreev7b53d042012-03-26 23:02:32 +0300501 * Set the controller overrides
Derek Jonesc7738402010-03-02 13:55:13 -0600502 *
Derek Jonesc7738402010-03-02 13:55:13 -0600503 * @param array
Andrey Andreev7b53d042012-03-26 23:02:32 +0300504 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200505 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200506 public function _set_overrides($routing)
Derek Jonesc7738402010-03-02 13:55:13 -0600507 {
508 if ( ! is_array($routing))
509 {
510 return;
511 }
Barry Mienydd671972010-10-04 16:33:58 +0200512
Derek Jonesc7738402010-03-02 13:55:13 -0600513 if (isset($routing['directory']))
514 {
515 $this->set_directory($routing['directory']);
516 }
Barry Mienydd671972010-10-04 16:33:58 +0200517
Andrey Andreev9ba661b2012-06-04 14:44:34 +0300518 if ( ! empty($routing['controller']))
Derek Jonesc7738402010-03-02 13:55:13 -0600519 {
520 $this->set_class($routing['controller']);
521 }
Barry Mienydd671972010-10-04 16:33:58 +0200522
Derek Jonesc7738402010-03-02 13:55:13 -0600523 if (isset($routing['function']))
524 {
Andrey Andreev9ba661b2012-06-04 14:44:34 +0300525 $routing['function'] = ($routing['function'] == '') ? 'index' : $routing['function'];
Derek Jonesc7738402010-03-02 13:55:13 -0600526 $this->set_method($routing['function']);
527 }
528 }
529
Derek Allard2067d1a2008-11-13 22:59:24 +0000530}
Derek Allard2067d1a2008-11-13 22:59:24 +0000531
532/* End of file Router.php */
Andrey Andreev7b53d042012-03-26 23:02:32 +0300533/* Location: ./system/core/Router.php */