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 | 48a7fbb | 2012-04-23 11:58:16 -0400 | [diff] [blame] | 53 | public $routes = array(); |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame] | 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 | 48a7fbb | 2012-04-23 11:58:16 -0400 | [diff] [blame] | 60 | public $error_routes = array(); |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame] | 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 | 48a7fbb | 2012-04-23 11:58:16 -0400 | [diff] [blame] | 67 | public $class = ''; |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame] | 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 | 48a7fbb | 2012-04-23 11:58:16 -0400 | [diff] [blame] | 74 | public $method = 'index'; |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame] | 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 | 48a7fbb | 2012-04-23 11:58:16 -0400 | [diff] [blame] | 81 | public $directory = ''; |
Timothy Warren | 40403d2 | 2012-04-19 16:38:50 -0400 | [diff] [blame] | 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 | |
Phil Sturgeon | cdfbd52 | 2012-04-26 22:13:31 +0100 | [diff] [blame] | 245 | // This is being routed to a file in a sub directory |
| 246 | $this->directory and array_unshift($segments, trim($this->directory, '/')); |
| 247 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 248 | // Update our "routed" segment array to contain the segments. |
| 249 | // Note: If there is no custom routing, this array will be |
Phil Sturgeon | cdfbd52 | 2012-04-26 22:13:31 +0100 | [diff] [blame] | 250 | // identical to $this->uri->segments |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 251 | $this->uri->rsegments = $segments; |
| 252 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 253 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 254 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 255 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 256 | /** |
Andrey Andreev | 7b53d04 | 2012-03-26 23:02:32 +0300 | [diff] [blame] | 257 | * Validates the supplied segments. |
| 258 | * Attempts to determine the path to the controller. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 259 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 260 | * @param array |
| 261 | * @return array |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 262 | */ |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 263 | protected function _validate_request($segments) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 264 | { |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 265 | if (count($segments) === 0) |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 266 | { |
| 267 | return $segments; |
| 268 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 269 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 270 | // Does the requested controller exist in the root folder? |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [diff] [blame] | 271 | if (file_exists(APPPATH.'controllers/'.$segments[0].'.php')) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 272 | { |
| 273 | return $segments; |
| 274 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 275 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 276 | // Is the controller in a sub-folder? |
| 277 | if (is_dir(APPPATH.'controllers/'.$segments[0])) |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 278 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 279 | // Set the directory and remove it from the segment array |
| 280 | $this->set_directory($segments[0]); |
| 281 | $segments = array_slice($segments, 1); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 282 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 283 | if (count($segments) > 0) |
| 284 | { |
| 285 | // Does the requested controller exist in the sub-folder? |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [diff] [blame] | 286 | if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].'.php')) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 287 | { |
Shane Pearson | 664a935 | 2011-08-10 16:02:32 -0500 | [diff] [blame] | 288 | if ( ! empty($this->routes['404_override'])) |
| 289 | { |
| 290 | $x = explode('/', $this->routes['404_override']); |
Shane Pearson | 664a935 | 2011-08-10 16:02:32 -0500 | [diff] [blame] | 291 | $this->set_directory(''); |
| 292 | $this->set_class($x[0]); |
| 293 | $this->set_method(isset($x[1]) ? $x[1] : 'index'); |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 294 | |
Shane Pearson | 664a935 | 2011-08-10 16:02:32 -0500 | [diff] [blame] | 295 | return $x; |
| 296 | } |
| 297 | else |
| 298 | { |
| 299 | show_404($this->fetch_directory().$segments[0]); |
| 300 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 301 | } |
| 302 | } |
| 303 | else |
| 304 | { |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 305 | // Is the method being specified in the route? |
| 306 | if (strpos($this->default_controller, '/') !== FALSE) |
| 307 | { |
| 308 | $x = explode('/', $this->default_controller); |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 309 | $this->set_class($x[0]); |
| 310 | $this->set_method($x[1]); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 311 | } |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 312 | else |
| 313 | { |
| 314 | $this->set_class($this->default_controller); |
| 315 | $this->set_method('index'); |
| 316 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 317 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 318 | // Does the default controller exist in the sub-folder? |
Greg Aker | 3a74665 | 2011-04-19 10:59:47 -0500 | [diff] [blame] | 319 | if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.'.php')) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 320 | { |
| 321 | $this->directory = ''; |
| 322 | return array(); |
| 323 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 324 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 325 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 326 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 327 | return $segments; |
| 328 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 329 | |
| 330 | |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 331 | // 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] | 332 | // controller class. We will now see if there is an override |
Eric Barnes | c5bf616 | 2011-01-30 21:17:11 -0500 | [diff] [blame] | 333 | if ( ! empty($this->routes['404_override'])) |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 334 | { |
Phil Sturgeon | 23174a6 | 2010-12-15 15:18:16 +0000 | [diff] [blame] | 335 | $x = explode('/', $this->routes['404_override']); |
Phil Sturgeon | 23174a6 | 2010-12-15 15:18:16 +0000 | [diff] [blame] | 336 | $this->set_class($x[0]); |
| 337 | $this->set_method(isset($x[1]) ? $x[1] : 'index'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 338 | |
Phil Sturgeon | 23174a6 | 2010-12-15 15:18:16 +0000 | [diff] [blame] | 339 | return $x; |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 340 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 341 | |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 342 | // Nothing else to do at this point but show a 404 |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 343 | show_404($segments[0]); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 344 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 345 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 346 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 347 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 348 | /** |
Andrey Andreev | 7b53d04 | 2012-03-26 23:02:32 +0300 | [diff] [blame] | 349 | * Parse Routes |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 350 | * |
| 351 | * This function matches any routes that may exist in |
| 352 | * the config/routes.php file against the URI to |
| 353 | * determine if the class/method need to be remapped. |
| 354 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 355 | * @return void |
| 356 | */ |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 357 | protected function _parse_routes() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 358 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 359 | // Turn the segment array into a URI string |
| 360 | $uri = implode('/', $this->uri->segments); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 361 | |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 362 | // Is there a literal match? If so we're done |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 363 | if (isset($this->routes[$uri])) |
| 364 | { |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 365 | return $this->_set_request(explode('/', $this->routes[$uri])); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 366 | } |
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 | // Loop through the route array looking for wild-cards |
| 369 | foreach ($this->routes as $key => $val) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 370 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 371 | // Convert wild-cards to RegEx |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 372 | $key = str_replace(array(':any', ':num'), array('.+', '[0-9]+'), $key); |
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 | // Does the RegEx match? |
| 375 | if (preg_match('#^'.$key.'$#', $uri)) |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 376 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 377 | // Do we have a back-reference? |
Andrey Andreev | 7b53d04 | 2012-03-26 23:02:32 +0300 | [diff] [blame] | 378 | if (strpos($val, '$') !== FALSE && strpos($key, '(') !== FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 379 | { |
| 380 | $val = preg_replace('#^'.$key.'$#', $val, $uri); |
| 381 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 382 | |
| 383 | return $this->_set_request(explode('/', $val)); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 384 | } |
| 385 | } |
| 386 | |
| 387 | // If we got this far it means we didn't encounter a |
| 388 | // matching route so we'll set the site default route |
| 389 | $this->_set_request($this->uri->segments); |
| 390 | } |
| 391 | |
| 392 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 393 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 394 | /** |
| 395 | * Set the class name |
| 396 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 397 | * @param string |
| 398 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 399 | */ |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 400 | public function set_class($class) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 401 | { |
Derek Jones | 2615e41 | 2010-10-06 17:51:16 -0500 | [diff] [blame] | 402 | $this->class = str_replace(array('/', '.'), '', $class); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 403 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 404 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 405 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 406 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 407 | /** |
| 408 | * Fetch the current class |
| 409 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 410 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 411 | */ |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 412 | public function fetch_class() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 413 | { |
| 414 | return $this->class; |
| 415 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 416 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 417 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 418 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 419 | /** |
Andrey Andreev | 7b53d04 | 2012-03-26 23:02:32 +0300 | [diff] [blame] | 420 | * Set the method name |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 421 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 422 | * @param string |
| 423 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 424 | */ |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 425 | public function set_method($method) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 426 | { |
| 427 | $this->method = $method; |
| 428 | } |
| 429 | |
| 430 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 431 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 432 | /** |
Andrey Andreev | 7b53d04 | 2012-03-26 23:02:32 +0300 | [diff] [blame] | 433 | * Fetch the current method |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 434 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 435 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 436 | */ |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 437 | public function fetch_method() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 438 | { |
| 439 | if ($this->method == $this->fetch_class()) |
| 440 | { |
| 441 | return 'index'; |
| 442 | } |
| 443 | |
| 444 | return $this->method; |
| 445 | } |
| 446 | |
| 447 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 448 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 449 | /** |
Andrey Andreev | 7b53d04 | 2012-03-26 23:02:32 +0300 | [diff] [blame] | 450 | * Set the directory name |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 451 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 452 | * @param string |
| 453 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 454 | */ |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 455 | public function set_directory($dir) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 456 | { |
Derek Jones | 2615e41 | 2010-10-06 17:51:16 -0500 | [diff] [blame] | 457 | $this->directory = str_replace(array('/', '.'), '', $dir).'/'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 461 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 462 | /** |
Andrey Andreev | 7b53d04 | 2012-03-26 23:02:32 +0300 | [diff] [blame] | 463 | * Fetch the sub-directory (if any) that contains the requested controller class |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 464 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 465 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 466 | */ |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 467 | public function fetch_directory() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 468 | { |
| 469 | return $this->directory; |
| 470 | } |
| 471 | |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 472 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 473 | |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 474 | /** |
Andrey Andreev | 7b53d04 | 2012-03-26 23:02:32 +0300 | [diff] [blame] | 475 | * Set the controller overrides |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 476 | * |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 477 | * @param array |
Andrey Andreev | 7b53d04 | 2012-03-26 23:02:32 +0300 | [diff] [blame] | 478 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 479 | */ |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 480 | public function _set_overrides($routing) |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 481 | { |
| 482 | if ( ! is_array($routing)) |
| 483 | { |
| 484 | return; |
| 485 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 486 | |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 487 | if (isset($routing['directory'])) |
| 488 | { |
| 489 | $this->set_directory($routing['directory']); |
| 490 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 491 | |
Andrey Andreev | 7b53d04 | 2012-03-26 23:02:32 +0300 | [diff] [blame] | 492 | if (isset($routing['controller']) && $routing['controller'] != '') |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 493 | { |
| 494 | $this->set_class($routing['controller']); |
| 495 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 496 | |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 497 | if (isset($routing['function'])) |
| 498 | { |
| 499 | $routing['function'] = ($routing['function'] == '') ? 'index' : $routing['function']; |
| 500 | $this->set_method($routing['function']); |
| 501 | } |
| 502 | } |
| 503 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 504 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 505 | |
| 506 | /* End of file Router.php */ |
Andrey Andreev | 7b53d04 | 2012-03-26 23:02:32 +0300 | [diff] [blame] | 507 | /* Location: ./system/core/Router.php */ |