blob: fa88cb3dc0542c67aee0dac159f37a0902b1e736 [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
Phil Sturgeoncdfbd522012-04-26 22:13:31 +0100240 // This is being routed to a file in a sub directory
241 $this->directory and array_unshift($segments, trim($this->directory, '/'));
242
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 // Update our "routed" segment array to contain the segments.
244 // Note: If there is no custom routing, this array will be
Phil Sturgeoncdfbd522012-04-26 22:13:31 +0100245 // identical to $this->uri->segments
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 $this->uri->rsegments = $segments;
247 }
Barry Mienydd671972010-10-04 16:33:58 +0200248
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200250
Derek Allard2067d1a2008-11-13 22:59:24 +0000251 /**
Andrey Andreev7b53d042012-03-26 23:02:32 +0300252 * Validates the supplied segments.
253 * Attempts to determine the path to the controller.
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 * @param array
256 * @return array
Barry Mienydd671972010-10-04 16:33:58 +0200257 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200258 protected function _validate_request($segments)
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 {
Andrey Andreevba6c0412012-01-07 21:10:09 +0200260 if (count($segments) === 0)
Derek Jonesc7738402010-03-02 13:55:13 -0600261 {
262 return $segments;
263 }
Barry Mienydd671972010-10-04 16:33:58 +0200264
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 // Does the requested controller exist in the root folder?
Greg Aker3a746652011-04-19 10:59:47 -0500266 if (file_exists(APPPATH.'controllers/'.$segments[0].'.php'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 {
268 return $segments;
269 }
Barry Mienydd671972010-10-04 16:33:58 +0200270
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 // Is the controller in a sub-folder?
272 if (is_dir(APPPATH.'controllers/'.$segments[0]))
Derek Jonesc7738402010-03-02 13:55:13 -0600273 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 // Set the directory and remove it from the segment array
275 $this->set_directory($segments[0]);
276 $segments = array_slice($segments, 1);
Barry Mienydd671972010-10-04 16:33:58 +0200277
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 if (count($segments) > 0)
279 {
280 // Does the requested controller exist in the sub-folder?
Greg Aker3a746652011-04-19 10:59:47 -0500281 if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].'.php'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 {
Shane Pearson664a9352011-08-10 16:02:32 -0500283 if ( ! empty($this->routes['404_override']))
284 {
285 $x = explode('/', $this->routes['404_override']);
Shane Pearson664a9352011-08-10 16:02:32 -0500286 $this->set_directory('');
287 $this->set_class($x[0]);
288 $this->set_method(isset($x[1]) ? $x[1] : 'index');
David Behler07b53422011-08-15 00:25:06 +0200289
Shane Pearson664a9352011-08-10 16:02:32 -0500290 return $x;
291 }
292 else
293 {
294 show_404($this->fetch_directory().$segments[0]);
295 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 }
297 }
298 else
299 {
Derek Jonesc7738402010-03-02 13:55:13 -0600300 // Is the method being specified in the route?
301 if (strpos($this->default_controller, '/') !== FALSE)
302 {
303 $x = explode('/', $this->default_controller);
Derek Jonesc7738402010-03-02 13:55:13 -0600304 $this->set_class($x[0]);
305 $this->set_method($x[1]);
Barry Mienydd671972010-10-04 16:33:58 +0200306 }
Derek Jonesc7738402010-03-02 13:55:13 -0600307 else
308 {
309 $this->set_class($this->default_controller);
310 $this->set_method('index');
311 }
Barry Mienydd671972010-10-04 16:33:58 +0200312
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 // Does the default controller exist in the sub-folder?
Greg Aker3a746652011-04-19 10:59:47 -0500314 if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.'.php'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 {
316 $this->directory = '';
317 return array();
318 }
Barry Mienydd671972010-10-04 16:33:58 +0200319
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 }
Barry Mienydd671972010-10-04 16:33:58 +0200321
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 return $segments;
323 }
Barry Mienydd671972010-10-04 16:33:58 +0200324
325
Derek Jonesc7738402010-03-02 13:55:13 -0600326 // If we've gotten this far it means that the URI does not correlate to a valid
Andrey Andreevba6c0412012-01-07 21:10:09 +0200327 // controller class. We will now see if there is an override
Eric Barnesc5bf6162011-01-30 21:17:11 -0500328 if ( ! empty($this->routes['404_override']))
Derek Jonesc7738402010-03-02 13:55:13 -0600329 {
Phil Sturgeon23174a62010-12-15 15:18:16 +0000330 $x = explode('/', $this->routes['404_override']);
Phil Sturgeon23174a62010-12-15 15:18:16 +0000331 $this->set_class($x[0]);
332 $this->set_method(isset($x[1]) ? $x[1] : 'index');
Barry Mienydd671972010-10-04 16:33:58 +0200333
Phil Sturgeon23174a62010-12-15 15:18:16 +0000334 return $x;
Derek Jonesc7738402010-03-02 13:55:13 -0600335 }
Barry Mienydd671972010-10-04 16:33:58 +0200336
Derek Jonesc7738402010-03-02 13:55:13 -0600337 // Nothing else to do at this point but show a 404
Barry Mienydd671972010-10-04 16:33:58 +0200338 show_404($segments[0]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 }
Barry Mienydd671972010-10-04 16:33:58 +0200340
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200342
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 /**
Andrey Andreev7b53d042012-03-26 23:02:32 +0300344 * Parse Routes
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 *
346 * This function matches any routes that may exist in
347 * the config/routes.php file against the URI to
348 * determine if the class/method need to be remapped.
349 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 * @return void
351 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200352 protected function _parse_routes()
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 // Turn the segment array into a URI string
355 $uri = implode('/', $this->uri->segments);
Barry Mienydd671972010-10-04 16:33:58 +0200356
Derek Jones37f4b9c2011-07-01 17:56:50 -0500357 // Is there a literal match? If so we're done
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 if (isset($this->routes[$uri]))
359 {
Derek Jonesc7738402010-03-02 13:55:13 -0600360 return $this->_set_request(explode('/', $this->routes[$uri]));
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 }
Barry Mienydd671972010-10-04 16:33:58 +0200362
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 // Loop through the route array looking for wild-cards
364 foreach ($this->routes as $key => $val)
Barry Mienydd671972010-10-04 16:33:58 +0200365 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 // Convert wild-cards to RegEx
Andrey Andreevba6c0412012-01-07 21:10:09 +0200367 $key = str_replace(array(':any', ':num'), array('.+', '[0-9]+'), $key);
Derek Jonesc7738402010-03-02 13:55:13 -0600368
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 // Does the RegEx match?
370 if (preg_match('#^'.$key.'$#', $uri))
Derek Jonesc7738402010-03-02 13:55:13 -0600371 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 // Do we have a back-reference?
Andrey Andreev7b53d042012-03-26 23:02:32 +0300373 if (strpos($val, '$') !== FALSE && strpos($key, '(') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 {
375 $val = preg_replace('#^'.$key.'$#', $val, $uri);
376 }
Barry Mienydd671972010-10-04 16:33:58 +0200377
378 return $this->_set_request(explode('/', $val));
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 }
380 }
381
382 // If we got this far it means we didn't encounter a
383 // matching route so we'll set the site default route
384 $this->_set_request($this->uri->segments);
385 }
386
387 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200388
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 /**
390 * Set the class name
391 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 * @param string
393 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200394 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200395 public function set_class($class)
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 {
Derek Jones2615e412010-10-06 17:51:16 -0500397 $this->class = str_replace(array('/', '.'), '', $class);
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 }
Barry Mienydd671972010-10-04 16:33:58 +0200399
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200401
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 /**
403 * Fetch the current class
404 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200406 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200407 public function fetch_class()
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 {
409 return $this->class;
410 }
Barry Mienydd671972010-10-04 16:33:58 +0200411
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200413
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 /**
Andrey Andreev7b53d042012-03-26 23:02:32 +0300415 * Set the method name
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 * @param string
418 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200419 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200420 public function set_method($method)
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 {
422 $this->method = $method;
423 }
424
425 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200426
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 /**
Andrey Andreev7b53d042012-03-26 23:02:32 +0300428 * Fetch the current method
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200431 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200432 public function fetch_method()
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 {
434 if ($this->method == $this->fetch_class())
435 {
436 return 'index';
437 }
438
439 return $this->method;
440 }
441
442 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200443
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 /**
Andrey Andreev7b53d042012-03-26 23:02:32 +0300445 * Set the directory name
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 * @param string
448 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200449 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200450 public function set_directory($dir)
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 {
Derek Jones2615e412010-10-06 17:51:16 -0500452 $this->directory = str_replace(array('/', '.'), '', $dir).'/';
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 }
454
455 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200456
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 /**
Andrey Andreev7b53d042012-03-26 23:02:32 +0300458 * Fetch the sub-directory (if any) that contains the requested controller class
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200461 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200462 public function fetch_directory()
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 {
464 return $this->directory;
465 }
466
Derek Jonesc7738402010-03-02 13:55:13 -0600467 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200468
Derek Jonesc7738402010-03-02 13:55:13 -0600469 /**
Andrey Andreev7b53d042012-03-26 23:02:32 +0300470 * Set the controller overrides
Derek Jonesc7738402010-03-02 13:55:13 -0600471 *
Derek Jonesc7738402010-03-02 13:55:13 -0600472 * @param array
Andrey Andreev7b53d042012-03-26 23:02:32 +0300473 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200474 */
Andrey Andreevba6c0412012-01-07 21:10:09 +0200475 public function _set_overrides($routing)
Derek Jonesc7738402010-03-02 13:55:13 -0600476 {
477 if ( ! is_array($routing))
478 {
479 return;
480 }
Barry Mienydd671972010-10-04 16:33:58 +0200481
Derek Jonesc7738402010-03-02 13:55:13 -0600482 if (isset($routing['directory']))
483 {
484 $this->set_directory($routing['directory']);
485 }
Barry Mienydd671972010-10-04 16:33:58 +0200486
Andrey Andreev7b53d042012-03-26 23:02:32 +0300487 if (isset($routing['controller']) && $routing['controller'] != '')
Derek Jonesc7738402010-03-02 13:55:13 -0600488 {
489 $this->set_class($routing['controller']);
490 }
Barry Mienydd671972010-10-04 16:33:58 +0200491
Derek Jonesc7738402010-03-02 13:55:13 -0600492 if (isset($routing['function']))
493 {
494 $routing['function'] = ($routing['function'] == '') ? 'index' : $routing['function'];
495 $this->set_method($routing['function']);
496 }
497 }
498
Derek Allard2067d1a2008-11-13 22:59:24 +0000499}
Derek Allard2067d1a2008-11-13 22:59:24 +0000500
501/* End of file Router.php */
Andrey Andreev7b53d042012-03-26 23:02:32 +0300502/* Location: ./system/core/Router.php */