blob: 89d11afabe6d93b29ebea5eac34db9e675d9883f [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
349 $http_verb = strtolower($_SERVER['REQUEST_METHOD']);
350
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100351 // Is there a literal match? If so we're done
Fatih Kalifa442b50b2013-11-01 02:44:56 +0700352 if (isset($this->routes[$uri][$http_verb]))
353 {
354 return $this->_set_request(explode('/', $this->routes[$uri][$http_verb]));
355 }
356 else if (isset($this->routes[$uri]['(:any)']))
357 {
358 return $this->_set_request(explode('/', $this->routes[$uri]['(:any)']));
359 }
360 // Fallback to default routing
361 else if (isset($this->routes[$uri]) && is_string($this->routes[$uri]))
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100362 {
363 return $this->_set_request(explode('/', $this->routes[$uri]));
364 }
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 Kalifa442b50b2013-11-01 02:44:56 +0700369 // Check if HTTP Verb is exist
370 if (is_array($val))
371 {
372 // HTTP verb included in routes
373 if (isset($val[$http_verb]))
374 {
375 $val = $val[$http_verb];
376 }
377 else if (isset($val['(:any)']))
378 {
379 $val = $val['(:any)'];
380 }
381 else
382 {
383 // HTTP Verb not found
384 continue;
385 }
386 }
387
vlakoffc941d852013-08-06 14:44:40 +0200388 // Convert wildcards to RegEx
Andrey Andreev7676c2d2012-10-30 13:42:01 +0200389 $key = str_replace(array(':any', ':num'), array('[^/]+', '[0-9]+'), $key);
Derek Jonesc7738402010-03-02 13:55:13 -0600390
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100391 // Does the RegEx match?
392 if (preg_match('#^'.$key.'$#', $uri, $matches))
393 {
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100394 // Are we using callbacks to process back-references?
Andrey Andreevda5562a2012-11-08 12:34:38 +0200395 if ( ! is_string($val) && is_callable($val))
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100396 {
397 // Remove the original string from the matches array.
398 array_shift($matches);
Derek Allard2067d1a2008-11-13 22:59:24 +0000399
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100400 // Get the match count.
401 $match_count = count($matches);
Jonatas Miguelc0d98b22012-08-06 15:42:50 +0100402
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100403 // Determine how many parameters the callback has.
404 $reflection = new ReflectionFunction($val);
405 $param_count = $reflection->getNumberOfParameters();
Jonatas Miguelc0d98b22012-08-06 15:42:50 +0100406
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100407 // Are there more parameters than matches?
Jonatas Miguel24296ca2012-09-27 12:10:01 +0100408 if ($param_count > $match_count)
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100409 {
410 // Any params without matches will be set to an empty string.
411 $matches = array_merge($matches, array_fill($match_count, $param_count - $match_count, ''));
Jonatas Miguelefb81662012-10-23 19:52:46 +0100412
413 $match_count = $param_count;
414 }
415
416 // Get the parameters so we can use their default values.
417 $params = $reflection->getParameters();
418
419 for ($m = 0; $m < $match_count; $m++)
420 {
421 // Is the match empty and does a default value exist?
422 if (empty($matches[$m]) && $params[$m]->isDefaultValueAvailable())
423 {
424 // Substitute the empty match for the default value.
425 $matches[$m] = $params[$m]->getDefaultValue();
426 }
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100427 }
Jonatas Miguelc0d98b22012-08-06 15:42:50 +0100428
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100429 // Execute the callback using the values in matches as its parameters.
430 $val = call_user_func_array($val, $matches);
431 }
Andrey Andreevda5562a2012-11-08 12:34:38 +0200432 // Are we using the default routing method for back-references?
433 elseif (strpos($val, '$') !== FALSE && strpos($key, '(') !== FALSE)
434 {
435 $val = preg_replace('#^'.$key.'$#', $val, $uri);
436 }
Jonatas Miguelc0d98b22012-08-06 15:42:50 +0100437
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100438 return $this->_set_request(explode('/', $val));
439 }
440 }
Jonatas Miguelc0d98b22012-08-06 15:42:50 +0100441
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100442 // If we got this far it means we didn't encounter a
443 // matching route so we'll set the site default route
444 $this->_set_request($this->uri->segments);
445 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000446
447 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200448
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300450 * Set class name
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300452 * @param string $class Class name
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200454 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200455 public function set_class($class)
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 {
Derek Jones2615e412010-10-06 17:51:16 -0500457 $this->class = str_replace(array('/', '.'), '', $class);
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 }
Barry Mienydd671972010-10-04 16:33:58 +0200459
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200461
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 /**
463 * Fetch the current class
464 *
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300465 * @deprecated 3.0.0 Read the 'class' property instead
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_class()
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 {
470 return $this->class;
471 }
Barry Mienydd671972010-10-04 16:33:58 +0200472
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200474
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300476 * Set method name
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300478 * @param string $method Method name
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200480 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200481 public function set_method($method)
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 {
483 $this->method = $method;
484 }
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 current method
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 *
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300491 * @deprecated 3.0.0 Read the 'method' property instead
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200493 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200494 public function fetch_method()
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 {
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300496 return $this->method;
Derek Allard2067d1a2008-11-13 22:59:24 +0000497 }
498
499 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200500
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300502 * Set directory name
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300504 * @param string $dir Directory name
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200506 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200507 public function set_directory($dir)
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 {
Derek Jones2615e412010-10-06 17:51:16 -0500509 $this->directory = str_replace(array('/', '.'), '', $dir).'/';
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 }
511
512 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200513
Derek Allard2067d1a2008-11-13 22:59:24 +0000514 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300515 * Fetch directory
516 *
517 * Feches the sub-directory (if any) that contains the requested
518 * controller class.
Derek Allard2067d1a2008-11-13 22:59:24 +0000519 *
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300520 * @deprecated 3.0.0 Read the 'directory' property instead
Derek Allard2067d1a2008-11-13 22:59:24 +0000521 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200522 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200523 public function fetch_directory()
Derek Allard2067d1a2008-11-13 22:59:24 +0000524 {
525 return $this->directory;
526 }
527
Derek Jonesc7738402010-03-02 13:55:13 -0600528 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200529
Derek Jonesc7738402010-03-02 13:55:13 -0600530 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300531 * Set controller overrides
Derek Jonesc7738402010-03-02 13:55:13 -0600532 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300533 * @param array $routing Route overrides
Andrey Andreev7b53d042012-03-26 23:02:32 +0300534 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200535 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200536 public function _set_overrides($routing)
Derek Jonesc7738402010-03-02 13:55:13 -0600537 {
538 if ( ! is_array($routing))
539 {
540 return;
541 }
Barry Mienydd671972010-10-04 16:33:58 +0200542
Derek Jonesc7738402010-03-02 13:55:13 -0600543 if (isset($routing['directory']))
544 {
545 $this->set_directory($routing['directory']);
546 }
Barry Mienydd671972010-10-04 16:33:58 +0200547
Andrey Andreev9ba661b2012-06-04 14:44:34 +0300548 if ( ! empty($routing['controller']))
Derek Jonesc7738402010-03-02 13:55:13 -0600549 {
550 $this->set_class($routing['controller']);
551 }
Barry Mienydd671972010-10-04 16:33:58 +0200552
Derek Jonesc7738402010-03-02 13:55:13 -0600553 if (isset($routing['function']))
554 {
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200555 $routing['function'] = empty($routing['function']) ? 'index' : $routing['function'];
Derek Jonesc7738402010-03-02 13:55:13 -0600556 $this->set_method($routing['function']);
557 }
558 }
559
Derek Allard2067d1a2008-11-13 22:59:24 +0000560}
Derek Allard2067d1a2008-11-13 22:59:24 +0000561
562/* End of file Router.php */
Andrey Andreev7b53d042012-03-26 23:02:32 +0300563/* Location: ./system/core/Router.php */