blob: d467d60fd7fbcde3bc23051091689b0f7b511753 [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 Andreev08fec7b2013-07-19 16:25:51 +030085 * Translate URI dashes
86 *
87 * Determines whether dashes in controller & method segments
88 * should be automatically replaced by underscores.
89 *
90 * @var bool
91 */
92 public $translate_uri_dashes = FALSE;
93
94 // --------------------------------------------------------------------
95
96 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +030097 * Class constructor
Derek Allard2067d1a2008-11-13 22:59:24 +000098 *
99 * Runs the route mapping function.
Andrey Andreev92ebfb62012-05-17 12:49:24 +0300100 *
101 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000102 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200103 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 {
Derek Jonesc7738402010-03-02 13:55:13 -0600105 $this->config =& load_class('Config', 'core');
106 $this->uri =& load_class('URI', 'core');
Andrey Andreev08fec7b2013-07-19 16:25:51 +0300107 $this->_set_routing();
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 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300114 * Set route mapping
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300116 * Determines what should be served based on the URI request,
Derek Allard2067d1a2008-11-13 22:59:24 +0000117 * 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 Andreev08fec7b2013-07-19 16:25:51 +0300121 protected 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 Andreev9515dd32012-12-07 15:15:15 +0200127 if ($this->config->item('enable_query_strings') === TRUE
128 && ! empty($_GET[$this->config->item('controller_trigger')])
129 && is_string($_GET[$this->config->item('controller_trigger')])
130 )
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 {
Andrey Andreev9515dd32012-12-07 15:15:15 +0200132 if (isset($_GET[$this->config->item('directory_trigger')]) && is_string($_GET[$this->config->item('directory_trigger')]))
Derek Jonesc7738402010-03-02 13:55:13 -0600133 {
134 $this->set_directory(trim($this->uri->_filter_uri($_GET[$this->config->item('directory_trigger')])));
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300135 $segments[] = $this->directory;
Derek Jonesc7738402010-03-02 13:55:13 -0600136 }
Barry Mienydd671972010-10-04 16:33:58 +0200137
Andrey Andreev9515dd32012-12-07 15:15:15 +0200138 $this->set_class(trim($this->uri->_filter_uri($_GET[$this->config->item('controller_trigger')])));
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300139 $segments[] = $this->class;
Barry Mienydd671972010-10-04 16:33:58 +0200140
Andrey Andreev9515dd32012-12-07 15:15:15 +0200141 if ( ! empty($_GET[$this->config->item('function_trigger')]) && is_string($_GET[$this->config->item('function_trigger')]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 {
143 $this->set_method(trim($this->uri->_filter_uri($_GET[$this->config->item('function_trigger')])));
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300144 $segments[] = $this->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 Andreev06879112013-01-29 15:05:02 +0200149 if (file_exists(APPPATH.'config/routes.php'))
Greg Akerd96f8822011-12-27 16:23:47 -0600150 {
151 include(APPPATH.'config/routes.php');
152 }
David Behler07b53422011-08-15 00:25:06 +0200153
Andrey Andreev06879112013-01-29 15:05:02 +0200154 if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/routes.php'))
155 {
156 include(APPPATH.'config/'.ENVIRONMENT.'/routes.php');
157 }
158
Andrey Andreev08fec7b2013-07-19 16:25:51 +0300159 // Validate & get reserved routes
160 if (isset($route) && is_array($route))
161 {
162 isset($route['default_controller']) && $this->default_controller = $route['default_controller'];
163 isset($route['translate_uri_dashes']) && $this->translate_uri_dashes = $route['translate_uri_dashes'];
164 unset($route['default_controller'], $route['translate_uri_dashes']);
165 $this->routes = $route;
166 }
Barry Mienydd671972010-10-04 16:33:58 +0200167
Andrey Andreevba6c0412012-01-07 21:10:09 +0200168 // 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 -0600169 if (count($segments) > 0)
170 {
171 return $this->_validate_request($segments);
172 }
Barry Mienydd671972010-10-04 16:33:58 +0200173
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 // Fetch the complete URI string
175 $this->uri->_fetch_uri_string();
Barry Mienydd671972010-10-04 16:33:58 +0200176
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 // 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 +0300178 if ($this->uri->uri_string == '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 {
Derek Jonesc7738402010-03-02 13:55:13 -0600180 return $this->_set_default_controller();
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 }
Barry Mienydd671972010-10-04 16:33:58 +0200182
Andrey Andreevba6c0412012-01-07 21:10:09 +0200183 $this->uri->_remove_url_suffix(); // Remove the URL suffix
184 $this->uri->_explode_segments(); // Compile the segments into an array
185 $this->_parse_routes(); // Parse any custom routing that may exist
186 $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 +0000187 }
Derek Jonesc7738402010-03-02 13:55:13 -0600188
189 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200190
Derek Jonesc7738402010-03-02 13:55:13 -0600191 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300192 * Set default controller
Derek Jonesc7738402010-03-02 13:55:13 -0600193 *
Derek Jonesc7738402010-03-02 13:55:13 -0600194 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200195 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200196 protected function _set_default_controller()
Derek Jonesc7738402010-03-02 13:55:13 -0600197 {
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200198 if (empty($this->default_controller))
Derek Jonesc7738402010-03-02 13:55:13 -0600199 {
Andrey Andreevba6c0412012-01-07 21:10:09 +0200200 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 -0600201 }
Andrey Andreevd1097a12012-11-01 19:55:42 +0200202
Derek Jonesc7738402010-03-02 13:55:13 -0600203 // Is the method being specified?
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200204 if (sscanf($this->default_controller, '%[^/]/%s', $class, $method) !== 2)
Derek Jonesc7738402010-03-02 13:55:13 -0600205 {
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200206 $method = 'index';
Barry Mienydd671972010-10-04 16:33:58 +0200207 }
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200208
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200209 $this->_set_request(array($class, $method));
Barry Mienydd671972010-10-04 16:33:58 +0200210
Derek Jonesc7738402010-03-02 13:55:13 -0600211 // re-index the routed segments array so it starts with 1 rather than 0
212 $this->uri->_reindex_segments();
Barry Mienydd671972010-10-04 16:33:58 +0200213
Andrey Andreevba6c0412012-01-07 21:10:09 +0200214 log_message('debug', 'No URI present. Default controller set.');
Derek Jonesc7738402010-03-02 13:55:13 -0600215 }
Barry Mienydd671972010-10-04 16:33:58 +0200216
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200218
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300220 * Set request route
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300222 * Takes an array of URI segments as input and sets the class/method
223 * to be called.
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300225 * @param array $segments URI segments
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 * @return void
227 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200228 protected function _set_request($segments = array())
Barry Mienydd671972010-10-04 16:33:58 +0200229 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 $segments = $this->_validate_request($segments);
Barry Mienydd671972010-10-04 16:33:58 +0200231
Andrey Andreevba6c0412012-01-07 21:10:09 +0200232 if (count($segments) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 {
Derek Jonesc7738402010-03-02 13:55:13 -0600234 return $this->_set_default_controller();
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 }
Barry Mienydd671972010-10-04 16:33:58 +0200236
Andrey Andreev08fec7b2013-07-19 16:25:51 +0300237 if ($this->translate_uri_dashes === TRUE)
238 {
239 $segments[0] = str_replace('-', '_', $segments[0]);
240 if (isset($segments[1]))
241 {
242 $segments[1] = str_replace('-', '_', $segments[1]);
243 }
244 }
Barry Mienydd671972010-10-04 16:33:58 +0200245
Andrey Andreev08fec7b2013-07-19 16:25:51 +0300246 $this->set_class($segments[0]);
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200247 isset($segments[1]) OR $segments[1] = 'index';
248 $this->set_method($segments[1]);
Barry Mienydd671972010-10-04 16:33:58 +0200249
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 // Update our "routed" segment array to contain the segments.
251 // Note: If there is no custom routing, this array will be
Phil Sturgeon81aa94b2012-05-02 11:40:46 +0100252 // identical to $this->uri->segments
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 $this->uri->rsegments = $segments;
254 }
Barry Mienydd671972010-10-04 16:33:58 +0200255
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200257
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300259 * Validate request
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300261 * Attempts validate the URI request and determine the controller path.
262 *
263 * @param array $segments URI segments
264 * @return array URI segments
Barry Mienydd671972010-10-04 16:33:58 +0200265 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200266 protected function _validate_request($segments)
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 {
Andrey Andreevba6c0412012-01-07 21:10:09 +0200268 if (count($segments) === 0)
Derek Jonesc7738402010-03-02 13:55:13 -0600269 {
270 return $segments;
271 }
Barry Mienydd671972010-10-04 16:33:58 +0200272
Andrey Andreev20292312013-07-22 14:29:10 +0300273 $test = ucfirst($this->translate_uri_dashes === TRUE ? str_replace('-', '_', $segments[0]) : $segments[0]);
Andrey Andreevd1097a12012-11-01 19:55:42 +0200274
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 // Does the requested controller exist in the root folder?
Andrey Andreev08fec7b2013-07-19 16:25:51 +0300276 if (file_exists(APPPATH.'controllers/'.$test.'.php'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 {
278 return $segments;
279 }
Barry Mienydd671972010-10-04 16:33:58 +0200280
Derek Allard2067d1a2008-11-13 22:59:24 +0000281 // Is the controller in a sub-folder?
282 if (is_dir(APPPATH.'controllers/'.$segments[0]))
Derek Jonesc7738402010-03-02 13:55:13 -0600283 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 // Set the directory and remove it from the segment array
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200285 $this->set_directory(array_shift($segments));
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 if (count($segments) > 0)
287 {
Andrey Andreev20292312013-07-22 14:29:10 +0300288 $test = ucfirst($this->translate_uri_dashes === TRUE ? str_replace('-', '_', $segments[0]) : $segments[0]);
Andrey Andreevd1097a12012-11-01 19:55:42 +0200289
Andrey Andreev08fec7b2013-07-19 16:25:51 +0300290 // Does the requested controller exist in the sub-directory?
291 if ( ! file_exists(APPPATH.'controllers/'.$this->directory.$test.'.php'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 {
Shane Pearson664a9352011-08-10 16:02:32 -0500293 if ( ! empty($this->routes['404_override']))
294 {
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200295 $this->directory = '';
296 return explode('/', $this->routes['404_override'], 2);
Shane Pearson664a9352011-08-10 16:02:32 -0500297 }
298 else
299 {
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300300 show_404($this->directory.$segments[0]);
Shane Pearson664a9352011-08-10 16:02:32 -0500301 }
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?
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200307 $segments = explode('/', $this->default_controller);
Andrey Andreev20292312013-07-22 14:29:10 +0300308 if ( ! file_exists(APPPATH.'controllers/'.$this->directory.ucfirst($segments[0]).'.php'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 {
310 $this->directory = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 }
Barry Mienydd671972010-10-04 16:33:58 +0200313
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 return $segments;
315 }
Barry Mienydd671972010-10-04 16:33:58 +0200316
Derek Jonesc7738402010-03-02 13:55:13 -0600317 // If we've gotten this far it means that the URI does not correlate to a valid
Andrey Andreevba6c0412012-01-07 21:10:09 +0200318 // controller class. We will now see if there is an override
Eric Barnesc5bf6162011-01-30 21:17:11 -0500319 if ( ! empty($this->routes['404_override']))
Derek Jonesc7738402010-03-02 13:55:13 -0600320 {
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200321 if (sscanf($this->routes['404_override'], '%[^/]/%s', $class, $method) !== 2)
322 {
323 $method = 'index';
324 }
Barry Mienydd671972010-10-04 16:33:58 +0200325
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200326 return array($class, $method);
Derek Jonesc7738402010-03-02 13:55:13 -0600327 }
Barry Mienydd671972010-10-04 16:33:58 +0200328
Derek Jonesc7738402010-03-02 13:55:13 -0600329 // Nothing else to do at this point but show a 404
Barry Mienydd671972010-10-04 16:33:58 +0200330 show_404($segments[0]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 }
Barry Mienydd671972010-10-04 16:33:58 +0200332
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200334
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 /**
Andrey Andreev7b53d042012-03-26 23:02:32 +0300336 * Parse Routes
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300338 * Matches any routes that may exist in the config/routes.php file
339 * against the URI to determine if the class/method need to be remapped.
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 * @return void
342 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200343 protected function _parse_routes()
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100344 {
345 // Turn the segment array into a URI string
346 $uri = implode('/', $this->uri->segments);
Barry Mienydd671972010-10-04 16:33:58 +0200347
Fatih Kalifa442b50b2013-11-01 02:44:56 +0700348 // Get HTTP verb
Andrey Andreevc761a202013-11-11 14:02:15 +0200349 $http_verb = isset($_SERVER['REQUEST_METHOD']) ? strtolower($_SERVER['REQUEST_METHOD']) : 'cli';
Fatih Kalifa442b50b2013-11-01 02:44:56 +0700350
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100351 // Is there a literal match? If so we're done
Fatih Kalifa0b58b3c2013-11-05 15:36:40 +0700352 if (isset($this->routes[$uri]))
Fatih Kalifa442b50b2013-11-01 02:44:56 +0700353 {
Fatih Kalifa0b58b3c2013-11-05 15:36:40 +0700354 // Check default routes format
355 if (is_string($this->routes[$uri]))
356 {
357 return $this->_set_request(explode('/', $this->routes[$uri]));
358 }
Andrey Andreevc761a202013-11-11 14:02:15 +0200359 // Is there a matching http verb?
Fatih Kalifa0b58b3c2013-11-05 15:36:40 +0700360 elseif (is_array($this->routes[$uri]) && isset($this->routes[$uri][$http_verb]))
361 {
362 return $this->_set_request(explode('/', $this->routes[$uri][$http_verb]));
363 }
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100364 }
Barry Mienydd671972010-10-04 16:33:58 +0200365
vlakoffc941d852013-08-06 14:44:40 +0200366 // Loop through the route array looking for wildcards
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100367 foreach ($this->routes as $key => $val)
368 {
Fatih Kalifa0b58b3c2013-11-05 15:36:40 +0700369 // Check if route format is using http verb
Fatih Kalifa442b50b2013-11-01 02:44:56 +0700370 if (is_array($val))
371 {
Fatih Kalifa442b50b2013-11-01 02:44:56 +0700372 if (isset($val[$http_verb]))
373 {
374 $val = $val[$http_verb];
375 }
Fatih Kalifa442b50b2013-11-01 02:44:56 +0700376 else
377 {
Fatih Kalifa442b50b2013-11-01 02:44:56 +0700378 continue;
379 }
380 }
381
vlakoffc941d852013-08-06 14:44:40 +0200382 // Convert wildcards to RegEx
Andrey Andreev7676c2d2012-10-30 13:42:01 +0200383 $key = str_replace(array(':any', ':num'), array('[^/]+', '[0-9]+'), $key);
Derek Jonesc7738402010-03-02 13:55:13 -0600384
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100385 // Does the RegEx match?
386 if (preg_match('#^'.$key.'$#', $uri, $matches))
387 {
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100388 // Are we using callbacks to process back-references?
Andrey Andreevda5562a2012-11-08 12:34:38 +0200389 if ( ! is_string($val) && is_callable($val))
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100390 {
391 // Remove the original string from the matches array.
392 array_shift($matches);
Derek Allard2067d1a2008-11-13 22:59:24 +0000393
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100394 // Get the match count.
395 $match_count = count($matches);
Jonatas Miguelc0d98b22012-08-06 15:42:50 +0100396
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100397 // Determine how many parameters the callback has.
398 $reflection = new ReflectionFunction($val);
399 $param_count = $reflection->getNumberOfParameters();
Jonatas Miguelc0d98b22012-08-06 15:42:50 +0100400
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100401 // Are there more parameters than matches?
Jonatas Miguel24296ca2012-09-27 12:10:01 +0100402 if ($param_count > $match_count)
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100403 {
404 // Any params without matches will be set to an empty string.
405 $matches = array_merge($matches, array_fill($match_count, $param_count - $match_count, ''));
Jonatas Miguelefb81662012-10-23 19:52:46 +0100406
407 $match_count = $param_count;
408 }
409
410 // Get the parameters so we can use their default values.
411 $params = $reflection->getParameters();
412
413 for ($m = 0; $m < $match_count; $m++)
414 {
415 // Is the match empty and does a default value exist?
416 if (empty($matches[$m]) && $params[$m]->isDefaultValueAvailable())
417 {
418 // Substitute the empty match for the default value.
419 $matches[$m] = $params[$m]->getDefaultValue();
420 }
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100421 }
Jonatas Miguelc0d98b22012-08-06 15:42:50 +0100422
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100423 // Execute the callback using the values in matches as its parameters.
424 $val = call_user_func_array($val, $matches);
425 }
Andrey Andreevda5562a2012-11-08 12:34:38 +0200426 // Are we using the default routing method for back-references?
427 elseif (strpos($val, '$') !== FALSE && strpos($key, '(') !== FALSE)
428 {
429 $val = preg_replace('#^'.$key.'$#', $val, $uri);
430 }
Jonatas Miguelc0d98b22012-08-06 15:42:50 +0100431
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100432 return $this->_set_request(explode('/', $val));
433 }
434 }
Jonatas Miguelc0d98b22012-08-06 15:42:50 +0100435
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100436 // If we got this far it means we didn't encounter a
437 // matching route so we'll set the site default route
438 $this->_set_request($this->uri->segments);
439 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000440
441 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200442
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300444 * Set class name
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300446 * @param string $class Class name
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200448 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200449 public function set_class($class)
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 {
Derek Jones2615e412010-10-06 17:51:16 -0500451 $this->class = str_replace(array('/', '.'), '', $class);
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 }
Barry Mienydd671972010-10-04 16:33:58 +0200453
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200455
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 /**
457 * Fetch the current class
458 *
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300459 * @deprecated 3.0.0 Read the 'class' property instead
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200461 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200462 public function fetch_class()
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 {
464 return $this->class;
465 }
Barry Mienydd671972010-10-04 16:33:58 +0200466
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200468
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300470 * Set method name
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300472 * @param string $method Method name
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200474 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200475 public function set_method($method)
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 {
477 $this->method = $method;
478 }
479
480 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200481
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 /**
Andrey Andreev7b53d042012-03-26 23:02:32 +0300483 * Fetch the current method
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 *
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300485 * @deprecated 3.0.0 Read the 'method' property instead
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200487 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200488 public function fetch_method()
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 {
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300490 return $this->method;
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 * Set directory name
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300498 * @param string $dir Directory name
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200500 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200501 public function set_directory($dir)
Derek Allard2067d1a2008-11-13 22:59:24 +0000502 {
Derek Jones2615e412010-10-06 17:51:16 -0500503 $this->directory = str_replace(array('/', '.'), '', $dir).'/';
Derek Allard2067d1a2008-11-13 22:59:24 +0000504 }
505
506 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200507
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300509 * Fetch directory
510 *
511 * Feches the sub-directory (if any) that contains the requested
512 * controller class.
Derek Allard2067d1a2008-11-13 22:59:24 +0000513 *
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300514 * @deprecated 3.0.0 Read the 'directory' property instead
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200516 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200517 public function fetch_directory()
Derek Allard2067d1a2008-11-13 22:59:24 +0000518 {
519 return $this->directory;
520 }
521
Derek Jonesc7738402010-03-02 13:55:13 -0600522 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200523
Derek Jonesc7738402010-03-02 13:55:13 -0600524 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300525 * Set controller overrides
Derek Jonesc7738402010-03-02 13:55:13 -0600526 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300527 * @param array $routing Route overrides
Andrey Andreev7b53d042012-03-26 23:02:32 +0300528 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200529 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200530 public function _set_overrides($routing)
Derek Jonesc7738402010-03-02 13:55:13 -0600531 {
532 if ( ! is_array($routing))
533 {
534 return;
535 }
Barry Mienydd671972010-10-04 16:33:58 +0200536
Derek Jonesc7738402010-03-02 13:55:13 -0600537 if (isset($routing['directory']))
538 {
539 $this->set_directory($routing['directory']);
540 }
Barry Mienydd671972010-10-04 16:33:58 +0200541
Andrey Andreev9ba661b2012-06-04 14:44:34 +0300542 if ( ! empty($routing['controller']))
Derek Jonesc7738402010-03-02 13:55:13 -0600543 {
544 $this->set_class($routing['controller']);
545 }
Barry Mienydd671972010-10-04 16:33:58 +0200546
Derek Jonesc7738402010-03-02 13:55:13 -0600547 if (isset($routing['function']))
548 {
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200549 $routing['function'] = empty($routing['function']) ? 'index' : $routing['function'];
Derek Jonesc7738402010-03-02 13:55:13 -0600550 $this->set_method($routing['function']);
551 }
552 }
553
Derek Allard2067d1a2008-11-13 22:59:24 +0000554}
Derek Allard2067d1a2008-11-13 22:59:24 +0000555
556/* End of file Router.php */
Andrey Andreev7b53d042012-03-26 23:02:32 +0300557/* Location: ./system/core/Router.php */