blob: d21319565d4f25bdf5686330987bddc41e18080e [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 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 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
28// ------------------------------------------------------------------------
29
30/**
31 * Router Class
32 *
33 * Parses URIs and determines routing
34 *
35 * @package CodeIgniter
36 * @subpackage Libraries
Derek Jonesf4a4bd82011-10-20 12:18:42 -050037 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000038 * @category Libraries
39 * @link http://codeigniter.com/user_guide/general/routing.html
40 */
41class CI_Router {
42
David Behler07b53422011-08-15 00:25:06 +020043 /**
44 * Config class
45 *
46 * @var object
David Behler07b53422011-08-15 00:25:06 +020047 */
Andrey Andreevba6c0412012-01-07 21:10:09 +020048 public $config;
David Behler07b53422011-08-15 00:25:06 +020049 /**
50 * List of routes
51 *
52 * @var array
David Behler07b53422011-08-15 00:25:06 +020053 */
Andrey Andreevba6c0412012-01-07 21:10:09 +020054 public $routes = array();
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 */
Andrey Andreevba6c0412012-01-07 21:10:09 +020060 public $error_routes = array();
David Behler07b53422011-08-15 00:25:06 +020061 /**
62 * Current class name
63 *
64 * @var string
David Behler07b53422011-08-15 00:25:06 +020065 */
Andrey Andreevba6c0412012-01-07 21:10:09 +020066 public $class = '';
David Behler07b53422011-08-15 00:25:06 +020067 /**
68 * Current method name
69 *
70 * @var string
David Behler07b53422011-08-15 00:25:06 +020071 */
Andrey Andreevba6c0412012-01-07 21:10:09 +020072 public $method = 'index';
David Behler07b53422011-08-15 00:25:06 +020073 /**
74 * Sub-directory that contains the requested controller class
75 *
76 * @var string
David Behler07b53422011-08-15 00:25:06 +020077 */
Andrey Andreevba6c0412012-01-07 21:10:09 +020078 public $directory = '';
David Behler07b53422011-08-15 00:25:06 +020079 /**
80 * Default controller (and method if specific)
81 *
82 * @var string
David Behler07b53422011-08-15 00:25:06 +020083 */
Andrey Andreevba6c0412012-01-07 21:10:09 +020084 public $default_controller;
Barry Mienydd671972010-10-04 16:33:58 +020085
Derek Allard2067d1a2008-11-13 22:59:24 +000086 /**
87 * Constructor
88 *
89 * Runs the route mapping function.
90 */
Andrey Andreevba6c0412012-01-07 21:10:09 +020091 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000092 {
Derek Jonesc7738402010-03-02 13:55:13 -060093 $this->config =& load_class('Config', 'core');
94 $this->uri =& load_class('URI', 'core');
Andrey Andreevba6c0412012-01-07 21:10:09 +020095 log_message('debug', 'Router Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +000096 }
Barry Mienydd671972010-10-04 16:33:58 +020097
Derek Allard2067d1a2008-11-13 22:59:24 +000098 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020099
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 /**
101 * Set the route mapping
102 *
103 * This function determines what should be served based on the URI request,
104 * as well as any "routes" that have been set in the routing config file.
105 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000106 * @return void
107 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200108 public function _set_routing()
Barry Mienydd671972010-10-04 16:33:58 +0200109 {
Andrey Andreevba6c0412012-01-07 21:10:09 +0200110 // Are query strings enabled in the config file? Normally CI doesn't utilize query strings
Barry Mienydd671972010-10-04 16:33:58 +0200111 // since URI segments are more search-engine friendly, but they can optionally be used.
Derek Jonesc7738402010-03-02 13:55:13 -0600112 // If this feature is enabled, we will gather the directory/class/method a little differently
113 $segments = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 if ($this->config->item('enable_query_strings') === TRUE AND isset($_GET[$this->config->item('controller_trigger')]))
115 {
Derek Jonesc7738402010-03-02 13:55:13 -0600116 if (isset($_GET[$this->config->item('directory_trigger')]))
117 {
118 $this->set_directory(trim($this->uri->_filter_uri($_GET[$this->config->item('directory_trigger')])));
119 $segments[] = $this->fetch_directory();
120 }
Barry Mienydd671972010-10-04 16:33:58 +0200121
Derek Jonesc7738402010-03-02 13:55:13 -0600122 if (isset($_GET[$this->config->item('controller_trigger')]))
123 {
124 $this->set_class(trim($this->uri->_filter_uri($_GET[$this->config->item('controller_trigger')])));
125 $segments[] = $this->fetch_class();
126 }
Barry Mienydd671972010-10-04 16:33:58 +0200127
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 if (isset($_GET[$this->config->item('function_trigger')]))
129 {
130 $this->set_method(trim($this->uri->_filter_uri($_GET[$this->config->item('function_trigger')])));
Derek Jonesc7738402010-03-02 13:55:13 -0600131 $segments[] = $this->fetch_method();
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 }
Barry Mienydd671972010-10-04 16:33:58 +0200134
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 // Load the routes.php file.
Greg Akerd96f8822011-12-27 16:23:47 -0600136 if (defined('ENVIRONMENT') AND is_file(APPPATH.'config/'.ENVIRONMENT.'/routes.php'))
137 {
138 include(APPPATH.'config/'.ENVIRONMENT.'/routes.php');
139 }
140 elseif (is_file(APPPATH.'config/routes.php'))
141 {
142 include(APPPATH.'config/routes.php');
143 }
David Behler07b53422011-08-15 00:25:06 +0200144
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 $this->routes = ( ! isset($route) OR ! is_array($route)) ? array() : $route;
146 unset($route);
Barry Mienydd671972010-10-04 16:33:58 +0200147
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 // Set the default controller so we can display it in the event
149 // the URI doesn't correlated to a valid controller.
Barry Mienydd671972010-10-04 16:33:58 +0200150 $this->default_controller = ( ! isset($this->routes['default_controller']) OR $this->routes['default_controller'] == '') ? FALSE : strtolower($this->routes['default_controller']);
151
Andrey Andreevba6c0412012-01-07 21:10:09 +0200152 // 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 -0600153 if (count($segments) > 0)
154 {
155 return $this->_validate_request($segments);
156 }
Barry Mienydd671972010-10-04 16:33:58 +0200157
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 // Fetch the complete URI string
159 $this->uri->_fetch_uri_string();
Barry Mienydd671972010-10-04 16:33:58 +0200160
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 // Is there a URI string? If not, the default controller specified in the "routes" file will be shown.
162 if ($this->uri->uri_string == '')
163 {
Derek Jonesc7738402010-03-02 13:55:13 -0600164 return $this->_set_default_controller();
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 }
Barry Mienydd671972010-10-04 16:33:58 +0200166
Andrey Andreevba6c0412012-01-07 21:10:09 +0200167 $this->uri->_remove_url_suffix(); // Remove the URL suffix
168 $this->uri->_explode_segments(); // Compile the segments into an array
169 $this->_parse_routes(); // Parse any custom routing that may exist
170 $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 +0000171 }
Derek Jonesc7738402010-03-02 13:55:13 -0600172
173 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200174
Derek Jonesc7738402010-03-02 13:55:13 -0600175 /**
176 * Set the default controller
177 *
Derek Jonesc7738402010-03-02 13:55:13 -0600178 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200179 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200180 protected function _set_default_controller()
Derek Jonesc7738402010-03-02 13:55:13 -0600181 {
182 if ($this->default_controller === FALSE)
183 {
Andrey Andreevba6c0412012-01-07 21:10:09 +0200184 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 -0600185 }
186 // Is the method being specified?
187 if (strpos($this->default_controller, '/') !== FALSE)
188 {
189 $x = explode('/', $this->default_controller);
Derek Jonesc7738402010-03-02 13:55:13 -0600190 $this->set_class($x[0]);
191 $this->set_method($x[1]);
Pascal Kriete790ebf32010-12-15 10:53:35 -0500192 $this->_set_request($x);
Barry Mienydd671972010-10-04 16:33:58 +0200193 }
Derek Jonesc7738402010-03-02 13:55:13 -0600194 else
195 {
196 $this->set_class($this->default_controller);
197 $this->set_method('index');
198 $this->_set_request(array($this->default_controller, 'index'));
199 }
Barry Mienydd671972010-10-04 16:33:58 +0200200
Derek Jonesc7738402010-03-02 13:55:13 -0600201 // re-index the routed segments array so it starts with 1 rather than 0
202 $this->uri->_reindex_segments();
Barry Mienydd671972010-10-04 16:33:58 +0200203
Andrey Andreevba6c0412012-01-07 21:10:09 +0200204 log_message('debug', 'No URI present. Default controller set.');
Derek Jonesc7738402010-03-02 13:55:13 -0600205 }
Barry Mienydd671972010-10-04 16:33:58 +0200206
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200208
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 /**
210 * Set the Route
211 *
212 * This function takes an array of URI segments as
213 * input, and sets the current class/method
214 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 * @param array
216 * @param bool
217 * @return void
218 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200219 protected function _set_request($segments = array())
Barry Mienydd671972010-10-04 16:33:58 +0200220 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 $segments = $this->_validate_request($segments);
Barry Mienydd671972010-10-04 16:33:58 +0200222
Andrey Andreevba6c0412012-01-07 21:10:09 +0200223 if (count($segments) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 {
Derek Jonesc7738402010-03-02 13:55:13 -0600225 return $this->_set_default_controller();
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 }
Barry Mienydd671972010-10-04 16:33:58 +0200227
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 $this->set_class($segments[0]);
Barry Mienydd671972010-10-04 16:33:58 +0200229
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 if (isset($segments[1]))
231 {
Derek Jonesc7738402010-03-02 13:55:13 -0600232 // A standard method request
233 $this->set_method($segments[1]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 }
235 else
236 {
237 // This lets the "routed" segment array identify that the default
238 // index method is being used.
239 $segments[1] = 'index';
240 }
Barry Mienydd671972010-10-04 16:33:58 +0200241
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 // Update our "routed" segment array to contain the segments.
243 // Note: If there is no custom routing, this array will be
244 // identical to $this->uri->segments
245 $this->uri->rsegments = $segments;
246 }
Barry Mienydd671972010-10-04 16:33:58 +0200247
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200249
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -0500251 * Validates the supplied segments. Attempts to determine the path to
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 * the controller.
253 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 * @param array
255 * @return array
Barry Mienydd671972010-10-04 16:33:58 +0200256 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200257 protected function _validate_request($segments)
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 {
Andrey Andreevba6c0412012-01-07 21:10:09 +0200259 if (count($segments) === 0)
Derek Jonesc7738402010-03-02 13:55:13 -0600260 {
261 return $segments;
262 }
Barry Mienydd671972010-10-04 16:33:58 +0200263
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 // Does the requested controller exist in the root folder?
Greg Aker3a746652011-04-19 10:59:47 -0500265 if (file_exists(APPPATH.'controllers/'.$segments[0].'.php'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000266 {
267 return $segments;
268 }
Barry Mienydd671972010-10-04 16:33:58 +0200269
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 // Is the controller in a sub-folder?
271 if (is_dir(APPPATH.'controllers/'.$segments[0]))
Derek Jonesc7738402010-03-02 13:55:13 -0600272 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 // Set the directory and remove it from the segment array
274 $this->set_directory($segments[0]);
275 $segments = array_slice($segments, 1);
Barry Mienydd671972010-10-04 16:33:58 +0200276
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 if (count($segments) > 0)
278 {
279 // Does the requested controller exist in the sub-folder?
Greg Aker3a746652011-04-19 10:59:47 -0500280 if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].'.php'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000281 {
Shane Pearson664a9352011-08-10 16:02:32 -0500282 if ( ! empty($this->routes['404_override']))
283 {
284 $x = explode('/', $this->routes['404_override']);
Shane Pearson664a9352011-08-10 16:02:32 -0500285 $this->set_directory('');
286 $this->set_class($x[0]);
287 $this->set_method(isset($x[1]) ? $x[1] : 'index');
David Behler07b53422011-08-15 00:25:06 +0200288
Shane Pearson664a9352011-08-10 16:02:32 -0500289 return $x;
290 }
291 else
292 {
293 show_404($this->fetch_directory().$segments[0]);
294 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 }
296 }
297 else
298 {
Derek Jonesc7738402010-03-02 13:55:13 -0600299 // Is the method being specified in the route?
300 if (strpos($this->default_controller, '/') !== FALSE)
301 {
302 $x = explode('/', $this->default_controller);
Derek Jonesc7738402010-03-02 13:55:13 -0600303 $this->set_class($x[0]);
304 $this->set_method($x[1]);
Barry Mienydd671972010-10-04 16:33:58 +0200305 }
Derek Jonesc7738402010-03-02 13:55:13 -0600306 else
307 {
308 $this->set_class($this->default_controller);
309 $this->set_method('index');
310 }
Barry Mienydd671972010-10-04 16:33:58 +0200311
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 // Does the default controller exist in the sub-folder?
Greg Aker3a746652011-04-19 10:59:47 -0500313 if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.'.php'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 {
315 $this->directory = '';
316 return array();
317 }
Barry Mienydd671972010-10-04 16:33:58 +0200318
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 }
Barry Mienydd671972010-10-04 16:33:58 +0200320
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 return $segments;
322 }
Barry Mienydd671972010-10-04 16:33:58 +0200323
324
Derek Jonesc7738402010-03-02 13:55:13 -0600325 // If we've gotten this far it means that the URI does not correlate to a valid
Andrey Andreevba6c0412012-01-07 21:10:09 +0200326 // controller class. We will now see if there is an override
Eric Barnesc5bf6162011-01-30 21:17:11 -0500327 if ( ! empty($this->routes['404_override']))
Derek Jonesc7738402010-03-02 13:55:13 -0600328 {
Phil Sturgeon23174a62010-12-15 15:18:16 +0000329 $x = explode('/', $this->routes['404_override']);
Phil Sturgeon23174a62010-12-15 15:18:16 +0000330 $this->set_class($x[0]);
331 $this->set_method(isset($x[1]) ? $x[1] : 'index');
Barry Mienydd671972010-10-04 16:33:58 +0200332
Phil Sturgeon23174a62010-12-15 15:18:16 +0000333 return $x;
Derek Jonesc7738402010-03-02 13:55:13 -0600334 }
Barry Mienydd671972010-10-04 16:33:58 +0200335
Derek Jonesc7738402010-03-02 13:55:13 -0600336 // Nothing else to do at this point but show a 404
Barry Mienydd671972010-10-04 16:33:58 +0200337 show_404($segments[0]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 }
Barry Mienydd671972010-10-04 16:33:58 +0200339
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200341
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -0500343 * Parse Routes
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 *
345 * This function matches any routes that may exist in
346 * the config/routes.php file against the URI to
347 * determine if the class/method need to be remapped.
348 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 * @return void
350 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200351 protected function _parse_routes()
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 // Turn the segment array into a URI string
354 $uri = implode('/', $this->uri->segments);
Barry Mienydd671972010-10-04 16:33:58 +0200355
Derek Jones37f4b9c2011-07-01 17:56:50 -0500356 // Is there a literal match? If so we're done
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 if (isset($this->routes[$uri]))
358 {
Derek Jonesc7738402010-03-02 13:55:13 -0600359 return $this->_set_request(explode('/', $this->routes[$uri]));
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 }
Barry Mienydd671972010-10-04 16:33:58 +0200361
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 // Loop through the route array looking for wild-cards
363 foreach ($this->routes as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200364 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 // Convert wild-cards to RegEx
Andrey Andreevba6c0412012-01-07 21:10:09 +0200366 $key = str_replace(array(':any', ':num'), array('.+', '[0-9]+'), $key);
Derek Jonesc7738402010-03-02 13:55:13 -0600367
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 // Does the RegEx match?
369 if (preg_match('#^'.$key.'$#', $uri))
Derek Jonesc7738402010-03-02 13:55:13 -0600370 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 // Do we have a back-reference?
372 if (strpos($val, '$') !== FALSE AND strpos($key, '(') !== FALSE)
373 {
374 $val = preg_replace('#^'.$key.'$#', $val, $uri);
375 }
Barry Mienydd671972010-10-04 16:33:58 +0200376
377 return $this->_set_request(explode('/', $val));
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 }
379 }
380
381 // If we got this far it means we didn't encounter a
382 // matching route so we'll set the site default route
383 $this->_set_request($this->uri->segments);
384 }
385
386 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200387
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 /**
389 * Set the class name
390 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000391 * @param string
392 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200393 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200394 public function set_class($class)
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 {
Derek Jones2615e412010-10-06 17:51:16 -0500396 $this->class = str_replace(array('/', '.'), '', $class);
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 }
Barry Mienydd671972010-10-04 16:33:58 +0200398
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200400
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 /**
402 * Fetch the current class
403 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200405 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200406 public function fetch_class()
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 {
408 return $this->class;
409 }
Barry Mienydd671972010-10-04 16:33:58 +0200410
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200412
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -0500414 * Set the method name
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 * @param string
417 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200418 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200419 public function set_method($method)
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 {
421 $this->method = $method;
422 }
423
424 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200425
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -0500427 * Fetch the current method
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200430 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200431 public function fetch_method()
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 {
433 if ($this->method == $this->fetch_class())
434 {
435 return 'index';
436 }
437
438 return $this->method;
439 }
440
441 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200442
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -0500444 * Set the directory name
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 * @param string
447 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200448 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200449 public function set_directory($dir)
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 {
Derek Jones2615e412010-10-06 17:51:16 -0500451 $this->directory = str_replace(array('/', '.'), '', $dir).'/';
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 }
453
454 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200455
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -0500457 * Fetch the sub-directory (if any) that contains the requested controller class
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200460 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200461 public function fetch_directory()
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 {
463 return $this->directory;
464 }
465
Derek Jonesc7738402010-03-02 13:55:13 -0600466 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200467
Derek Jonesc7738402010-03-02 13:55:13 -0600468 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -0500469 * Set the controller overrides
Derek Jonesc7738402010-03-02 13:55:13 -0600470 *
Derek Jonesc7738402010-03-02 13:55:13 -0600471 * @param array
472 * @return null
Barry Mienydd671972010-10-04 16:33:58 +0200473 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200474 public function _set_overrides($routing)
Derek Jonesc7738402010-03-02 13:55:13 -0600475 {
476 if ( ! is_array($routing))
477 {
478 return;
479 }
Barry Mienydd671972010-10-04 16:33:58 +0200480
Derek Jonesc7738402010-03-02 13:55:13 -0600481 if (isset($routing['directory']))
482 {
483 $this->set_directory($routing['directory']);
484 }
Barry Mienydd671972010-10-04 16:33:58 +0200485
Derek Jonesc7738402010-03-02 13:55:13 -0600486 if (isset($routing['controller']) AND $routing['controller'] != '')
487 {
488 $this->set_class($routing['controller']);
489 }
Barry Mienydd671972010-10-04 16:33:58 +0200490
Derek Jonesc7738402010-03-02 13:55:13 -0600491 if (isset($routing['function']))
492 {
493 $routing['function'] = ($routing['function'] == '') ? 'index' : $routing['function'];
494 $this->set_method($routing['function']);
495 }
496 }
497
Derek Allard2067d1a2008-11-13 22:59:24 +0000498}
Derek Allard2067d1a2008-11-13 22:59:24 +0000499
500/* End of file Router.php */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200501/* Location: ./system/core/Router.php */