blob: 5477fed5dd9caac92a8729aabcbd13c86d821a13 [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;
David Behler07b53422011-08-15 00:25:06 +020047 /**
48 * List of routes
49 *
50 * @var array
David Behler07b53422011-08-15 00:25:06 +020051 */
Andrey Andreevba6c0412012-01-07 21:10:09 +020052 public $routes = array();
David Behler07b53422011-08-15 00:25:06 +020053 /**
54 * List of error routes
55 *
56 * @var array
David Behler07b53422011-08-15 00:25:06 +020057 */
Andrey Andreevba6c0412012-01-07 21:10:09 +020058 public $error_routes = array();
David Behler07b53422011-08-15 00:25:06 +020059 /**
60 * Current class name
61 *
62 * @var string
David Behler07b53422011-08-15 00:25:06 +020063 */
Andrey Andreevba6c0412012-01-07 21:10:09 +020064 public $class = '';
David Behler07b53422011-08-15 00:25:06 +020065 /**
66 * Current method name
67 *
68 * @var string
David Behler07b53422011-08-15 00:25:06 +020069 */
Andrey Andreevba6c0412012-01-07 21:10:09 +020070 public $method = 'index';
David Behler07b53422011-08-15 00:25:06 +020071 /**
72 * Sub-directory that contains the requested controller class
73 *
74 * @var string
David Behler07b53422011-08-15 00:25:06 +020075 */
Andrey Andreevba6c0412012-01-07 21:10:09 +020076 public $directory = '';
David Behler07b53422011-08-15 00:25:06 +020077 /**
78 * Default controller (and method if specific)
79 *
80 * @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 /**
85 * Constructor
86 *
87 * Runs the route mapping function.
88 */
Andrey Andreevba6c0412012-01-07 21:10:09 +020089 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000090 {
Derek Jonesc7738402010-03-02 13:55:13 -060091 $this->config =& load_class('Config', 'core');
92 $this->uri =& load_class('URI', 'core');
Andrey Andreevba6c0412012-01-07 21:10:09 +020093 log_message('debug', 'Router Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +000094 }
Barry Mienydd671972010-10-04 16:33:58 +020095
Derek Allard2067d1a2008-11-13 22:59:24 +000096 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020097
Derek Allard2067d1a2008-11-13 22:59:24 +000098 /**
99 * Set the route mapping
100 *
101 * This function determines what should be served based on the URI request,
102 * as well as any "routes" that have been set in the routing config file.
103 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 * @return void
105 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200106 public function _set_routing()
Barry Mienydd671972010-10-04 16:33:58 +0200107 {
Andrey Andreevba6c0412012-01-07 21:10:09 +0200108 // Are query strings enabled in the config file? Normally CI doesn't utilize query strings
Barry Mienydd671972010-10-04 16:33:58 +0200109 // since URI segments are more search-engine friendly, but they can optionally be used.
Derek Jonesc7738402010-03-02 13:55:13 -0600110 // If this feature is enabled, we will gather the directory/class/method a little differently
111 $segments = array();
Andrey Andreev7b53d042012-03-26 23:02:32 +0300112 if ($this->config->item('enable_query_strings') === TRUE && isset($_GET[$this->config->item('controller_trigger')]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 {
Derek Jonesc7738402010-03-02 13:55:13 -0600114 if (isset($_GET[$this->config->item('directory_trigger')]))
115 {
116 $this->set_directory(trim($this->uri->_filter_uri($_GET[$this->config->item('directory_trigger')])));
117 $segments[] = $this->fetch_directory();
118 }
Barry Mienydd671972010-10-04 16:33:58 +0200119
Derek Jonesc7738402010-03-02 13:55:13 -0600120 if (isset($_GET[$this->config->item('controller_trigger')]))
121 {
122 $this->set_class(trim($this->uri->_filter_uri($_GET[$this->config->item('controller_trigger')])));
123 $segments[] = $this->fetch_class();
124 }
Barry Mienydd671972010-10-04 16:33:58 +0200125
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 if (isset($_GET[$this->config->item('function_trigger')]))
127 {
128 $this->set_method(trim($this->uri->_filter_uri($_GET[$this->config->item('function_trigger')])));
Derek Jonesc7738402010-03-02 13:55:13 -0600129 $segments[] = $this->fetch_method();
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 }
Barry Mienydd671972010-10-04 16:33:58 +0200132
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 // Load the routes.php file.
Andrey Andreev7b53d042012-03-26 23:02:32 +0300134 if (defined('ENVIRONMENT') && is_file(APPPATH.'config/'.ENVIRONMENT.'/routes.php'))
Greg Akerd96f8822011-12-27 16:23:47 -0600135 {
136 include(APPPATH.'config/'.ENVIRONMENT.'/routes.php');
137 }
138 elseif (is_file(APPPATH.'config/routes.php'))
139 {
140 include(APPPATH.'config/routes.php');
141 }
David Behler07b53422011-08-15 00:25:06 +0200142
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 $this->routes = ( ! isset($route) OR ! is_array($route)) ? array() : $route;
144 unset($route);
Barry Mienydd671972010-10-04 16:33:58 +0200145
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 // Set the default controller so we can display it in the event
147 // the URI doesn't correlated to a valid controller.
Andrey Andreev7b53d042012-03-26 23:02:32 +0300148 $this->default_controller = empty($this->routes['default_controller']) ? FALSE : strtolower($this->routes['default_controller']);
Barry Mienydd671972010-10-04 16:33:58 +0200149
Andrey Andreevba6c0412012-01-07 21:10:09 +0200150 // 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 -0600151 if (count($segments) > 0)
152 {
153 return $this->_validate_request($segments);
154 }
Barry Mienydd671972010-10-04 16:33:58 +0200155
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 // Fetch the complete URI string
157 $this->uri->_fetch_uri_string();
Barry Mienydd671972010-10-04 16:33:58 +0200158
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 // Is there a URI string? If not, the default controller specified in the "routes" file will be shown.
160 if ($this->uri->uri_string == '')
161 {
Derek Jonesc7738402010-03-02 13:55:13 -0600162 return $this->_set_default_controller();
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 }
Barry Mienydd671972010-10-04 16:33:58 +0200164
Andrey Andreevba6c0412012-01-07 21:10:09 +0200165 $this->uri->_remove_url_suffix(); // Remove the URL suffix
166 $this->uri->_explode_segments(); // Compile the segments into an array
167 $this->_parse_routes(); // Parse any custom routing that may exist
168 $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 +0000169 }
Derek Jonesc7738402010-03-02 13:55:13 -0600170
171 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200172
Derek Jonesc7738402010-03-02 13:55:13 -0600173 /**
174 * Set the default controller
175 *
Derek Jonesc7738402010-03-02 13:55:13 -0600176 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200177 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200178 protected function _set_default_controller()
Derek Jonesc7738402010-03-02 13:55:13 -0600179 {
180 if ($this->default_controller === FALSE)
181 {
Andrey Andreevba6c0412012-01-07 21:10:09 +0200182 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 -0600183 }
184 // Is the method being specified?
185 if (strpos($this->default_controller, '/') !== FALSE)
186 {
187 $x = explode('/', $this->default_controller);
Derek Jonesc7738402010-03-02 13:55:13 -0600188 $this->set_class($x[0]);
189 $this->set_method($x[1]);
Pascal Kriete790ebf32010-12-15 10:53:35 -0500190 $this->_set_request($x);
Barry Mienydd671972010-10-04 16:33:58 +0200191 }
Derek Jonesc7738402010-03-02 13:55:13 -0600192 else
193 {
194 $this->set_class($this->default_controller);
195 $this->set_method('index');
196 $this->_set_request(array($this->default_controller, 'index'));
197 }
Barry Mienydd671972010-10-04 16:33:58 +0200198
Derek Jonesc7738402010-03-02 13:55:13 -0600199 // re-index the routed segments array so it starts with 1 rather than 0
200 $this->uri->_reindex_segments();
Barry Mienydd671972010-10-04 16:33:58 +0200201
Andrey Andreevba6c0412012-01-07 21:10:09 +0200202 log_message('debug', 'No URI present. Default controller set.');
Derek Jonesc7738402010-03-02 13:55:13 -0600203 }
Barry Mienydd671972010-10-04 16:33:58 +0200204
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200206
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 /**
208 * Set the Route
209 *
210 * This function takes an array of URI segments as
211 * input, and sets the current class/method
212 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 * @param array
214 * @param bool
215 * @return void
216 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200217 protected function _set_request($segments = array())
Barry Mienydd671972010-10-04 16:33:58 +0200218 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 $segments = $this->_validate_request($segments);
Barry Mienydd671972010-10-04 16:33:58 +0200220
Andrey Andreevba6c0412012-01-07 21:10:09 +0200221 if (count($segments) === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 {
Derek Jonesc7738402010-03-02 13:55:13 -0600223 return $this->_set_default_controller();
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 }
Barry Mienydd671972010-10-04 16:33:58 +0200225
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 $this->set_class($segments[0]);
Barry Mienydd671972010-10-04 16:33:58 +0200227
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 if (isset($segments[1]))
229 {
Derek Jonesc7738402010-03-02 13:55:13 -0600230 // A standard method request
231 $this->set_method($segments[1]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 }
233 else
234 {
235 // This lets the "routed" segment array identify that the default
236 // index method is being used.
237 $segments[1] = 'index';
238 }
Barry Mienydd671972010-10-04 16:33:58 +0200239
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 // Update our "routed" segment array to contain the segments.
241 // Note: If there is no custom routing, this array will be
242 // identical to $this->uri->segments
243 $this->uri->rsegments = $segments;
244 }
Barry Mienydd671972010-10-04 16:33:58 +0200245
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200247
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 /**
Andrey Andreev7b53d042012-03-26 23:02:32 +0300249 * Validates the supplied segments.
250 * Attempts to determine the path to the controller.
Derek Allard2067d1a2008-11-13 22:59:24 +0000251 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 * @param array
253 * @return array
Barry Mienydd671972010-10-04 16:33:58 +0200254 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200255 protected function _validate_request($segments)
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 {
Andrey Andreevba6c0412012-01-07 21:10:09 +0200257 if (count($segments) === 0)
Derek Jonesc7738402010-03-02 13:55:13 -0600258 {
259 return $segments;
260 }
Barry Mienydd671972010-10-04 16:33:58 +0200261
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 // Does the requested controller exist in the root folder?
Greg Aker3a746652011-04-19 10:59:47 -0500263 if (file_exists(APPPATH.'controllers/'.$segments[0].'.php'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 {
265 return $segments;
266 }
Barry Mienydd671972010-10-04 16:33:58 +0200267
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 // Is the controller in a sub-folder?
269 if (is_dir(APPPATH.'controllers/'.$segments[0]))
Derek Jonesc7738402010-03-02 13:55:13 -0600270 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 // Set the directory and remove it from the segment array
272 $this->set_directory($segments[0]);
273 $segments = array_slice($segments, 1);
Barry Mienydd671972010-10-04 16:33:58 +0200274
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 if (count($segments) > 0)
276 {
277 // Does the requested controller exist in the sub-folder?
Greg Aker3a746652011-04-19 10:59:47 -0500278 if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].'.php'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 {
Shane Pearson664a9352011-08-10 16:02:32 -0500280 if ( ! empty($this->routes['404_override']))
281 {
282 $x = explode('/', $this->routes['404_override']);
Shane Pearson664a9352011-08-10 16:02:32 -0500283 $this->set_directory('');
284 $this->set_class($x[0]);
285 $this->set_method(isset($x[1]) ? $x[1] : 'index');
David Behler07b53422011-08-15 00:25:06 +0200286
Shane Pearson664a9352011-08-10 16:02:32 -0500287 return $x;
288 }
289 else
290 {
291 show_404($this->fetch_directory().$segments[0]);
292 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 }
294 }
295 else
296 {
Derek Jonesc7738402010-03-02 13:55:13 -0600297 // Is the method being specified in the route?
298 if (strpos($this->default_controller, '/') !== FALSE)
299 {
300 $x = explode('/', $this->default_controller);
Derek Jonesc7738402010-03-02 13:55:13 -0600301 $this->set_class($x[0]);
302 $this->set_method($x[1]);
Barry Mienydd671972010-10-04 16:33:58 +0200303 }
Derek Jonesc7738402010-03-02 13:55:13 -0600304 else
305 {
306 $this->set_class($this->default_controller);
307 $this->set_method('index');
308 }
Barry Mienydd671972010-10-04 16:33:58 +0200309
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 // Does the default controller exist in the sub-folder?
Greg Aker3a746652011-04-19 10:59:47 -0500311 if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.'.php'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 {
313 $this->directory = '';
314 return array();
315 }
Barry Mienydd671972010-10-04 16:33:58 +0200316
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 }
Barry Mienydd671972010-10-04 16:33:58 +0200318
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 return $segments;
320 }
Barry Mienydd671972010-10-04 16:33:58 +0200321
322
Derek Jonesc7738402010-03-02 13:55:13 -0600323 // If we've gotten this far it means that the URI does not correlate to a valid
Andrey Andreevba6c0412012-01-07 21:10:09 +0200324 // controller class. We will now see if there is an override
Eric Barnesc5bf6162011-01-30 21:17:11 -0500325 if ( ! empty($this->routes['404_override']))
Derek Jonesc7738402010-03-02 13:55:13 -0600326 {
Phil Sturgeon23174a62010-12-15 15:18:16 +0000327 $x = explode('/', $this->routes['404_override']);
Phil Sturgeon23174a62010-12-15 15:18:16 +0000328 $this->set_class($x[0]);
329 $this->set_method(isset($x[1]) ? $x[1] : 'index');
Barry Mienydd671972010-10-04 16:33:58 +0200330
Phil Sturgeon23174a62010-12-15 15:18:16 +0000331 return $x;
Derek Jonesc7738402010-03-02 13:55:13 -0600332 }
Barry Mienydd671972010-10-04 16:33:58 +0200333
Derek Jonesc7738402010-03-02 13:55:13 -0600334 // Nothing else to do at this point but show a 404
Barry Mienydd671972010-10-04 16:33:58 +0200335 show_404($segments[0]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 }
Barry Mienydd671972010-10-04 16:33:58 +0200337
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200339
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 /**
Andrey Andreev7b53d042012-03-26 23:02:32 +0300341 * Parse Routes
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 *
343 * This function matches any routes that may exist in
344 * the config/routes.php file against the URI to
345 * determine if the class/method need to be remapped.
346 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 * @return void
348 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200349 protected function _parse_routes()
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 // Turn the segment array into a URI string
352 $uri = implode('/', $this->uri->segments);
Barry Mienydd671972010-10-04 16:33:58 +0200353
Derek Jones37f4b9c2011-07-01 17:56:50 -0500354 // Is there a literal match? If so we're done
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 if (isset($this->routes[$uri]))
356 {
Derek Jonesc7738402010-03-02 13:55:13 -0600357 return $this->_set_request(explode('/', $this->routes[$uri]));
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 }
Barry Mienydd671972010-10-04 16:33:58 +0200359
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 // Loop through the route array looking for wild-cards
361 foreach ($this->routes as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200362 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 // Convert wild-cards to RegEx
Andrey Andreevba6c0412012-01-07 21:10:09 +0200364 $key = str_replace(array(':any', ':num'), array('.+', '[0-9]+'), $key);
Derek Jonesc7738402010-03-02 13:55:13 -0600365
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 // Does the RegEx match?
367 if (preg_match('#^'.$key.'$#', $uri))
Derek Jonesc7738402010-03-02 13:55:13 -0600368 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 // Do we have a back-reference?
Andrey Andreev7b53d042012-03-26 23:02:32 +0300370 if (strpos($val, '$') !== FALSE && strpos($key, '(') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 {
372 $val = preg_replace('#^'.$key.'$#', $val, $uri);
373 }
Barry Mienydd671972010-10-04 16:33:58 +0200374
375 return $this->_set_request(explode('/', $val));
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 }
377 }
378
379 // If we got this far it means we didn't encounter a
380 // matching route so we'll set the site default route
381 $this->_set_request($this->uri->segments);
382 }
383
384 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200385
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 /**
387 * Set the class name
388 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 * @param string
390 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200391 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200392 public function set_class($class)
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 {
Derek Jones2615e412010-10-06 17:51:16 -0500394 $this->class = str_replace(array('/', '.'), '', $class);
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 }
Barry Mienydd671972010-10-04 16:33:58 +0200396
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200398
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 /**
400 * Fetch the current class
401 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200403 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200404 public function fetch_class()
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 {
406 return $this->class;
407 }
Barry Mienydd671972010-10-04 16:33:58 +0200408
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200410
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 /**
Andrey Andreev7b53d042012-03-26 23:02:32 +0300412 * Set the method name
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 * @param string
415 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200416 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200417 public function set_method($method)
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 {
419 $this->method = $method;
420 }
421
422 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200423
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 /**
Andrey Andreev7b53d042012-03-26 23:02:32 +0300425 * Fetch the current method
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200428 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200429 public function fetch_method()
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 {
431 if ($this->method == $this->fetch_class())
432 {
433 return 'index';
434 }
435
436 return $this->method;
437 }
438
439 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200440
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 /**
Andrey Andreev7b53d042012-03-26 23:02:32 +0300442 * Set the directory name
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 * @param string
445 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200446 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200447 public function set_directory($dir)
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 {
Derek Jones2615e412010-10-06 17:51:16 -0500449 $this->directory = str_replace(array('/', '.'), '', $dir).'/';
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 }
451
452 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200453
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 /**
Andrey Andreev7b53d042012-03-26 23:02:32 +0300455 * Fetch the sub-directory (if any) that contains the requested controller class
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200458 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200459 public function fetch_directory()
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 {
461 return $this->directory;
462 }
463
Derek Jonesc7738402010-03-02 13:55:13 -0600464 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200465
Derek Jonesc7738402010-03-02 13:55:13 -0600466 /**
Andrey Andreev7b53d042012-03-26 23:02:32 +0300467 * Set the controller overrides
Derek Jonesc7738402010-03-02 13:55:13 -0600468 *
Derek Jonesc7738402010-03-02 13:55:13 -0600469 * @param array
Andrey Andreev7b53d042012-03-26 23:02:32 +0300470 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200471 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200472 public function _set_overrides($routing)
Derek Jonesc7738402010-03-02 13:55:13 -0600473 {
474 if ( ! is_array($routing))
475 {
476 return;
477 }
Barry Mienydd671972010-10-04 16:33:58 +0200478
Derek Jonesc7738402010-03-02 13:55:13 -0600479 if (isset($routing['directory']))
480 {
481 $this->set_directory($routing['directory']);
482 }
Barry Mienydd671972010-10-04 16:33:58 +0200483
Andrey Andreev7b53d042012-03-26 23:02:32 +0300484 if (isset($routing['controller']) && $routing['controller'] != '')
Derek Jonesc7738402010-03-02 13:55:13 -0600485 {
486 $this->set_class($routing['controller']);
487 }
Barry Mienydd671972010-10-04 16:33:58 +0200488
Derek Jonesc7738402010-03-02 13:55:13 -0600489 if (isset($routing['function']))
490 {
491 $routing['function'] = ($routing['function'] == '') ? 'index' : $routing['function'];
492 $this->set_method($routing['function']);
493 }
494 }
495
Derek Allard2067d1a2008-11-13 22:59:24 +0000496}
Derek Allard2067d1a2008-11-13 22:59:24 +0000497
498/* End of file Router.php */
Andrey Andreev7b53d042012-03-26 23:02:32 +0300499/* Location: ./system/core/Router.php */