blob: fe9909b069ce042a6f19b76db24a888f41bb688b [file] [log] [blame]
Andrey Andreevba6c0412012-01-07 21:10:09 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreevba6c0412012-01-07 21:10:09 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreevba6c0412012-01-07 21:10:09 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * Router Class
30 *
31 * Parses URIs and determines routing
32 *
33 * @package CodeIgniter
34 * @subpackage Libraries
Derek Jonesf4a4bd82011-10-20 12:18:42 -050035 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @category Libraries
37 * @link http://codeigniter.com/user_guide/general/routing.html
38 */
39class CI_Router {
40
David Behler07b53422011-08-15 00:25:06 +020041 /**
42 * Config class
43 *
44 * @var object
David Behler07b53422011-08-15 00:25:06 +020045 */
Andrey Andreevba6c0412012-01-07 21:10:09 +020046 public $config;
Timothy Warren40403d22012-04-19 16:38:50 -040047
David Behler07b53422011-08-15 00:25:06 +020048 /**
49 * List of routes
50 *
51 * @var array
David Behler07b53422011-08-15 00:25:06 +020052 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040053 public $routes = array();
Timothy Warren40403d22012-04-19 16:38:50 -040054
David Behler07b53422011-08-15 00:25:06 +020055 /**
56 * List of error routes
57 *
58 * @var array
David Behler07b53422011-08-15 00:25:06 +020059 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040060 public $error_routes = array();
Timothy Warren40403d22012-04-19 16:38:50 -040061
David Behler07b53422011-08-15 00:25:06 +020062 /**
63 * Current class name
64 *
65 * @var string
David Behler07b53422011-08-15 00:25:06 +020066 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040067 public $class = '';
Timothy Warren40403d22012-04-19 16:38:50 -040068
David Behler07b53422011-08-15 00:25:06 +020069 /**
70 * Current method name
71 *
72 * @var string
David Behler07b53422011-08-15 00:25:06 +020073 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040074 public $method = 'index';
Timothy Warren40403d22012-04-19 16:38:50 -040075
David Behler07b53422011-08-15 00:25:06 +020076 /**
77 * Sub-directory that contains the requested controller class
78 *
79 * @var string
David Behler07b53422011-08-15 00:25:06 +020080 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040081 public $directory = '';
Timothy Warren40403d22012-04-19 16:38:50 -040082
David Behler07b53422011-08-15 00:25:06 +020083 /**
84 * Default controller (and method if specific)
85 *
86 * @var string
David Behler07b53422011-08-15 00:25:06 +020087 */
Andrey Andreevba6c0412012-01-07 21:10:09 +020088 public $default_controller;
Barry Mienydd671972010-10-04 16:33:58 +020089
Derek Allard2067d1a2008-11-13 22:59:24 +000090 /**
91 * Constructor
92 *
93 * Runs the route mapping function.
94 */
Andrey Andreevba6c0412012-01-07 21:10:09 +020095 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000096 {
Derek Jonesc7738402010-03-02 13:55:13 -060097 $this->config =& load_class('Config', 'core');
98 $this->uri =& load_class('URI', 'core');
Andrey Andreevba6c0412012-01-07 21:10:09 +020099 log_message('debug', 'Router Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 }
Barry Mienydd671972010-10-04 16:33:58 +0200101
Derek Allard2067d1a2008-11-13 22:59:24 +0000102 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200103
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 /**
105 * Set the route mapping
106 *
107 * This function determines what should be served based on the URI request,
108 * as well as any "routes" that have been set in the routing config file.
109 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000110 * @return void
111 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200112 public function _set_routing()
Barry Mienydd671972010-10-04 16:33:58 +0200113 {
Andrey Andreevba6c0412012-01-07 21:10:09 +0200114 // Are query strings enabled in the config file? Normally CI doesn't utilize query strings
Barry Mienydd671972010-10-04 16:33:58 +0200115 // since URI segments are more search-engine friendly, but they can optionally be used.
Derek Jonesc7738402010-03-02 13:55:13 -0600116 // If this feature is enabled, we will gather the directory/class/method a little differently
117 $segments = array();
Andrey Andreev7b53d042012-03-26 23:02:32 +0300118 if ($this->config->item('enable_query_strings') === TRUE && isset($_GET[$this->config->item('controller_trigger')]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 {
Derek Jonesc7738402010-03-02 13:55:13 -0600120 if (isset($_GET[$this->config->item('directory_trigger')]))
121 {
122 $this->set_directory(trim($this->uri->_filter_uri($_GET[$this->config->item('directory_trigger')])));
123 $segments[] = $this->fetch_directory();
124 }
Barry Mienydd671972010-10-04 16:33:58 +0200125
Derek Jonesc7738402010-03-02 13:55:13 -0600126 if (isset($_GET[$this->config->item('controller_trigger')]))
127 {
128 $this->set_class(trim($this->uri->_filter_uri($_GET[$this->config->item('controller_trigger')])));
129 $segments[] = $this->fetch_class();
130 }
Barry Mienydd671972010-10-04 16:33:58 +0200131
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 if (isset($_GET[$this->config->item('function_trigger')]))
133 {
134 $this->set_method(trim($this->uri->_filter_uri($_GET[$this->config->item('function_trigger')])));
Derek Jonesc7738402010-03-02 13:55:13 -0600135 $segments[] = $this->fetch_method();
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 }
Barry Mienydd671972010-10-04 16:33:58 +0200138
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 // Load the routes.php file.
Andrey Andreev7b53d042012-03-26 23:02:32 +0300140 if (defined('ENVIRONMENT') && is_file(APPPATH.'config/'.ENVIRONMENT.'/routes.php'))
Greg Akerd96f8822011-12-27 16:23:47 -0600141 {
142 include(APPPATH.'config/'.ENVIRONMENT.'/routes.php');
143 }
144 elseif (is_file(APPPATH.'config/routes.php'))
145 {
146 include(APPPATH.'config/routes.php');
147 }
David Behler07b53422011-08-15 00:25:06 +0200148
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 $this->routes = ( ! isset($route) OR ! is_array($route)) ? array() : $route;
150 unset($route);
Barry Mienydd671972010-10-04 16:33:58 +0200151
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 // Set the default controller so we can display it in the event
153 // the URI doesn't correlated to a valid controller.
Andrey Andreev7b53d042012-03-26 23:02:32 +0300154 $this->default_controller = empty($this->routes['default_controller']) ? FALSE : strtolower($this->routes['default_controller']);
Barry Mienydd671972010-10-04 16:33:58 +0200155
Andrey Andreevba6c0412012-01-07 21:10:09 +0200156 // 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 -0600157 if (count($segments) > 0)
158 {
159 return $this->_validate_request($segments);
160 }
Barry Mienydd671972010-10-04 16:33:58 +0200161
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 // Fetch the complete URI string
163 $this->uri->_fetch_uri_string();
Barry Mienydd671972010-10-04 16:33:58 +0200164
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 // Is there a URI string? If not, the default controller specified in the "routes" file will be shown.
166 if ($this->uri->uri_string == '')
167 {
Derek Jonesc7738402010-03-02 13:55:13 -0600168 return $this->_set_default_controller();
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 }
Barry Mienydd671972010-10-04 16:33:58 +0200170
Andrey Andreevba6c0412012-01-07 21:10:09 +0200171 $this->uri->_remove_url_suffix(); // Remove the URL suffix
172 $this->uri->_explode_segments(); // Compile the segments into an array
173 $this->_parse_routes(); // Parse any custom routing that may exist
174 $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 +0000175 }
Derek Jonesc7738402010-03-02 13:55:13 -0600176
177 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200178
Derek Jonesc7738402010-03-02 13:55:13 -0600179 /**
180 * Set the default controller
181 *
Derek Jonesc7738402010-03-02 13:55:13 -0600182 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200183 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200184 protected function _set_default_controller()
Derek Jonesc7738402010-03-02 13:55:13 -0600185 {
186 if ($this->default_controller === FALSE)
187 {
Andrey Andreevba6c0412012-01-07 21:10:09 +0200188 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 -0600189 }
190 // Is the method being specified?
191 if (strpos($this->default_controller, '/') !== FALSE)
192 {
193 $x = explode('/', $this->default_controller);
Derek Jonesc7738402010-03-02 13:55:13 -0600194 $this->set_class($x[0]);
195 $this->set_method($x[1]);
Pascal Kriete790ebf32010-12-15 10:53:35 -0500196 $this->_set_request($x);
Barry Mienydd671972010-10-04 16:33:58 +0200197 }
Derek Jonesc7738402010-03-02 13:55:13 -0600198 else
199 {
200 $this->set_class($this->default_controller);
201 $this->set_method('index');
202 $this->_set_request(array($this->default_controller, 'index'));
203 }
Barry Mienydd671972010-10-04 16:33:58 +0200204
Derek Jonesc7738402010-03-02 13:55:13 -0600205 // re-index the routed segments array so it starts with 1 rather than 0
206 $this->uri->_reindex_segments();
Barry Mienydd671972010-10-04 16:33:58 +0200207
Andrey Andreevba6c0412012-01-07 21:10:09 +0200208 log_message('debug', 'No URI present. Default controller set.');
Derek Jonesc7738402010-03-02 13:55:13 -0600209 }
Barry Mienydd671972010-10-04 16:33:58 +0200210
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200212
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 /**
214 * Set the Route
215 *
216 * This function takes an array of URI segments as
217 * input, and sets the current class/method
218 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 * @param array
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 * @return void
221 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200222 protected function _set_request($segments = array())
Barry Mienydd671972010-10-04 16:33:58 +0200223 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 $segments = $this->_validate_request($segments);
Barry Mienydd671972010-10-04 16:33:58 +0200225
Andrey Andreevba6c0412012-01-07 21:10:09 +0200226 if (count($segments) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 {
Derek Jonesc7738402010-03-02 13:55:13 -0600228 return $this->_set_default_controller();
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 }
Barry Mienydd671972010-10-04 16:33:58 +0200230
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 $this->set_class($segments[0]);
Barry Mienydd671972010-10-04 16:33:58 +0200232
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 if (isset($segments[1]))
234 {
Derek Jonesc7738402010-03-02 13:55:13 -0600235 // A standard method request
236 $this->set_method($segments[1]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 }
238 else
239 {
240 // This lets the "routed" segment array identify that the default
241 // index method is being used.
242 $segments[1] = 'index';
243 }
Barry Mienydd671972010-10-04 16:33:58 +0200244
Phil Sturgeoncdfbd522012-04-26 22:13:31 +0100245 // This is being routed to a file in a sub directory
246 $this->directory and array_unshift($segments, trim($this->directory, '/'));
247
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 // Update our "routed" segment array to contain the segments.
249 // Note: If there is no custom routing, this array will be
Phil Sturgeoncdfbd522012-04-26 22:13:31 +0100250 // identical to $this->uri->segments
Derek Allard2067d1a2008-11-13 22:59:24 +0000251 $this->uri->rsegments = $segments;
252 }
Barry Mienydd671972010-10-04 16:33:58 +0200253
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200255
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 /**
Andrey Andreev7b53d042012-03-26 23:02:32 +0300257 * Validates the supplied segments.
258 * Attempts to determine the path to the controller.
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 * @param array
261 * @return array
Barry Mienydd671972010-10-04 16:33:58 +0200262 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200263 protected function _validate_request($segments)
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 {
Andrey Andreevba6c0412012-01-07 21:10:09 +0200265 if (count($segments) === 0)
Derek Jonesc7738402010-03-02 13:55:13 -0600266 {
267 return $segments;
268 }
Barry Mienydd671972010-10-04 16:33:58 +0200269
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 // Does the requested controller exist in the root folder?
Greg Aker3a746652011-04-19 10:59:47 -0500271 if (file_exists(APPPATH.'controllers/'.$segments[0].'.php'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 {
273 return $segments;
274 }
Barry Mienydd671972010-10-04 16:33:58 +0200275
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 // Is the controller in a sub-folder?
277 if (is_dir(APPPATH.'controllers/'.$segments[0]))
Derek Jonesc7738402010-03-02 13:55:13 -0600278 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 // Set the directory and remove it from the segment array
280 $this->set_directory($segments[0]);
281 $segments = array_slice($segments, 1);
Barry Mienydd671972010-10-04 16:33:58 +0200282
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 if (count($segments) > 0)
284 {
285 // Does the requested controller exist in the sub-folder?
Greg Aker3a746652011-04-19 10:59:47 -0500286 if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].'.php'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 {
Shane Pearson664a9352011-08-10 16:02:32 -0500288 if ( ! empty($this->routes['404_override']))
289 {
290 $x = explode('/', $this->routes['404_override']);
Shane Pearson664a9352011-08-10 16:02:32 -0500291 $this->set_directory('');
292 $this->set_class($x[0]);
293 $this->set_method(isset($x[1]) ? $x[1] : 'index');
David Behler07b53422011-08-15 00:25:06 +0200294
Shane Pearson664a9352011-08-10 16:02:32 -0500295 return $x;
296 }
297 else
298 {
299 show_404($this->fetch_directory().$segments[0]);
300 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 }
302 }
303 else
304 {
Derek Jonesc7738402010-03-02 13:55:13 -0600305 // Is the method being specified in the route?
306 if (strpos($this->default_controller, '/') !== FALSE)
307 {
308 $x = explode('/', $this->default_controller);
Derek Jonesc7738402010-03-02 13:55:13 -0600309 $this->set_class($x[0]);
310 $this->set_method($x[1]);
Barry Mienydd671972010-10-04 16:33:58 +0200311 }
Derek Jonesc7738402010-03-02 13:55:13 -0600312 else
313 {
314 $this->set_class($this->default_controller);
315 $this->set_method('index');
316 }
Barry Mienydd671972010-10-04 16:33:58 +0200317
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 // Does the default controller exist in the sub-folder?
Greg Aker3a746652011-04-19 10:59:47 -0500319 if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.'.php'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 {
321 $this->directory = '';
322 return array();
323 }
Barry Mienydd671972010-10-04 16:33:58 +0200324
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 }
Barry Mienydd671972010-10-04 16:33:58 +0200326
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 return $segments;
328 }
Barry Mienydd671972010-10-04 16:33:58 +0200329
330
Derek Jonesc7738402010-03-02 13:55:13 -0600331 // If we've gotten this far it means that the URI does not correlate to a valid
Andrey Andreevba6c0412012-01-07 21:10:09 +0200332 // controller class. We will now see if there is an override
Eric Barnesc5bf6162011-01-30 21:17:11 -0500333 if ( ! empty($this->routes['404_override']))
Derek Jonesc7738402010-03-02 13:55:13 -0600334 {
Phil Sturgeon23174a62010-12-15 15:18:16 +0000335 $x = explode('/', $this->routes['404_override']);
Phil Sturgeon23174a62010-12-15 15:18:16 +0000336 $this->set_class($x[0]);
337 $this->set_method(isset($x[1]) ? $x[1] : 'index');
Barry Mienydd671972010-10-04 16:33:58 +0200338
Phil Sturgeon23174a62010-12-15 15:18:16 +0000339 return $x;
Derek Jonesc7738402010-03-02 13:55:13 -0600340 }
Barry Mienydd671972010-10-04 16:33:58 +0200341
Derek Jonesc7738402010-03-02 13:55:13 -0600342 // Nothing else to do at this point but show a 404
Barry Mienydd671972010-10-04 16:33:58 +0200343 show_404($segments[0]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 }
Barry Mienydd671972010-10-04 16:33:58 +0200345
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200347
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 /**
Andrey Andreev7b53d042012-03-26 23:02:32 +0300349 * Parse Routes
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 *
351 * This function matches any routes that may exist in
352 * the config/routes.php file against the URI to
353 * determine if the class/method need to be remapped.
354 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 * @return void
356 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200357 protected function _parse_routes()
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 // Turn the segment array into a URI string
360 $uri = implode('/', $this->uri->segments);
Barry Mienydd671972010-10-04 16:33:58 +0200361
Derek Jones37f4b9c2011-07-01 17:56:50 -0500362 // Is there a literal match? If so we're done
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 if (isset($this->routes[$uri]))
364 {
Derek Jonesc7738402010-03-02 13:55:13 -0600365 return $this->_set_request(explode('/', $this->routes[$uri]));
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 }
Barry Mienydd671972010-10-04 16:33:58 +0200367
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 // Loop through the route array looking for wild-cards
369 foreach ($this->routes as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200370 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 // Convert wild-cards to RegEx
Andrey Andreevba6c0412012-01-07 21:10:09 +0200372 $key = str_replace(array(':any', ':num'), array('.+', '[0-9]+'), $key);
Derek Jonesc7738402010-03-02 13:55:13 -0600373
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 // Does the RegEx match?
375 if (preg_match('#^'.$key.'$#', $uri))
Derek Jonesc7738402010-03-02 13:55:13 -0600376 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 // Do we have a back-reference?
Andrey Andreev7b53d042012-03-26 23:02:32 +0300378 if (strpos($val, '$') !== FALSE && strpos($key, '(') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 {
380 $val = preg_replace('#^'.$key.'$#', $val, $uri);
381 }
Barry Mienydd671972010-10-04 16:33:58 +0200382
383 return $this->_set_request(explode('/', $val));
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 }
385 }
386
387 // If we got this far it means we didn't encounter a
388 // matching route so we'll set the site default route
389 $this->_set_request($this->uri->segments);
390 }
391
392 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200393
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 /**
395 * Set the class name
396 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 * @param string
398 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200399 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200400 public function set_class($class)
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 {
Derek Jones2615e412010-10-06 17:51:16 -0500402 $this->class = str_replace(array('/', '.'), '', $class);
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 }
Barry Mienydd671972010-10-04 16:33:58 +0200404
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200406
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 /**
408 * Fetch the current class
409 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200411 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200412 public function fetch_class()
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 {
414 return $this->class;
415 }
Barry Mienydd671972010-10-04 16:33:58 +0200416
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200418
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 /**
Andrey Andreev7b53d042012-03-26 23:02:32 +0300420 * Set the method name
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 * @param string
423 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200424 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200425 public function set_method($method)
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 {
427 $this->method = $method;
428 }
429
430 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200431
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 /**
Andrey Andreev7b53d042012-03-26 23:02:32 +0300433 * Fetch the current method
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200436 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200437 public function fetch_method()
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 {
439 if ($this->method == $this->fetch_class())
440 {
441 return 'index';
442 }
443
444 return $this->method;
445 }
446
447 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200448
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 /**
Andrey Andreev7b53d042012-03-26 23:02:32 +0300450 * Set the directory name
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 * @param string
453 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200454 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200455 public function set_directory($dir)
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 {
Derek Jones2615e412010-10-06 17:51:16 -0500457 $this->directory = str_replace(array('/', '.'), '', $dir).'/';
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 }
459
460 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200461
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 /**
Andrey Andreev7b53d042012-03-26 23:02:32 +0300463 * Fetch the sub-directory (if any) that contains the requested controller class
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200466 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200467 public function fetch_directory()
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 {
469 return $this->directory;
470 }
471
Derek Jonesc7738402010-03-02 13:55:13 -0600472 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200473
Derek Jonesc7738402010-03-02 13:55:13 -0600474 /**
Andrey Andreev7b53d042012-03-26 23:02:32 +0300475 * Set the controller overrides
Derek Jonesc7738402010-03-02 13:55:13 -0600476 *
Derek Jonesc7738402010-03-02 13:55:13 -0600477 * @param array
Andrey Andreev7b53d042012-03-26 23:02:32 +0300478 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200479 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200480 public function _set_overrides($routing)
Derek Jonesc7738402010-03-02 13:55:13 -0600481 {
482 if ( ! is_array($routing))
483 {
484 return;
485 }
Barry Mienydd671972010-10-04 16:33:58 +0200486
Derek Jonesc7738402010-03-02 13:55:13 -0600487 if (isset($routing['directory']))
488 {
489 $this->set_directory($routing['directory']);
490 }
Barry Mienydd671972010-10-04 16:33:58 +0200491
Andrey Andreev7b53d042012-03-26 23:02:32 +0300492 if (isset($routing['controller']) && $routing['controller'] != '')
Derek Jonesc7738402010-03-02 13:55:13 -0600493 {
494 $this->set_class($routing['controller']);
495 }
Barry Mienydd671972010-10-04 16:33:58 +0200496
Derek Jonesc7738402010-03-02 13:55:13 -0600497 if (isset($routing['function']))
498 {
499 $routing['function'] = ($routing['function'] == '') ? 'index' : $routing['function'];
500 $this->set_method($routing['function']);
501 }
502 }
503
Derek Allard2067d1a2008-11-13 22:59:24 +0000504}
Derek Allard2067d1a2008-11-13 22:59:24 +0000505
506/* End of file Router.php */
Andrey Andreev7b53d042012-03-26 23:02:32 +0300507/* Location: ./system/core/Router.php */