blob: cb44a3ce9719066172492c01f88843590c0c908a [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 {
Andrey Andreev4e6c5282014-01-10 19:29:49 +0200105 global $routing;
106
Derek Jonesc7738402010-03-02 13:55:13 -0600107 $this->config =& load_class('Config', 'core');
108 $this->uri =& load_class('URI', 'core');
Andrey Andreev08fec7b2013-07-19 16:25:51 +0300109 $this->_set_routing();
Andrey Andreev4e6c5282014-01-10 19:29:49 +0200110
111 // Set any routing overrides that may exist in the main index file
112 if (isset($routing) && is_array($routing))
113 {
114 if (isset($routing['directory']))
115 {
116 $this->set_directory($routing['directory']);
117 }
118
119 if ( ! empty($routing['controller']))
120 {
121 $this->set_class($routing['controller']);
122 }
123
124 if (isset($routing['function']))
125 {
126 $routing['function'] = empty($routing['function']) ? 'index' : $routing['function'];
127 $this->set_method($routing['function']);
128 }
129 }
130
Andrey Andreevba6c0412012-01-07 21:10:09 +0200131 log_message('debug', 'Router Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 }
Barry Mienydd671972010-10-04 16:33:58 +0200133
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200135
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300137 * Set route mapping
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300139 * Determines what should be served based on the URI request,
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 * as well as any "routes" that have been set in the routing config file.
141 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 * @return void
143 */
Andrey Andreev08fec7b2013-07-19 16:25:51 +0300144 protected function _set_routing()
Barry Mienydd671972010-10-04 16:33:58 +0200145 {
Andrey Andreevba6c0412012-01-07 21:10:09 +0200146 // Are query strings enabled in the config file? Normally CI doesn't utilize query strings
Barry Mienydd671972010-10-04 16:33:58 +0200147 // since URI segments are more search-engine friendly, but they can optionally be used.
Derek Jonesc7738402010-03-02 13:55:13 -0600148 // If this feature is enabled, we will gather the directory/class/method a little differently
149 $segments = array();
Andrey Andreev9515dd32012-12-07 15:15:15 +0200150 if ($this->config->item('enable_query_strings') === TRUE
151 && ! empty($_GET[$this->config->item('controller_trigger')])
152 && is_string($_GET[$this->config->item('controller_trigger')])
153 )
Derek Allard2067d1a2008-11-13 22:59:24 +0000154 {
Andrey Andreev9515dd32012-12-07 15:15:15 +0200155 if (isset($_GET[$this->config->item('directory_trigger')]) && is_string($_GET[$this->config->item('directory_trigger')]))
Derek Jonesc7738402010-03-02 13:55:13 -0600156 {
157 $this->set_directory(trim($this->uri->_filter_uri($_GET[$this->config->item('directory_trigger')])));
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300158 $segments[] = $this->directory;
Derek Jonesc7738402010-03-02 13:55:13 -0600159 }
Barry Mienydd671972010-10-04 16:33:58 +0200160
Andrey Andreev9515dd32012-12-07 15:15:15 +0200161 $this->set_class(trim($this->uri->_filter_uri($_GET[$this->config->item('controller_trigger')])));
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300162 $segments[] = $this->class;
Barry Mienydd671972010-10-04 16:33:58 +0200163
Andrey Andreev9515dd32012-12-07 15:15:15 +0200164 if ( ! empty($_GET[$this->config->item('function_trigger')]) && is_string($_GET[$this->config->item('function_trigger')]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 {
166 $this->set_method(trim($this->uri->_filter_uri($_GET[$this->config->item('function_trigger')])));
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300167 $segments[] = $this->method;
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 }
Barry Mienydd671972010-10-04 16:33:58 +0200170
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 // Load the routes.php file.
Andrey Andreev06879112013-01-29 15:05:02 +0200172 if (file_exists(APPPATH.'config/routes.php'))
Greg Akerd96f8822011-12-27 16:23:47 -0600173 {
174 include(APPPATH.'config/routes.php');
175 }
David Behler07b53422011-08-15 00:25:06 +0200176
Andrey Andreev06879112013-01-29 15:05:02 +0200177 if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/routes.php'))
178 {
179 include(APPPATH.'config/'.ENVIRONMENT.'/routes.php');
180 }
181
Andrey Andreev08fec7b2013-07-19 16:25:51 +0300182 // Validate & get reserved routes
183 if (isset($route) && is_array($route))
184 {
185 isset($route['default_controller']) && $this->default_controller = $route['default_controller'];
186 isset($route['translate_uri_dashes']) && $this->translate_uri_dashes = $route['translate_uri_dashes'];
187 unset($route['default_controller'], $route['translate_uri_dashes']);
188 $this->routes = $route;
189 }
Barry Mienydd671972010-10-04 16:33:58 +0200190
Andrey Andreevba6c0412012-01-07 21:10:09 +0200191 // 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 -0600192 if (count($segments) > 0)
193 {
194 return $this->_validate_request($segments);
195 }
Barry Mienydd671972010-10-04 16:33:58 +0200196
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 // Fetch the complete URI string
198 $this->uri->_fetch_uri_string();
Barry Mienydd671972010-10-04 16:33:58 +0200199
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 // 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 +0300201 if ($this->uri->uri_string == '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 {
Derek Jonesc7738402010-03-02 13:55:13 -0600203 return $this->_set_default_controller();
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 }
Barry Mienydd671972010-10-04 16:33:58 +0200205
Andrey Andreevba6c0412012-01-07 21:10:09 +0200206 $this->uri->_remove_url_suffix(); // Remove the URL suffix
207 $this->uri->_explode_segments(); // Compile the segments into an array
208 $this->_parse_routes(); // Parse any custom routing that may exist
209 $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 +0000210 }
Derek Jonesc7738402010-03-02 13:55:13 -0600211
212 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200213
Derek Jonesc7738402010-03-02 13:55:13 -0600214 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300215 * Set default controller
Derek Jonesc7738402010-03-02 13:55:13 -0600216 *
Derek Jonesc7738402010-03-02 13:55:13 -0600217 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200218 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200219 protected function _set_default_controller()
Derek Jonesc7738402010-03-02 13:55:13 -0600220 {
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200221 if (empty($this->default_controller))
Derek Jonesc7738402010-03-02 13:55:13 -0600222 {
Andrey Andreevba6c0412012-01-07 21:10:09 +0200223 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 -0600224 }
Andrey Andreevd1097a12012-11-01 19:55:42 +0200225
Derek Jonesc7738402010-03-02 13:55:13 -0600226 // Is the method being specified?
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200227 if (sscanf($this->default_controller, '%[^/]/%s', $class, $method) !== 2)
Derek Jonesc7738402010-03-02 13:55:13 -0600228 {
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200229 $method = 'index';
Barry Mienydd671972010-10-04 16:33:58 +0200230 }
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200231
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200232 $this->_set_request(array($class, $method));
Barry Mienydd671972010-10-04 16:33:58 +0200233
Derek Jonesc7738402010-03-02 13:55:13 -0600234 // re-index the routed segments array so it starts with 1 rather than 0
235 $this->uri->_reindex_segments();
Barry Mienydd671972010-10-04 16:33:58 +0200236
Andrey Andreevba6c0412012-01-07 21:10:09 +0200237 log_message('debug', 'No URI present. Default controller set.');
Derek Jonesc7738402010-03-02 13:55:13 -0600238 }
Barry Mienydd671972010-10-04 16:33:58 +0200239
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200241
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300243 * Set request route
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300245 * Takes an array of URI segments as input and sets the class/method
246 * to be called.
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300248 * @param array $segments URI segments
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 * @return void
250 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200251 protected function _set_request($segments = array())
Barry Mienydd671972010-10-04 16:33:58 +0200252 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 $segments = $this->_validate_request($segments);
Barry Mienydd671972010-10-04 16:33:58 +0200254
Andrey Andreevba6c0412012-01-07 21:10:09 +0200255 if (count($segments) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 {
Derek Jonesc7738402010-03-02 13:55:13 -0600257 return $this->_set_default_controller();
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 }
Barry Mienydd671972010-10-04 16:33:58 +0200259
Andrey Andreev08fec7b2013-07-19 16:25:51 +0300260 if ($this->translate_uri_dashes === TRUE)
261 {
262 $segments[0] = str_replace('-', '_', $segments[0]);
263 if (isset($segments[1]))
264 {
265 $segments[1] = str_replace('-', '_', $segments[1]);
266 }
267 }
Barry Mienydd671972010-10-04 16:33:58 +0200268
Andrey Andreev08fec7b2013-07-19 16:25:51 +0300269 $this->set_class($segments[0]);
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200270 isset($segments[1]) OR $segments[1] = 'index';
271 $this->set_method($segments[1]);
Barry Mienydd671972010-10-04 16:33:58 +0200272
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 // Update our "routed" segment array to contain the segments.
274 // Note: If there is no custom routing, this array will be
Phil Sturgeon81aa94b2012-05-02 11:40:46 +0100275 // identical to $this->uri->segments
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 $this->uri->rsegments = $segments;
277 }
Barry Mienydd671972010-10-04 16:33:58 +0200278
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200280
Derek Allard2067d1a2008-11-13 22:59:24 +0000281 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300282 * Validate request
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300284 * Attempts validate the URI request and determine the controller path.
285 *
286 * @param array $segments URI segments
287 * @return array URI segments
Barry Mienydd671972010-10-04 16:33:58 +0200288 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200289 protected function _validate_request($segments)
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 {
Andrey Andreevba6c0412012-01-07 21:10:09 +0200291 if (count($segments) === 0)
Derek Jonesc7738402010-03-02 13:55:13 -0600292 {
293 return $segments;
294 }
Barry Mienydd671972010-10-04 16:33:58 +0200295
Andrey Andreev20292312013-07-22 14:29:10 +0300296 $test = ucfirst($this->translate_uri_dashes === TRUE ? str_replace('-', '_', $segments[0]) : $segments[0]);
Andrey Andreevd1097a12012-11-01 19:55:42 +0200297
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 // Does the requested controller exist in the root folder?
Andrey Andreev08fec7b2013-07-19 16:25:51 +0300299 if (file_exists(APPPATH.'controllers/'.$test.'.php'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 {
301 return $segments;
302 }
Barry Mienydd671972010-10-04 16:33:58 +0200303
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 // Is the controller in a sub-folder?
305 if (is_dir(APPPATH.'controllers/'.$segments[0]))
Derek Jonesc7738402010-03-02 13:55:13 -0600306 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 // Set the directory and remove it from the segment array
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200308 $this->set_directory(array_shift($segments));
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 if (count($segments) > 0)
310 {
Andrey Andreev20292312013-07-22 14:29:10 +0300311 $test = ucfirst($this->translate_uri_dashes === TRUE ? str_replace('-', '_', $segments[0]) : $segments[0]);
Andrey Andreevd1097a12012-11-01 19:55:42 +0200312
Andrey Andreev08fec7b2013-07-19 16:25:51 +0300313 // Does the requested controller exist in the sub-directory?
314 if ( ! file_exists(APPPATH.'controllers/'.$this->directory.$test.'.php'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 {
Shane Pearson664a9352011-08-10 16:02:32 -0500316 if ( ! empty($this->routes['404_override']))
317 {
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200318 $this->directory = '';
319 return explode('/', $this->routes['404_override'], 2);
Shane Pearson664a9352011-08-10 16:02:32 -0500320 }
321 else
322 {
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300323 show_404($this->directory.$segments[0]);
Shane Pearson664a9352011-08-10 16:02:32 -0500324 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 }
326 }
327 else
328 {
Derek Jonesc7738402010-03-02 13:55:13 -0600329 // Is the method being specified in the route?
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200330 $segments = explode('/', $this->default_controller);
Andrey Andreev20292312013-07-22 14:29:10 +0300331 if ( ! file_exists(APPPATH.'controllers/'.$this->directory.ucfirst($segments[0]).'.php'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 {
333 $this->directory = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 }
Barry Mienydd671972010-10-04 16:33:58 +0200336
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 return $segments;
338 }
Barry Mienydd671972010-10-04 16:33:58 +0200339
Derek Jonesc7738402010-03-02 13:55:13 -0600340 // If we've gotten this far it means that the URI does not correlate to a valid
Andrey Andreevba6c0412012-01-07 21:10:09 +0200341 // controller class. We will now see if there is an override
Eric Barnesc5bf6162011-01-30 21:17:11 -0500342 if ( ! empty($this->routes['404_override']))
Derek Jonesc7738402010-03-02 13:55:13 -0600343 {
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200344 if (sscanf($this->routes['404_override'], '%[^/]/%s', $class, $method) !== 2)
345 {
346 $method = 'index';
347 }
Barry Mienydd671972010-10-04 16:33:58 +0200348
Andrey Andreev533bf2d2012-11-02 22:44:29 +0200349 return array($class, $method);
Derek Jonesc7738402010-03-02 13:55:13 -0600350 }
Barry Mienydd671972010-10-04 16:33:58 +0200351
Derek Jonesc7738402010-03-02 13:55:13 -0600352 // Nothing else to do at this point but show a 404
Barry Mienydd671972010-10-04 16:33:58 +0200353 show_404($segments[0]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 }
Barry Mienydd671972010-10-04 16:33:58 +0200355
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200357
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 /**
Andrey Andreev7b53d042012-03-26 23:02:32 +0300359 * Parse Routes
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300361 * Matches any routes that may exist in the config/routes.php file
362 * against the URI to determine if the class/method need to be remapped.
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 * @return void
365 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200366 protected function _parse_routes()
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100367 {
368 // Turn the segment array into a URI string
369 $uri = implode('/', $this->uri->segments);
Barry Mienydd671972010-10-04 16:33:58 +0200370
Fatih Kalifa442b50b2013-11-01 02:44:56 +0700371 // Get HTTP verb
Andrey Andreevc761a202013-11-11 14:02:15 +0200372 $http_verb = isset($_SERVER['REQUEST_METHOD']) ? strtolower($_SERVER['REQUEST_METHOD']) : 'cli';
Fatih Kalifa442b50b2013-11-01 02:44:56 +0700373
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100374 // Is there a literal match? If so we're done
Fatih Kalifa0b58b3c2013-11-05 15:36:40 +0700375 if (isset($this->routes[$uri]))
Fatih Kalifa442b50b2013-11-01 02:44:56 +0700376 {
Fatih Kalifa0b58b3c2013-11-05 15:36:40 +0700377 // Check default routes format
378 if (is_string($this->routes[$uri]))
379 {
380 return $this->_set_request(explode('/', $this->routes[$uri]));
381 }
Andrey Andreevc761a202013-11-11 14:02:15 +0200382 // Is there a matching http verb?
Fatih Kalifa0b58b3c2013-11-05 15:36:40 +0700383 elseif (is_array($this->routes[$uri]) && isset($this->routes[$uri][$http_verb]))
384 {
385 return $this->_set_request(explode('/', $this->routes[$uri][$http_verb]));
386 }
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100387 }
Barry Mienydd671972010-10-04 16:33:58 +0200388
vlakoffc941d852013-08-06 14:44:40 +0200389 // Loop through the route array looking for wildcards
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100390 foreach ($this->routes as $key => $val)
391 {
Fatih Kalifa0b58b3c2013-11-05 15:36:40 +0700392 // Check if route format is using http verb
Fatih Kalifa442b50b2013-11-01 02:44:56 +0700393 if (is_array($val))
394 {
Fatih Kalifa442b50b2013-11-01 02:44:56 +0700395 if (isset($val[$http_verb]))
396 {
397 $val = $val[$http_verb];
398 }
Fatih Kalifa442b50b2013-11-01 02:44:56 +0700399 else
400 {
Fatih Kalifa442b50b2013-11-01 02:44:56 +0700401 continue;
402 }
403 }
404
vlakoffc941d852013-08-06 14:44:40 +0200405 // Convert wildcards to RegEx
Andrey Andreev7676c2d2012-10-30 13:42:01 +0200406 $key = str_replace(array(':any', ':num'), array('[^/]+', '[0-9]+'), $key);
Derek Jonesc7738402010-03-02 13:55:13 -0600407
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100408 // Does the RegEx match?
409 if (preg_match('#^'.$key.'$#', $uri, $matches))
410 {
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100411 // Are we using callbacks to process back-references?
Andrey Andreevda5562a2012-11-08 12:34:38 +0200412 if ( ! is_string($val) && is_callable($val))
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100413 {
414 // Remove the original string from the matches array.
415 array_shift($matches);
Derek Allard2067d1a2008-11-13 22:59:24 +0000416
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100417 // Get the match count.
418 $match_count = count($matches);
Jonatas Miguelc0d98b22012-08-06 15:42:50 +0100419
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100420 // Determine how many parameters the callback has.
421 $reflection = new ReflectionFunction($val);
422 $param_count = $reflection->getNumberOfParameters();
Jonatas Miguelc0d98b22012-08-06 15:42:50 +0100423
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100424 // Are there more parameters than matches?
Jonatas Miguel24296ca2012-09-27 12:10:01 +0100425 if ($param_count > $match_count)
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100426 {
427 // Any params without matches will be set to an empty string.
428 $matches = array_merge($matches, array_fill($match_count, $param_count - $match_count, ''));
Jonatas Miguelefb81662012-10-23 19:52:46 +0100429
430 $match_count = $param_count;
431 }
432
433 // Get the parameters so we can use their default values.
434 $params = $reflection->getParameters();
435
436 for ($m = 0; $m < $match_count; $m++)
437 {
438 // Is the match empty and does a default value exist?
439 if (empty($matches[$m]) && $params[$m]->isDefaultValueAvailable())
440 {
441 // Substitute the empty match for the default value.
442 $matches[$m] = $params[$m]->getDefaultValue();
443 }
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100444 }
Jonatas Miguelc0d98b22012-08-06 15:42:50 +0100445
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100446 // Execute the callback using the values in matches as its parameters.
447 $val = call_user_func_array($val, $matches);
448 }
Andrey Andreevda5562a2012-11-08 12:34:38 +0200449 // Are we using the default routing method for back-references?
450 elseif (strpos($val, '$') !== FALSE && strpos($key, '(') !== FALSE)
451 {
452 $val = preg_replace('#^'.$key.'$#', $val, $uri);
453 }
Jonatas Miguelc0d98b22012-08-06 15:42:50 +0100454
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100455 return $this->_set_request(explode('/', $val));
456 }
457 }
Jonatas Miguelc0d98b22012-08-06 15:42:50 +0100458
Jonatas Miguel59dbe852012-08-07 12:13:32 +0100459 // If we got this far it means we didn't encounter a
460 // matching route so we'll set the site default route
461 $this->_set_request($this->uri->segments);
462 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000463
464 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200465
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300467 * Set class name
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300469 * @param string $class Class name
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200471 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200472 public function set_class($class)
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 {
Derek Jones2615e412010-10-06 17:51:16 -0500474 $this->class = str_replace(array('/', '.'), '', $class);
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 }
Barry Mienydd671972010-10-04 16:33:58 +0200476
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200478
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 /**
480 * Fetch the current class
481 *
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300482 * @deprecated 3.0.0 Read the 'class' property instead
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200484 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200485 public function fetch_class()
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 {
487 return $this->class;
488 }
Barry Mienydd671972010-10-04 16:33:58 +0200489
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200491
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300493 * Set method name
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300495 * @param string $method Method name
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200497 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200498 public function set_method($method)
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 {
500 $this->method = $method;
501 }
502
503 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200504
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 /**
Andrey Andreev7b53d042012-03-26 23:02:32 +0300506 * Fetch the current method
Derek Allard2067d1a2008-11-13 22:59:24 +0000507 *
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300508 * @deprecated 3.0.0 Read the 'method' property instead
Derek Allard2067d1a2008-11-13 22:59:24 +0000509 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200510 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200511 public function fetch_method()
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 {
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300513 return $this->method;
Derek Allard2067d1a2008-11-13 22:59:24 +0000514 }
515
516 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200517
Derek Allard2067d1a2008-11-13 22:59:24 +0000518 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300519 * Set directory name
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 *
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300521 * @param string $dir Directory name
Derek Allard2067d1a2008-11-13 22:59:24 +0000522 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200523 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200524 public function set_directory($dir)
Derek Allard2067d1a2008-11-13 22:59:24 +0000525 {
Derek Jones2615e412010-10-06 17:51:16 -0500526 $this->directory = str_replace(array('/', '.'), '', $dir).'/';
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 }
528
529 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200530
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 /**
Andrey Andreevb9fe7e92012-10-27 18:45:59 +0300532 * Fetch directory
533 *
534 * Feches the sub-directory (if any) that contains the requested
535 * controller class.
Derek Allard2067d1a2008-11-13 22:59:24 +0000536 *
Andrey Andreev0e4237f2013-04-04 16:53:21 +0300537 * @deprecated 3.0.0 Read the 'directory' property instead
Derek Allard2067d1a2008-11-13 22:59:24 +0000538 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200539 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200540 public function fetch_directory()
Derek Allard2067d1a2008-11-13 22:59:24 +0000541 {
542 return $this->directory;
543 }
544
545}
Derek Allard2067d1a2008-11-13 22:59:24 +0000546
547/* End of file Router.php */
Andrey Andreev7b53d042012-03-26 23:02:32 +0300548/* Location: ./system/core/Router.php */