1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
\r
5 * An open source application development framework for PHP 4.3.2 or newer
\r
7 * @package CodeIgniter
\r
8 * @author ExpressionEngine Dev Team
\r
9 * @copyright Copyright (c) 2008, EllisLab, Inc.
\r
10 * @license http://codeigniter.com/user_guide/license.html
\r
11 * @link http://codeigniter.com
\r
12 * @since Version 1.0
\r
16 // ------------------------------------------------------------------------
\r
21 * Parses URIs and determines routing
\r
23 * @package CodeIgniter
\r
24 * @subpackage Libraries
\r
25 * @author ExpressionEngine Dev Team
\r
26 * @category Libraries
\r
27 * @link http://codeigniter.com/user_guide/general/routing.html
\r
32 var $routes = array();
\r
33 var $error_routes = array();
\r
35 var $method = 'index';
\r
36 var $directory = '';
\r
37 var $uri_protocol = 'auto';
\r
38 var $default_controller;
\r
39 var $scaffolding_request = FALSE; // Must be set to FALSE
\r
44 * Runs the route mapping function.
\r
46 function CI_Router()
\r
48 $this->config =& load_class('Config');
\r
49 $this->uri =& load_class('URI');
\r
50 $this->_set_routing();
\r
51 log_message('debug', "Router Class Initialized");
\r
54 // --------------------------------------------------------------------
\r
57 * Set the route mapping
\r
59 * This function determines what should be served based on the URI request,
\r
60 * as well as any "routes" that have been set in the routing config file.
\r
65 function _set_routing()
\r
67 // Are query strings enabled in the config file?
\r
68 // If so, we're done since segment based URIs are not used with query strings.
\r
69 if ($this->config->item('enable_query_strings') === TRUE AND isset($_GET[$this->config->item('controller_trigger')]))
\r
71 $this->set_class(trim($this->uri->_filter_uri($_GET[$this->config->item('controller_trigger')])));
\r
73 if (isset($_GET[$this->config->item('function_trigger')]))
\r
75 $this->set_method(trim($this->uri->_filter_uri($_GET[$this->config->item('function_trigger')])));
\r
81 // Load the routes.php file.
\r
82 @include(APPPATH.'config/routes'.EXT);
\r
83 $this->routes = ( ! isset($route) OR ! is_array($route)) ? array() : $route;
\r
86 // Set the default controller so we can display it in the event
\r
87 // the URI doesn't correlated to a valid controller.
\r
88 $this->default_controller = ( ! isset($this->routes['default_controller']) OR $this->routes['default_controller'] == '') ? FALSE : strtolower($this->routes['default_controller']);
\r
90 // Fetch the complete URI string
\r
91 $this->uri->_fetch_uri_string();
\r
93 // Is there a URI string? If not, the default controller specified in the "routes" file will be shown.
\r
94 if ($this->uri->uri_string == '')
\r
96 if ($this->default_controller === FALSE)
\r
98 show_error("Unable to determine what should be displayed. A default route has not been specified in the routing file.");
\r
101 // Turn the default route into an array. We explode it in the event that
\r
102 // the controller is located in a subfolder
\r
103 $segments = $this->_validate_request(explode('/', $this->default_controller));
\r
105 // Set the class and method
\r
106 $this->set_class($segments[0]);
\r
107 $this->set_method('index');
\r
109 // Assign the segments to the URI class
\r
110 $this->uri->rsegments = $segments;
\r
112 // re-index the routed segments array so it starts with 1 rather than 0
\r
113 $this->uri->_reindex_segments();
\r
115 log_message('debug', "No URI present. Default controller set.");
\r
118 unset($this->routes['default_controller']);
\r
120 // Do we need to remove the URL suffix?
\r
121 $this->uri->_remove_url_suffix();
\r
123 // Compile the segments into an array
\r
124 $this->uri->_explode_segments();
\r
126 // Parse any custom routing that may exist
\r
127 $this->_parse_routes();
\r
129 // Re-index the segment array so that it starts with 1 rather than 0
\r
130 $this->uri->_reindex_segments();
\r
133 // --------------------------------------------------------------------
\r
138 * This function takes an array of URI segments as
\r
139 * input, and sets the current class/method
\r
146 function _set_request($segments = array())
\r
148 $segments = $this->_validate_request($segments);
\r
150 if (count($segments) == 0)
\r
155 $this->set_class($segments[0]);
\r
157 if (isset($segments[1]))
\r
159 // A scaffolding request. No funny business with the URL
\r
160 if ($this->routes['scaffolding_trigger'] == $segments[1] AND $segments[1] != '_ci_scaffolding')
\r
162 $this->scaffolding_request = TRUE;
\r
163 unset($this->routes['scaffolding_trigger']);
\r
167 // A standard method request
\r
168 $this->set_method($segments[1]);
\r
173 // This lets the "routed" segment array identify that the default
\r
174 // index method is being used.
\r
175 $segments[1] = 'index';
\r
178 // Update our "routed" segment array to contain the segments.
\r
179 // Note: If there is no custom routing, this array will be
\r
180 // identical to $this->uri->segments
\r
181 $this->uri->rsegments = $segments;
\r
184 // --------------------------------------------------------------------
\r
187 * Validates the supplied segments. Attempts to determine the path to
\r
194 function _validate_request($segments)
\r
196 // Does the requested controller exist in the root folder?
\r
197 if (file_exists(APPPATH.'controllers/'.$segments[0].EXT))
\r
202 // Is the controller in a sub-folder?
\r
203 if (is_dir(APPPATH.'controllers/'.$segments[0]))
\r
205 // Set the directory and remove it from the segment array
\r
206 $this->set_directory($segments[0]);
\r
207 $segments = array_slice($segments, 1);
\r
209 if (count($segments) > 0)
\r
211 // Does the requested controller exist in the sub-folder?
\r
212 if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].EXT))
\r
214 show_404($this->fetch_directory().$segments[0]);
\r
219 $this->set_class($this->default_controller);
\r
220 $this->set_method('index');
\r
222 // Does the default controller exist in the sub-folder?
\r
223 if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT))
\r
225 $this->directory = '';
\r
234 // Can't find the requested controller...
\r
235 show_404($segments[0]);
\r
238 // --------------------------------------------------------------------
\r
243 * This function matches any routes that may exist in
\r
244 * the config/routes.php file against the URI to
\r
245 * determine if the class/method need to be remapped.
\r
250 function _parse_routes()
\r
252 // Do we even have any custom routing to deal with?
\r
253 // There is a default scaffolding trigger, so we'll look just for 1
\r
254 if (count($this->routes) == 1)
\r
256 $this->_set_request($this->uri->segments);
\r
260 // Turn the segment array into a URI string
\r
261 $uri = implode('/', $this->uri->segments);
\r
263 // Is there a literal match? If so we're done
\r
264 if (isset($this->routes[$uri]))
\r
266 $this->_set_request(explode('/', $this->routes[$uri]));
\r
270 // Loop through the route array looking for wild-cards
\r
271 foreach ($this->routes as $key => $val)
\r
273 // Convert wild-cards to RegEx
\r
274 $key = str_replace(':any', '.+', str_replace(':num', '[0-9]+', $key));
\r
276 // Does the RegEx match?
\r
277 if (preg_match('#^'.$key.'$#', $uri))
\r
279 // Do we have a back-reference?
\r
280 if (strpos($val, '$') !== FALSE AND strpos($key, '(') !== FALSE)
\r
282 $val = preg_replace('#^'.$key.'$#', $val, $uri);
\r
285 $this->_set_request(explode('/', $val));
\r
290 // If we got this far it means we didn't encounter a
\r
291 // matching route so we'll set the site default route
\r
292 $this->_set_request($this->uri->segments);
\r
295 // --------------------------------------------------------------------
\r
298 * Set the class name
\r
304 function set_class($class)
\r
306 $this->class = $class;
\r
309 // --------------------------------------------------------------------
\r
312 * Fetch the current class
\r
317 function fetch_class()
\r
319 return $this->class;
\r
322 // --------------------------------------------------------------------
\r
325 * Set the method name
\r
331 function set_method($method)
\r
333 $this->method = $method;
\r
336 // --------------------------------------------------------------------
\r
339 * Fetch the current method
\r
344 function fetch_method()
\r
346 if ($this->method == $this->fetch_class())
\r
351 return $this->method;
\r
354 // --------------------------------------------------------------------
\r
357 * Set the directory name
\r
363 function set_directory($dir)
\r
365 $this->directory = $dir.'/';
\r
368 // --------------------------------------------------------------------
\r
371 * Fetch the sub-directory (if any) that contains the requested controller class
\r
376 function fetch_directory()
\r
378 return $this->directory;
\r
382 // END Router Class
\r
384 /* End of file Router.php */
\r
385 /* Location: ./system/libraries/Router.php */