blob: c86ab9c20733699ba83a5b8991af0ced6d4ae267 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreevba6c0412012-01-07 21:10:09 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreevba6c0412012-01-07 21:10:09 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Andrey Andreev80500af2013-01-01 08:16:53 +020021 * @copyright Copyright (c) 2008 - 2013, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * Router Class
31 *
32 * Parses URIs and determines routing
33 *
34 * @package CodeIgniter
35 * @subpackage Libraries
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @category Libraries
Andrey Andreev92ebfb62012-05-17 12:49:24 +030037 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000038 * @link http://codeigniter.com/user_guide/general/routing.html
39 */
40class CI_Router {
41
David Behler07b53422011-08-15 00:25:06 +020042 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +030043 * CI_Config class object
David Behler07b53422011-08-15 00:25:06 +020044 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +030045 * @var object
David Behler07b53422011-08-15 00:25:06 +020046 */
Andrey Andreevba6c0412012-01-07 21:10:09 +020047 public $config;
Andrey Andreev92ebfb62012-05-17 12:49:24 +030048
David Behler07b53422011-08-15 00:25:06 +020049 /**
50 * List of routes
51 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +030052 * @var array
David Behler07b53422011-08-15 00:25:06 +020053 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040054 public $routes = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +030055
David Behler07b53422011-08-15 00:25:06 +020056 /**
David Behler07b53422011-08-15 00:25:06 +020057 * Current class name
58 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +030059 * @var string
David Behler07b53422011-08-15 00:25:06 +020060 */
Andrey Andreev92ebfb62012-05-17 12:49:24 +030061 public $class = '';
62
David Behler07b53422011-08-15 00:25:06 +020063 /**
64 * Current method name
65 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +030066 * @var string
David Behler07b53422011-08-15 00:25:06 +020067 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040068 public $method = 'index';
Andrey Andreev92ebfb62012-05-17 12:49:24 +030069
David Behler07b53422011-08-15 00:25:06 +020070 /**
71 * Sub-directory that contains the requested controller class
72 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +030073 * @var string
David Behler07b53422011-08-15 00:25:06 +020074 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040075 public $directory = '';
Andrey Andreev92ebfb62012-05-17 12:49:24 +030076
David Behler07b53422011-08-15 00:25:06 +020077 /**
78 * Default controller (and method if specific)
79 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +030080 * @var string
David Behler07b53422011-08-15 00:25:06 +020081 */
Andrey Andreevba6c0412012-01-07 21:10:09 +020082 public $default_controller;
Barry Mienydd671972010-10-04 16:33:58 +020083
Derek Allard2067d1a2008-11-13 22:59:24 +000084 /**
Andrey 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 Andreev9515dd32012-12-07 15:15:15 +0200114 if ($this->config->item('enable_query_strings') === TRUE
115 && ! empty($_GET[$this->config->item('controller_trigger')])
116 && is_string($_GET[$this->config->item('controller_trigger')])
117 )
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 {
Andrey Andreev9515dd32012-12-07 15:15:15 +0200119 if (isset($_GET[$this->config->item('directory_trigger')]) && is_string($_GET[$this->config->item('directory_trigger')]))
Derek Jonesc7738402010-03-02 13:55:13 -0600120 {
121 $this->set_directory(trim($this->uri->_filter_uri($_GET[$this->config->item('directory_trigger')])));
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300122 $segments[] = $this->directory;
Derek Jonesc7738402010-03-02 13:55:13 -0600123 }
Barry Mienydd671972010-10-04 16:33:58 +0200124
Andrey Andreev9515dd32012-12-07 15:15:15 +0200125 $this->set_class(trim($this->uri->_filter_uri($_GET[$this->config->item('controller_trigger')])));
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300126 $segments[] = $this->class;
Barry Mienydd671972010-10-04 16:33:58 +0200127
Andrey Andreev9515dd32012-12-07 15:15:15 +0200128 if ( ! empty($_GET[$this->config->item('function_trigger')]) && is_string($_GET[$this->config->item('function_trigger')]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 {
130 $this->set_method(trim($this->uri->_filter_uri($_GET[$this->config->item('function_trigger')])));
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300131 $segments[] = $this->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 Andreev06879112013-01-29 15:05:02 +0200136 if (file_exists(APPPATH.'config/routes.php'))
Greg Akerd96f8822011-12-27 16:23:47 -0600137 {
138 include(APPPATH.'config/routes.php');
139 }
David Behler07b53422011-08-15 00:25:06 +0200140
Andrey Andreev06879112013-01-29 15:05:02 +0200141 if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/routes.php'))
142 {
143 include(APPPATH.'config/'.ENVIRONMENT.'/routes.php');
144 }
145
Andrey Andreev9515dd32012-12-07 15:15:15 +0200146 $this->routes = (empty($route) OR ! is_array($route)) ? array() : $route;
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 unset($route);
Barry Mienydd671972010-10-04 16:33:58 +0200148
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 // Set the default controller so we can display it in the event
150 // the URI doesn't correlated to a valid controller.
Andrey Andreeva3f5c5c2012-11-04 20:08:42 +0200151 $this->default_controller = empty($this->routes['default_controller']) ? FALSE : $this->routes['default_controller'];
Barry Mienydd671972010-10-04 16:33:58 +0200152
Andrey Andreevba6c0412012-01-07 21:10:09 +0200153 // 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 -0600154 if (count($segments) > 0)
155 {
156 return $this->_validate_request($segments);
157 }
Barry Mienydd671972010-10-04 16:33:58 +0200158
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 // Fetch the complete URI string
160 $this->uri->_fetch_uri_string();
Barry Mienydd671972010-10-04 16:33:58 +0200161
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 // 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 +0300163 if ($this->uri->uri_string == '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 {
Derek Jonesc7738402010-03-02 13:55:13 -0600165 return $this->_set_default_controller();
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 }
Barry Mienydd671972010-10-04 16:33:58 +0200167
Andrey Andreevba6c0412012-01-07 21:10:09 +0200168 $this->uri->_remove_url_suffix(); // Remove the URL suffix
169 $this->uri->_explode_segments(); // Compile the segments into an array
170 $this->_parse_routes(); // Parse any custom routing that may exist
171 $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 +0000172 }
Derek Jonesc7738402010-03-02 13:55:13 -0600173
174 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200175
Derek Jonesc7738402010-03-02 13:55:13 -0600176 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300177 * Set default controller
Derek Jonesc7738402010-03-02 13:55:13 -0600178 *
Derek Jonesc7738402010-03-02 13:55:13 -0600179 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200180 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200181 protected function _set_default_controller()
Derek Jonesc7738402010-03-02 13:55:13 -0600182 {
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200183 if (empty($this->default_controller))
Derek Jonesc7738402010-03-02 13:55:13 -0600184 {
Andrey Andreevba6c0412012-01-07 21:10:09 +0200185 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 -0600186 }
Andrey Andreevd1097a12012-11-01 19:55:42 +0200187
Derek Jonesc7738402010-03-02 13:55:13 -0600188 // Is the method being specified?
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200189 if (sscanf($this->default_controller, '%[^/]/%s', $class, $method) !== 2)
Derek Jonesc7738402010-03-02 13:55:13 -0600190 {
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200191 $method = 'index';
Barry Mienydd671972010-10-04 16:33:58 +0200192 }
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200193
194 $this->set_class($class);
195 $this->set_method($method);
196 $this->_set_request(array($class, $method));
Barry Mienydd671972010-10-04 16:33:58 +0200197
Derek Jonesc7738402010-03-02 13:55:13 -0600198 // re-index the routed segments array so it starts with 1 rather than 0
199 $this->uri->_reindex_segments();
Barry Mienydd671972010-10-04 16:33:58 +0200200
Andrey Andreevba6c0412012-01-07 21:10:09 +0200201 log_message('debug', 'No URI present. Default controller set.');
Derek Jonesc7738402010-03-02 13:55:13 -0600202 }
Barry Mienydd671972010-10-04 16:33:58 +0200203
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200205
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300207 * Set request route
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300209 * Takes an array of URI segments as input and sets the class/method
210 * to be called.
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300212 * @param array $segments URI segments
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 * @return void
214 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200215 protected function _set_request($segments = array())
Barry Mienydd671972010-10-04 16:33:58 +0200216 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 $segments = $this->_validate_request($segments);
Barry Mienydd671972010-10-04 16:33:58 +0200218
Andrey Andreevba6c0412012-01-07 21:10:09 +0200219 if (count($segments) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 {
Derek Jonesc7738402010-03-02 13:55:13 -0600221 return $this->_set_default_controller();
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 }
Barry Mienydd671972010-10-04 16:33:58 +0200223
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 $this->set_class($segments[0]);
Barry Mienydd671972010-10-04 16:33:58 +0200225
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200226 isset($segments[1]) OR $segments[1] = 'index';
227 $this->set_method($segments[1]);
Barry Mienydd671972010-10-04 16:33:58 +0200228
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 // Update our "routed" segment array to contain the segments.
230 // Note: If there is no custom routing, this array will be
Phil Sturgeon81aa94b2012-05-02 11:40:46 +0100231 // identical to $this->uri->segments
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 $this->uri->rsegments = $segments;
233 }
Barry Mienydd671972010-10-04 16:33:58 +0200234
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200236
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300238 * Validate request
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300240 * Attempts validate the URI request and determine the controller path.
241 *
242 * @param array $segments URI segments
243 * @return array URI segments
Barry Mienydd671972010-10-04 16:33:58 +0200244 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200245 protected function _validate_request($segments)
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 {
Andrey Andreevba6c0412012-01-07 21:10:09 +0200247 if (count($segments) === 0)
Derek Jonesc7738402010-03-02 13:55:13 -0600248 {
249 return $segments;
250 }
Barry Mienydd671972010-10-04 16:33:58 +0200251
Andrey Andreevd1097a12012-11-01 19:55:42 +0200252 $temp = str_replace('-', '_', $segments[0]);
253
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 // Does the requested controller exist in the root folder?
Andrey Andreevd1097a12012-11-01 19:55:42 +0200255 if (file_exists(APPPATH.'controllers/'.$temp.'.php'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 {
Andrey Andreevd1097a12012-11-01 19:55:42 +0200257 $segments[0] = $temp;
258 empty($segments[1]) OR $segments[1] = str_replace('-', '_', $segments[1]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 return $segments;
260 }
Barry Mienydd671972010-10-04 16:33:58 +0200261
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 // Is the controller in a sub-folder?
263 if (is_dir(APPPATH.'controllers/'.$segments[0]))
Derek Jonesc7738402010-03-02 13:55:13 -0600264 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 // Set the directory and remove it from the segment array
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200266 $this->set_directory(array_shift($segments));
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 if (count($segments) > 0)
268 {
Andrey Andreevd1097a12012-11-01 19:55:42 +0200269 $segments[0] = str_replace('-', '_', $segments[0]);
270 empty($segments[1]) OR $segments[1] = str_replace('-', '_', $segments[1]);
271
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 // Does the requested controller exist in the sub-folder?
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300273 if ( ! file_exists(APPPATH.'controllers/'.$this->directory.$segments[0].'.php'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 {
Shane Pearson664a9352011-08-10 16:02:32 -0500275 if ( ! empty($this->routes['404_override']))
276 {
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200277 $this->directory = '';
278 return explode('/', $this->routes['404_override'], 2);
Shane Pearson664a9352011-08-10 16:02:32 -0500279 }
280 else
281 {
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300282 show_404($this->directory.$segments[0]);
Shane Pearson664a9352011-08-10 16:02:32 -0500283 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 }
285 }
286 else
287 {
Derek Jonesc7738402010-03-02 13:55:13 -0600288 // Is the method being specified in the route?
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200289 $segments = explode('/', $this->default_controller);
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300290 if ( ! file_exists(APPPATH.'controllers/'.$this->directory.$segments[0].'.php'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 {
292 $this->directory = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 }
Barry Mienydd671972010-10-04 16:33:58 +0200295
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 return $segments;
297 }
Barry Mienydd671972010-10-04 16:33:58 +0200298
Derek Jonesc7738402010-03-02 13:55:13 -0600299 // If we've gotten this far it means that the URI does not correlate to a valid
Andrey Andreevba6c0412012-01-07 21:10:09 +0200300 // controller class. We will now see if there is an override
Eric Barnesc5bf6162011-01-30 21:17:11 -0500301 if ( ! empty($this->routes['404_override']))
Derek Jonesc7738402010-03-02 13:55:13 -0600302 {
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200303 if (sscanf($this->routes['404_override'], '%[^/]/%s', $class, $method) !== 2)
304 {
305 $method = 'index';
306 }
Barry Mienydd671972010-10-04 16:33:58 +0200307
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200308 return array($class, $method);
Derek Jonesc7738402010-03-02 13:55:13 -0600309 }
Barry Mienydd671972010-10-04 16:33:58 +0200310
Derek Jonesc7738402010-03-02 13:55:13 -0600311 // Nothing else to do at this point but show a 404
Barry Mienydd671972010-10-04 16:33:58 +0200312 show_404($segments[0]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 }
Barry Mienydd671972010-10-04 16:33:58 +0200314
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200316
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 /**
Andrey Andreev7b53d042012-03-26 23:02:32 +0300318 * Parse Routes
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300320 * Matches any routes that may exist in the config/routes.php file
321 * against the URI to determine if the class/method need to be remapped.
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 * @return void
324 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200325 protected function _parse_routes()
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100326 {
327 // Turn the segment array into a URI string
328 $uri = implode('/', $this->uri->segments);
Barry Mienydd671972010-10-04 16:33:58 +0200329
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100330 // Is there a literal match? If so we're done
Andrey Andreeve2b07542012-11-08 12:37:40 +0200331 if (isset($this->routes[$uri]) && is_string($this->routes[$uri]))
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100332 {
333 return $this->_set_request(explode('/', $this->routes[$uri]));
334 }
Barry Mienydd671972010-10-04 16:33:58 +0200335
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100336 // Loop through the route array looking for wild-cards
337 foreach ($this->routes as $key => $val)
338 {
339 // Convert wild-cards to RegEx
Andrey Andreev7676c2d2012-10-30 13:42:01 +0200340 $key = str_replace(array(':any', ':num'), array('[^/]+', '[0-9]+'), $key);
Derek Jonesc7738402010-03-02 13:55:13 -0600341
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100342 // Does the RegEx match?
343 if (preg_match('#^'.$key.'$#', $uri, $matches))
344 {
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100345 // Are we using callbacks to process back-references?
Andrey Andreevda5562a2012-11-08 12:34:38 +0200346 if ( ! is_string($val) && is_callable($val))
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100347 {
348 // Remove the original string from the matches array.
349 array_shift($matches);
Derek Allard2067d1a2008-11-13 22:59:24 +0000350
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100351 // Get the match count.
352 $match_count = count($matches);
Jonatas Miguelc0d98b22012-08-06 15:42:50 +0100353
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100354 // Determine how many parameters the callback has.
355 $reflection = new ReflectionFunction($val);
356 $param_count = $reflection->getNumberOfParameters();
Jonatas Miguelc0d98b22012-08-06 15:42:50 +0100357
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100358 // Are there more parameters than matches?
Jonatas Miguel24296ca2012-09-27 12:10:01 +0100359 if ($param_count > $match_count)
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100360 {
361 // Any params without matches will be set to an empty string.
362 $matches = array_merge($matches, array_fill($match_count, $param_count - $match_count, ''));
Jonatas Miguelefb81662012-10-23 19:52:46 +0100363
364 $match_count = $param_count;
365 }
366
367 // Get the parameters so we can use their default values.
368 $params = $reflection->getParameters();
369
370 for ($m = 0; $m < $match_count; $m++)
371 {
372 // Is the match empty and does a default value exist?
373 if (empty($matches[$m]) && $params[$m]->isDefaultValueAvailable())
374 {
375 // Substitute the empty match for the default value.
376 $matches[$m] = $params[$m]->getDefaultValue();
377 }
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100378 }
Jonatas Miguelc0d98b22012-08-06 15:42:50 +0100379
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100380 // Execute the callback using the values in matches as its parameters.
381 $val = call_user_func_array($val, $matches);
382 }
Andrey Andreevda5562a2012-11-08 12:34:38 +0200383 // Are we using the default routing method for back-references?
384 elseif (strpos($val, '$') !== FALSE && strpos($key, '(') !== FALSE)
385 {
386 $val = preg_replace('#^'.$key.'$#', $val, $uri);
387 }
Jonatas Miguelc0d98b22012-08-06 15:42:50 +0100388
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100389 return $this->_set_request(explode('/', $val));
390 }
391 }
Jonatas Miguelc0d98b22012-08-06 15:42:50 +0100392
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100393 // If we got this far it means we didn't encounter a
394 // matching route so we'll set the site default route
395 $this->_set_request($this->uri->segments);
396 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000397
398 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200399
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300401 * Set class name
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300403 * @param string $class Class name
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200405 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200406 public function set_class($class)
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 {
Derek Jones2615e412010-10-06 17:51:16 -0500408 $this->class = str_replace(array('/', '.'), '', $class);
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 }
Barry Mienydd671972010-10-04 16:33:58 +0200410
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200412
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 /**
414 * Fetch the current class
415 *
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300416 * @deprecated 3.0.0 Read the 'class' property instead
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200418 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200419 public function fetch_class()
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 {
421 return $this->class;
422 }
Barry Mienydd671972010-10-04 16:33:58 +0200423
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200425
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300427 * Set method name
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300429 * @param string $method Method name
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200431 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200432 public function set_method($method)
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 {
434 $this->method = $method;
435 }
436
437 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200438
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 /**
Andrey Andreev7b53d042012-03-26 23:02:32 +0300440 * Fetch the current method
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 *
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300442 * @deprecated 3.0.0 Read the 'method' property instead
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200444 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200445 public function fetch_method()
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 {
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300447 return $this->method;
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 }
449
450 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200451
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300453 * Set directory name
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300455 * @param string $dir Directory name
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200457 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200458 public function set_directory($dir)
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 {
Derek Jones2615e412010-10-06 17:51:16 -0500460 $this->directory = str_replace(array('/', '.'), '', $dir).'/';
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 }
462
463 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200464
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300466 * Fetch directory
467 *
468 * Feches the sub-directory (if any) that contains the requested
469 * controller class.
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 *
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300471 * @deprecated 3.0.0 Read the 'directory' property instead
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200473 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200474 public function fetch_directory()
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 {
476 return $this->directory;
477 }
478
Derek Jonesc7738402010-03-02 13:55:13 -0600479 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200480
Derek Jonesc7738402010-03-02 13:55:13 -0600481 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300482 * Set controller overrides
Derek Jonesc7738402010-03-02 13:55:13 -0600483 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300484 * @param array $routing Route overrides
Andrey Andreev7b53d042012-03-26 23:02:32 +0300485 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200486 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200487 public function _set_overrides($routing)
Derek Jonesc7738402010-03-02 13:55:13 -0600488 {
489 if ( ! is_array($routing))
490 {
491 return;
492 }
Barry Mienydd671972010-10-04 16:33:58 +0200493
Derek Jonesc7738402010-03-02 13:55:13 -0600494 if (isset($routing['directory']))
495 {
496 $this->set_directory($routing['directory']);
497 }
Barry Mienydd671972010-10-04 16:33:58 +0200498
Andrey Andreev9ba661b2012-06-04 14:44:34 +0300499 if ( ! empty($routing['controller']))
Derek Jonesc7738402010-03-02 13:55:13 -0600500 {
501 $this->set_class($routing['controller']);
502 }
Barry Mienydd671972010-10-04 16:33:58 +0200503
Derek Jonesc7738402010-03-02 13:55:13 -0600504 if (isset($routing['function']))
505 {
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200506 $routing['function'] = empty($routing['function']) ? 'index' : $routing['function'];
Derek Jonesc7738402010-03-02 13:55:13 -0600507 $this->set_method($routing['function']);
508 }
509 }
510
Derek Allard2067d1a2008-11-13 22:59:24 +0000511}
Derek Allard2067d1a2008-11-13 22:59:24 +0000512
513/* End of file Router.php */
Andrey Andreev7b53d042012-03-26 23:02:32 +0300514/* Location: ./system/core/Router.php */