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; |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame] | 47 | |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 48 | /** |
| 49 | * List of routes |
| 50 | * |
| 51 | * @var array |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 52 | */ |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame] | 53 | public $routes = array(); |
| 54 | |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 55 | /** |
| 56 | * List of error routes |
| 57 | * |
| 58 | * @var array |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 59 | */ |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame] | 60 | public $error_routes = array(); |
| 61 | |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 62 | /** |
| 63 | * Current class name |
| 64 | * |
| 65 | * @var string |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 66 | */ |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame] | 67 | public $class = ''; |
| 68 | |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 69 | /** |
| 70 | * Current method name |
| 71 | * |
| 72 | * @var string |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 73 | */ |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame] | 74 | public $method = 'index'; |
| 75 | |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 76 | /** |
| 77 | * Sub-directory that contains the requested controller class |
| 78 | * |
| 79 | * @var string |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 80 | */ |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame] | 81 | public $directory = ''; |
| 82 | |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 83 | /** |
| 84 | * Default controller (and method if specific) |
| 85 | * |
| 86 | * @var string |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 87 | */ |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 88 | public $default_controller; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 89 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 90 | /** |
| 91 | * Constructor |
| 92 | * |
| 93 | * Runs the route mapping function. |
| 94 | */ |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 95 | public function __construct() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 96 | { |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 97 | $this->config =& load_class('Config', 'core'); |
| 98 | $this->uri =& load_class('URI', 'core'); |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 99 | log_message('debug', 'Router Class Initialized'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 100 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 101 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 102 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 103 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 104 | /** |
| 105 | * Set the route mapping |
| 106 | * |
| 107 | * This function determines what should be served based on the URI request, |
| 108 | * as well as any "routes" that have been set in the routing config file. |
| 109 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 110 | * @return void |
| 111 | */ |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 112 | public function _set_routing() |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 113 | { |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 114 | // 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] | 115 | // 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] | 116 | // If this feature is enabled, we will gather the directory/class/method a little differently |
| 117 | $segments = array(); |
Andrey Andreev | 7b53d04 | 2012-03-26 23:02:32 +0300 | [diff] [blame] | 118 | 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] | 119 | { |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 120 | if (isset($_GET[$this->config->item('directory_trigger')])) |
| 121 | { |
| 122 | $this->set_directory(trim($this->uri->_filter_uri($_GET[$this->config->item('directory_trigger')]))); |
| 123 | $segments[] = $this->fetch_directory(); |
| 124 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 125 | |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 126 | if (isset($_GET[$this->config->item('controller_trigger')])) |
| 127 | { |
| 128 | $this->set_class(trim($this->uri->_filter_uri($_GET[$this->config->item('controller_trigger')]))); |
| 129 | $segments[] = $this->fetch_class(); |
| 130 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 131 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 132 | if (isset($_GET[$this->config->item('function_trigger')])) |
| 133 | { |
| 134 | $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] | 135 | $segments[] = $this->fetch_method(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 136 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 137 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 138 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 139 | // Load the routes.php file. |
Andrey Andreev | 7b53d04 | 2012-03-26 23:02:32 +0300 | [diff] [blame] | 140 | if (defined('ENVIRONMENT') && is_file(APPPATH.'config/'.ENVIRONMENT.'/routes.php')) |
Greg Aker | d96f882 | 2011-12-27 16:23:47 -0600 | [diff] [blame] | 141 | { |
| 142 | include(APPPATH.'config/'.ENVIRONMENT.'/routes.php'); |
| 143 | } |
| 144 | elseif (is_file(APPPATH.'config/routes.php')) |
| 145 | { |
| 146 | include(APPPATH.'config/routes.php'); |
| 147 | } |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 148 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 149 | $this->routes = ( ! isset($route) OR ! is_array($route)) ? array() : $route; |
| 150 | unset($route); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 151 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 152 | // Set the default controller so we can display it in the event |
| 153 | // the URI doesn't correlated to a valid controller. |
Andrey Andreev | 7b53d04 | 2012-03-26 23:02:32 +0300 | [diff] [blame] | 154 | $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] | 155 | |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 156 | // 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] | 157 | if (count($segments) > 0) |
| 158 | { |
| 159 | return $this->_validate_request($segments); |
| 160 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 161 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 162 | // Fetch the complete URI string |
| 163 | $this->uri->_fetch_uri_string(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 164 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 165 | // Is there a URI string? If not, the default controller specified in the "routes" file will be shown. |
| 166 | if ($this->uri->uri_string == '') |
| 167 | { |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 168 | return $this->_set_default_controller(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 169 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 170 | |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 171 | $this->uri->_remove_url_suffix(); // Remove the URL suffix |
| 172 | $this->uri->_explode_segments(); // Compile the segments into an array |
| 173 | $this->_parse_routes(); // Parse any custom routing that may exist |
| 174 | $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] | 175 | } |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 176 | |
| 177 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 178 | |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 179 | /** |
| 180 | * Set the default controller |
| 181 | * |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 182 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 183 | */ |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 184 | protected function _set_default_controller() |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 185 | { |
| 186 | if ($this->default_controller === FALSE) |
| 187 | { |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 188 | 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] | 189 | } |
| 190 | // Is the method being specified? |
| 191 | if (strpos($this->default_controller, '/') !== FALSE) |
| 192 | { |
| 193 | $x = explode('/', $this->default_controller); |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 194 | $this->set_class($x[0]); |
| 195 | $this->set_method($x[1]); |
Pascal Kriete | 790ebf3 | 2010-12-15 10:53:35 -0500 | [diff] [blame] | 196 | $this->_set_request($x); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 197 | } |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 198 | else |
| 199 | { |
| 200 | $this->set_class($this->default_controller); |
| 201 | $this->set_method('index'); |
| 202 | $this->_set_request(array($this->default_controller, 'index')); |
| 203 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 204 | |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 205 | // re-index the routed segments array so it starts with 1 rather than 0 |
| 206 | $this->uri->_reindex_segments(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 207 | |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 208 | log_message('debug', 'No URI present. Default controller set.'); |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 209 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 210 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 211 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 212 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 213 | /** |
| 214 | * Set the Route |
| 215 | * |
| 216 | * This function takes an array of URI segments as |
| 217 | * input, and sets the current class/method |
| 218 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 219 | * @param array |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 220 | * @return void |
| 221 | */ |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 222 | protected function _set_request($segments = array()) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 223 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 224 | $segments = $this->_validate_request($segments); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 225 | |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 226 | if (count($segments) === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 227 | { |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 228 | return $this->_set_default_controller(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 229 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 230 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 231 | $this->set_class($segments[0]); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 232 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 233 | if (isset($segments[1])) |
| 234 | { |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 235 | // A standard method request |
| 236 | $this->set_method($segments[1]); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 237 | } |
| 238 | else |
| 239 | { |
| 240 | // This lets the "routed" segment array identify that the default |
| 241 | // index method is being used. |
| 242 | $segments[1] = 'index'; |
| 243 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 244 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 245 | // Update our "routed" segment array to contain the segments. |
| 246 | // Note: If there is no custom routing, this array will be |
| 247 | // identical to $this->uri->segments |
| 248 | $this->uri->rsegments = $segments; |
| 249 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 250 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 251 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 252 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 253 | /** |
Andrey Andreev | 7b53d04 | 2012-03-26 23:02:32 +0300 | [diff] [blame] | 254 | * Validates the supplied segments. |
| 255 | * Attempts to determine the path to the controller. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 256 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 257 | * @param array |
| 258 | * @return array |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 259 | */ |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 260 | protected function _validate_request($segments) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 261 | { |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 262 | if (count($segments) === 0) |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 263 | { |
| 264 | return $segments; |
| 265 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 266 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 267 | // Does the requested controller exist in the root folder? |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [diff] [blame] | 268 | if (file_exists(APPPATH.'controllers/'.$segments[0].'.php')) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 269 | { |
| 270 | return $segments; |
| 271 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 272 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 273 | // Is the controller in a sub-folder? |
| 274 | if (is_dir(APPPATH.'controllers/'.$segments[0])) |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 275 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 276 | // Set the directory and remove it from the segment array |
| 277 | $this->set_directory($segments[0]); |
| 278 | $segments = array_slice($segments, 1); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 279 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 280 | if (count($segments) > 0) |
| 281 | { |
| 282 | // Does the requested controller exist in the sub-folder? |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [diff] [blame] | 283 | if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].'.php')) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 284 | { |
Shane Pearson | 664a935 | 2011-08-10 16:02:32 -0500 | [diff] [blame] | 285 | if ( ! empty($this->routes['404_override'])) |
| 286 | { |
| 287 | $x = explode('/', $this->routes['404_override']); |
Shane Pearson | 664a935 | 2011-08-10 16:02:32 -0500 | [diff] [blame] | 288 | $this->set_directory(''); |
| 289 | $this->set_class($x[0]); |
| 290 | $this->set_method(isset($x[1]) ? $x[1] : 'index'); |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 291 | |
Shane Pearson | 664a935 | 2011-08-10 16:02:32 -0500 | [diff] [blame] | 292 | return $x; |
| 293 | } |
| 294 | else |
| 295 | { |
| 296 | show_404($this->fetch_directory().$segments[0]); |
| 297 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | else |
| 301 | { |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 302 | // Is the method being specified in the route? |
| 303 | if (strpos($this->default_controller, '/') !== FALSE) |
| 304 | { |
| 305 | $x = explode('/', $this->default_controller); |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 306 | $this->set_class($x[0]); |
| 307 | $this->set_method($x[1]); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 308 | } |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 309 | else |
| 310 | { |
| 311 | $this->set_class($this->default_controller); |
| 312 | $this->set_method('index'); |
| 313 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 314 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 315 | // Does the default controller exist in the sub-folder? |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [diff] [blame] | 316 | if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.'.php')) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 317 | { |
| 318 | $this->directory = ''; |
| 319 | return array(); |
| 320 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 321 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 322 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 323 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 324 | return $segments; |
| 325 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 326 | |
| 327 | |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 328 | // 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] | 329 | // controller class. We will now see if there is an override |
Eric Barnes | c5bf616 | 2011-01-30 21:17:11 -0500 | [diff] [blame] | 330 | if ( ! empty($this->routes['404_override'])) |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 331 | { |
Phil Sturgeon | 23174a6 | 2010-12-15 15:18:16 +0000 | [diff] [blame] | 332 | $x = explode('/', $this->routes['404_override']); |
Phil Sturgeon | 23174a6 | 2010-12-15 15:18:16 +0000 | [diff] [blame] | 333 | $this->set_class($x[0]); |
| 334 | $this->set_method(isset($x[1]) ? $x[1] : 'index'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 335 | |
Phil Sturgeon | 23174a6 | 2010-12-15 15:18:16 +0000 | [diff] [blame] | 336 | return $x; |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 337 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 338 | |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 339 | // Nothing else to do at this point but show a 404 |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 340 | show_404($segments[0]); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 341 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 342 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 343 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 344 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 345 | /** |
Andrey Andreev | 7b53d04 | 2012-03-26 23:02:32 +0300 | [diff] [blame] | 346 | * Parse Routes |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 347 | * |
| 348 | * This function matches any routes that may exist in |
| 349 | * the config/routes.php file against the URI to |
| 350 | * determine if the class/method need to be remapped. |
| 351 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 352 | * @return void |
| 353 | */ |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 354 | protected function _parse_routes() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 355 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 356 | // Turn the segment array into a URI string |
| 357 | $uri = implode('/', $this->uri->segments); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 358 | |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 359 | // Is there a literal match? If so we're done |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 360 | if (isset($this->routes[$uri])) |
| 361 | { |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 362 | return $this->_set_request(explode('/', $this->routes[$uri])); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 363 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 364 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 365 | // Loop through the route array looking for wild-cards |
| 366 | foreach ($this->routes as $key => $val) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 367 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 368 | // Convert wild-cards to RegEx |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 369 | $key = str_replace(array(':any', ':num'), array('.+', '[0-9]+'), $key); |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 370 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 371 | // Does the RegEx match? |
| 372 | if (preg_match('#^'.$key.'$#', $uri)) |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 373 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 374 | // Do we have a back-reference? |
Andrey Andreev | 7b53d04 | 2012-03-26 23:02:32 +0300 | [diff] [blame] | 375 | if (strpos($val, '$') !== FALSE && strpos($key, '(') !== FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 376 | { |
| 377 | $val = preg_replace('#^'.$key.'$#', $val, $uri); |
| 378 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 379 | |
| 380 | return $this->_set_request(explode('/', $val)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 381 | } |
| 382 | } |
| 383 | |
| 384 | // If we got this far it means we didn't encounter a |
| 385 | // matching route so we'll set the site default route |
| 386 | $this->_set_request($this->uri->segments); |
| 387 | } |
| 388 | |
| 389 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 390 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 391 | /** |
| 392 | * Set the class name |
| 393 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 394 | * @param string |
| 395 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 396 | */ |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 397 | public function set_class($class) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 398 | { |
Derek Jones | 2615e41 | 2010-10-06 17:51:16 -0500 | [diff] [blame] | 399 | $this->class = str_replace(array('/', '.'), '', $class); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 400 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 401 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 402 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 403 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 404 | /** |
| 405 | * Fetch the current class |
| 406 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 407 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 408 | */ |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 409 | public function fetch_class() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 410 | { |
| 411 | return $this->class; |
| 412 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 413 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 414 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 415 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 416 | /** |
Andrey Andreev | 7b53d04 | 2012-03-26 23:02:32 +0300 | [diff] [blame] | 417 | * Set the method name |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 418 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 419 | * @param string |
| 420 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 421 | */ |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 422 | public function set_method($method) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 423 | { |
| 424 | $this->method = $method; |
| 425 | } |
| 426 | |
| 427 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 428 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 429 | /** |
Andrey Andreev | 7b53d04 | 2012-03-26 23:02:32 +0300 | [diff] [blame] | 430 | * Fetch the current method |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 431 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 432 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 433 | */ |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 434 | public function fetch_method() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 435 | { |
| 436 | if ($this->method == $this->fetch_class()) |
| 437 | { |
| 438 | return 'index'; |
| 439 | } |
| 440 | |
| 441 | return $this->method; |
| 442 | } |
| 443 | |
| 444 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 445 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 446 | /** |
Andrey Andreev | 7b53d04 | 2012-03-26 23:02:32 +0300 | [diff] [blame] | 447 | * Set the directory name |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 448 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 449 | * @param string |
| 450 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 451 | */ |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 452 | public function set_directory($dir) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 453 | { |
Derek Jones | 2615e41 | 2010-10-06 17:51:16 -0500 | [diff] [blame] | 454 | $this->directory = str_replace(array('/', '.'), '', $dir).'/'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 458 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 459 | /** |
Andrey Andreev | 7b53d04 | 2012-03-26 23:02:32 +0300 | [diff] [blame] | 460 | * Fetch the sub-directory (if any) that contains the requested controller class |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 461 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 462 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 463 | */ |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 464 | public function fetch_directory() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 465 | { |
| 466 | return $this->directory; |
| 467 | } |
| 468 | |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 469 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 470 | |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 471 | /** |
Andrey Andreev | 7b53d04 | 2012-03-26 23:02:32 +0300 | [diff] [blame] | 472 | * Set the controller overrides |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 473 | * |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 474 | * @param array |
Andrey Andreev | 7b53d04 | 2012-03-26 23:02:32 +0300 | [diff] [blame] | 475 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 476 | */ |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 477 | public function _set_overrides($routing) |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 478 | { |
| 479 | if ( ! is_array($routing)) |
| 480 | { |
| 481 | return; |
| 482 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 483 | |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 484 | if (isset($routing['directory'])) |
| 485 | { |
| 486 | $this->set_directory($routing['directory']); |
| 487 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 488 | |
Andrey Andreev | 7b53d04 | 2012-03-26 23:02:32 +0300 | [diff] [blame] | 489 | if (isset($routing['controller']) && $routing['controller'] != '') |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 490 | { |
| 491 | $this->set_class($routing['controller']); |
| 492 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 493 | |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 494 | if (isset($routing['function'])) |
| 495 | { |
| 496 | $routing['function'] = ($routing['function'] == '') ? 'index' : $routing['function']; |
| 497 | $this->set_method($routing['function']); |
| 498 | } |
| 499 | } |
| 500 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 501 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 502 | |
| 503 | /* End of file Router.php */ |
Andrey Andreev | 7b53d04 | 2012-03-26 23:02:32 +0300 | [diff] [blame] | 504 | /* Location: ./system/core/Router.php */ |