Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Phil Sturgeon | 07c1ac8 | 2012-03-09 17:03:37 +0000 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 7 | * NOTICE OF LICENSE |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 11 | * 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
Greg Aker | 0defe5d | 2012-01-01 18:46:41 -0600 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 1.0 |
| 25 | * @filesource |
| 26 | */ |
| 27 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 28 | /** |
| 29 | * Router Class |
| 30 | * |
| 31 | * Parses URIs and determines routing |
| 32 | * |
| 33 | * @package CodeIgniter |
| 34 | * @subpackage Libraries |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 35 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 36 | * @category Libraries |
| 37 | * @link http://codeigniter.com/user_guide/general/routing.html |
| 38 | */ |
| 39 | class CI_Router { |
| 40 | |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 41 | /** |
| 42 | * Config class |
| 43 | * |
| 44 | * @var object |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 45 | */ |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 46 | public $config; |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 47 | /** |
| 48 | * List of routes |
| 49 | * |
| 50 | * @var array |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 51 | */ |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 52 | public $routes = array(); |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 53 | /** |
| 54 | * List of error routes |
| 55 | * |
| 56 | * @var array |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 57 | */ |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 58 | public $error_routes = array(); |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 59 | /** |
| 60 | * Current class name |
| 61 | * |
| 62 | * @var string |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 63 | */ |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 64 | public $class = ''; |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 65 | /** |
| 66 | * Current method name |
| 67 | * |
| 68 | * @var string |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 69 | */ |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 70 | public $method = 'index'; |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 71 | /** |
| 72 | * Sub-directory that contains the requested controller class |
| 73 | * |
| 74 | * @var string |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 75 | */ |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 76 | public $directory = ''; |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 77 | /** |
| 78 | * Default controller (and method if specific) |
| 79 | * |
| 80 | * @var string |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 81 | */ |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 82 | public $default_controller; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 83 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 84 | /** |
| 85 | * Constructor |
| 86 | * |
| 87 | * Runs the route mapping function. |
| 88 | */ |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 89 | public function __construct() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 90 | { |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 91 | $this->config =& load_class('Config', 'core'); |
| 92 | $this->uri =& load_class('URI', 'core'); |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 93 | log_message('debug', 'Router Class Initialized'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 94 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 95 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 96 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 97 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 98 | /** |
| 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 104 | * @return void |
| 105 | */ |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 106 | public function _set_routing() |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 107 | { |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 108 | // Are query strings enabled in the config file? Normally CI doesn't utilize query strings |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 109 | // since URI segments are more search-engine friendly, but they can optionally be used. |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 110 | // If this feature is enabled, we will gather the directory/class/method a little differently |
| 111 | $segments = array(); |
Andrey Andreev | 7b53d04 | 2012-03-26 23:02:32 +0300 | [diff] [blame^] | 112 | if ($this->config->item('enable_query_strings') === TRUE && isset($_GET[$this->config->item('controller_trigger')])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 113 | { |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 114 | 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 119 | |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 120 | 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 125 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 126 | 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 Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 129 | $segments[] = $this->fetch_method(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 130 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 131 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 132 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 133 | // Load the routes.php file. |
Andrey Andreev | 7b53d04 | 2012-03-26 23:02:32 +0300 | [diff] [blame^] | 134 | if (defined('ENVIRONMENT') && is_file(APPPATH.'config/'.ENVIRONMENT.'/routes.php')) |
Greg Aker | d96f882 | 2011-12-27 16:23:47 -0600 | [diff] [blame] | 135 | { |
| 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 Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 142 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 143 | $this->routes = ( ! isset($route) OR ! is_array($route)) ? array() : $route; |
| 144 | unset($route); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 145 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 146 | // Set the default controller so we can display it in the event |
| 147 | // the URI doesn't correlated to a valid controller. |
Andrey Andreev | 7b53d04 | 2012-03-26 23:02:32 +0300 | [diff] [blame^] | 148 | $this->default_controller = empty($this->routes['default_controller']) ? FALSE : strtolower($this->routes['default_controller']); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 149 | |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 150 | // Were there any query string segments? If so, we'll validate them and bail out since we're done. |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 151 | if (count($segments) > 0) |
| 152 | { |
| 153 | return $this->_validate_request($segments); |
| 154 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 155 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 156 | // Fetch the complete URI string |
| 157 | $this->uri->_fetch_uri_string(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 158 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 159 | // 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 Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 162 | return $this->_set_default_controller(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 163 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 164 | |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 165 | $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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 169 | } |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 170 | |
| 171 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 172 | |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 173 | /** |
| 174 | * Set the default controller |
| 175 | * |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 176 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 177 | */ |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 178 | protected function _set_default_controller() |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 179 | { |
| 180 | if ($this->default_controller === FALSE) |
| 181 | { |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 182 | show_error('Unable to determine what should be displayed. A default route has not been specified in the routing file.'); |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 183 | } |
| 184 | // Is the method being specified? |
| 185 | if (strpos($this->default_controller, '/') !== FALSE) |
| 186 | { |
| 187 | $x = explode('/', $this->default_controller); |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 188 | $this->set_class($x[0]); |
| 189 | $this->set_method($x[1]); |
Pascal Kriete | 790ebf3 | 2010-12-15 10:53:35 -0500 | [diff] [blame] | 190 | $this->_set_request($x); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 191 | } |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 192 | 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 198 | |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 199 | // re-index the routed segments array so it starts with 1 rather than 0 |
| 200 | $this->uri->_reindex_segments(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 201 | |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 202 | log_message('debug', 'No URI present. Default controller set.'); |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 203 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 204 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 205 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 206 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 207 | /** |
| 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 213 | * @param array |
| 214 | * @param bool |
| 215 | * @return void |
| 216 | */ |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 217 | protected function _set_request($segments = array()) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 218 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 219 | $segments = $this->_validate_request($segments); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 220 | |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 221 | if (count($segments) === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 222 | { |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 223 | return $this->_set_default_controller(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 224 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 225 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 226 | $this->set_class($segments[0]); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 227 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 228 | if (isset($segments[1])) |
| 229 | { |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 230 | // A standard method request |
| 231 | $this->set_method($segments[1]); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 232 | } |
| 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 239 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 240 | // 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 245 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 246 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 247 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 248 | /** |
Andrey Andreev | 7b53d04 | 2012-03-26 23:02:32 +0300 | [diff] [blame^] | 249 | * Validates the supplied segments. |
| 250 | * Attempts to determine the path to the controller. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 251 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 252 | * @param array |
| 253 | * @return array |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 254 | */ |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 255 | protected function _validate_request($segments) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 256 | { |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 257 | if (count($segments) === 0) |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 258 | { |
| 259 | return $segments; |
| 260 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 261 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 262 | // Does the requested controller exist in the root folder? |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [diff] [blame] | 263 | if (file_exists(APPPATH.'controllers/'.$segments[0].'.php')) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 264 | { |
| 265 | return $segments; |
| 266 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 267 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 268 | // Is the controller in a sub-folder? |
| 269 | if (is_dir(APPPATH.'controllers/'.$segments[0])) |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 270 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 271 | // Set the directory and remove it from the segment array |
| 272 | $this->set_directory($segments[0]); |
| 273 | $segments = array_slice($segments, 1); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 274 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 275 | if (count($segments) > 0) |
| 276 | { |
| 277 | // Does the requested controller exist in the sub-folder? |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [diff] [blame] | 278 | if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].'.php')) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 279 | { |
Shane Pearson | 664a935 | 2011-08-10 16:02:32 -0500 | [diff] [blame] | 280 | if ( ! empty($this->routes['404_override'])) |
| 281 | { |
| 282 | $x = explode('/', $this->routes['404_override']); |
Shane Pearson | 664a935 | 2011-08-10 16:02:32 -0500 | [diff] [blame] | 283 | $this->set_directory(''); |
| 284 | $this->set_class($x[0]); |
| 285 | $this->set_method(isset($x[1]) ? $x[1] : 'index'); |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 286 | |
Shane Pearson | 664a935 | 2011-08-10 16:02:32 -0500 | [diff] [blame] | 287 | return $x; |
| 288 | } |
| 289 | else |
| 290 | { |
| 291 | show_404($this->fetch_directory().$segments[0]); |
| 292 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | else |
| 296 | { |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 297 | // Is the method being specified in the route? |
| 298 | if (strpos($this->default_controller, '/') !== FALSE) |
| 299 | { |
| 300 | $x = explode('/', $this->default_controller); |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 301 | $this->set_class($x[0]); |
| 302 | $this->set_method($x[1]); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 303 | } |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 304 | else |
| 305 | { |
| 306 | $this->set_class($this->default_controller); |
| 307 | $this->set_method('index'); |
| 308 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 309 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 310 | // Does the default controller exist in the sub-folder? |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [diff] [blame] | 311 | if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.'.php')) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 312 | { |
| 313 | $this->directory = ''; |
| 314 | return array(); |
| 315 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 316 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 317 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 318 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 319 | return $segments; |
| 320 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 321 | |
| 322 | |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 323 | // If we've gotten this far it means that the URI does not correlate to a valid |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 324 | // controller class. We will now see if there is an override |
Eric Barnes | c5bf616 | 2011-01-30 21:17:11 -0500 | [diff] [blame] | 325 | if ( ! empty($this->routes['404_override'])) |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 326 | { |
Phil Sturgeon | 23174a6 | 2010-12-15 15:18:16 +0000 | [diff] [blame] | 327 | $x = explode('/', $this->routes['404_override']); |
Phil Sturgeon | 23174a6 | 2010-12-15 15:18:16 +0000 | [diff] [blame] | 328 | $this->set_class($x[0]); |
| 329 | $this->set_method(isset($x[1]) ? $x[1] : 'index'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 330 | |
Phil Sturgeon | 23174a6 | 2010-12-15 15:18:16 +0000 | [diff] [blame] | 331 | return $x; |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 332 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 333 | |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 334 | // Nothing else to do at this point but show a 404 |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 335 | show_404($segments[0]); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 336 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 337 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 338 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 339 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 340 | /** |
Andrey Andreev | 7b53d04 | 2012-03-26 23:02:32 +0300 | [diff] [blame^] | 341 | * Parse Routes |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 342 | * |
| 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 347 | * @return void |
| 348 | */ |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 349 | protected function _parse_routes() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 350 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 351 | // Turn the segment array into a URI string |
| 352 | $uri = implode('/', $this->uri->segments); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 353 | |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 354 | // Is there a literal match? If so we're done |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 355 | if (isset($this->routes[$uri])) |
| 356 | { |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 357 | return $this->_set_request(explode('/', $this->routes[$uri])); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 358 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 359 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 360 | // Loop through the route array looking for wild-cards |
| 361 | foreach ($this->routes as $key => $val) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 362 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 363 | // Convert wild-cards to RegEx |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 364 | $key = str_replace(array(':any', ':num'), array('.+', '[0-9]+'), $key); |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 365 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 366 | // Does the RegEx match? |
| 367 | if (preg_match('#^'.$key.'$#', $uri)) |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 368 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 369 | // Do we have a back-reference? |
Andrey Andreev | 7b53d04 | 2012-03-26 23:02:32 +0300 | [diff] [blame^] | 370 | if (strpos($val, '$') !== FALSE && strpos($key, '(') !== FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 371 | { |
| 372 | $val = preg_replace('#^'.$key.'$#', $val, $uri); |
| 373 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 374 | |
| 375 | return $this->_set_request(explode('/', $val)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 376 | } |
| 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 385 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 386 | /** |
| 387 | * Set the class name |
| 388 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 389 | * @param string |
| 390 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 391 | */ |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 392 | public function set_class($class) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 393 | { |
Derek Jones | 2615e41 | 2010-10-06 17:51:16 -0500 | [diff] [blame] | 394 | $this->class = str_replace(array('/', '.'), '', $class); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 395 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 396 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 397 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 398 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 399 | /** |
| 400 | * Fetch the current class |
| 401 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 402 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 403 | */ |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 404 | public function fetch_class() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 405 | { |
| 406 | return $this->class; |
| 407 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 408 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 409 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 410 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 411 | /** |
Andrey Andreev | 7b53d04 | 2012-03-26 23:02:32 +0300 | [diff] [blame^] | 412 | * Set the method name |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 413 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 414 | * @param string |
| 415 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 416 | */ |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 417 | public function set_method($method) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 418 | { |
| 419 | $this->method = $method; |
| 420 | } |
| 421 | |
| 422 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 423 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 424 | /** |
Andrey Andreev | 7b53d04 | 2012-03-26 23:02:32 +0300 | [diff] [blame^] | 425 | * Fetch the current method |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 426 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 427 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 428 | */ |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 429 | public function fetch_method() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 430 | { |
| 431 | if ($this->method == $this->fetch_class()) |
| 432 | { |
| 433 | return 'index'; |
| 434 | } |
| 435 | |
| 436 | return $this->method; |
| 437 | } |
| 438 | |
| 439 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 440 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 441 | /** |
Andrey Andreev | 7b53d04 | 2012-03-26 23:02:32 +0300 | [diff] [blame^] | 442 | * Set the directory name |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 443 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 444 | * @param string |
| 445 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 446 | */ |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 447 | public function set_directory($dir) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 448 | { |
Derek Jones | 2615e41 | 2010-10-06 17:51:16 -0500 | [diff] [blame] | 449 | $this->directory = str_replace(array('/', '.'), '', $dir).'/'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 453 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 454 | /** |
Andrey Andreev | 7b53d04 | 2012-03-26 23:02:32 +0300 | [diff] [blame^] | 455 | * Fetch the sub-directory (if any) that contains the requested controller class |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 456 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 457 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 458 | */ |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 459 | public function fetch_directory() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 460 | { |
| 461 | return $this->directory; |
| 462 | } |
| 463 | |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 464 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 465 | |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 466 | /** |
Andrey Andreev | 7b53d04 | 2012-03-26 23:02:32 +0300 | [diff] [blame^] | 467 | * Set the controller overrides |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 468 | * |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 469 | * @param array |
Andrey Andreev | 7b53d04 | 2012-03-26 23:02:32 +0300 | [diff] [blame^] | 470 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 471 | */ |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 472 | public function _set_overrides($routing) |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 473 | { |
| 474 | if ( ! is_array($routing)) |
| 475 | { |
| 476 | return; |
| 477 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 478 | |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 479 | if (isset($routing['directory'])) |
| 480 | { |
| 481 | $this->set_directory($routing['directory']); |
| 482 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 483 | |
Andrey Andreev | 7b53d04 | 2012-03-26 23:02:32 +0300 | [diff] [blame^] | 484 | if (isset($routing['controller']) && $routing['controller'] != '') |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 485 | { |
| 486 | $this->set_class($routing['controller']); |
| 487 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 488 | |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 489 | if (isset($routing['function'])) |
| 490 | { |
| 491 | $routing['function'] = ($routing['function'] == '') ? 'index' : $routing['function']; |
| 492 | $this->set_method($routing['function']); |
| 493 | } |
| 494 | } |
| 495 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 496 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 497 | |
| 498 | /* End of file Router.php */ |
Andrey Andreev | 7b53d04 | 2012-03-26 23:02:32 +0300 | [diff] [blame^] | 499 | /* Location: ./system/core/Router.php */ |