Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 1 | <?php |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Andrey Andreev | fe9309d | 2015-01-09 17:48:58 +0200 | [diff] [blame] | 5 | * An open source application development framework for PHP |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 7 | * This content is released under the MIT License (MIT) |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 8 | * |
Andrey Andreev | cce6bd1 | 2018-01-09 11:32:02 +0200 | [diff] [blame^] | 9 | * Copyright (c) 2014 - 2018, British Columbia Institute of Technology |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 10 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 11 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 12 | * of this software and associated documentation files (the "Software"), to deal |
| 13 | * in the Software without restriction, including without limitation the rights |
| 14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 15 | * copies of the Software, and to permit persons to whom the Software is |
| 16 | * furnished to do so, subject to the following conditions: |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 17 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 18 | * The above copyright notice and this permission notice shall be included in |
| 19 | * all copies or substantial portions of the Software. |
| 20 | * |
| 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 27 | * THE SOFTWARE. |
| 28 | * |
| 29 | * @package CodeIgniter |
| 30 | * @author EllisLab Dev Team |
Andrey Andreev | 1924e87 | 2016-01-11 12:55:34 +0200 | [diff] [blame] | 31 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/) |
Andrey Andreev | cce6bd1 | 2018-01-09 11:32:02 +0200 | [diff] [blame^] | 32 | * @copyright Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/) |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 33 | * @license http://opensource.org/licenses/MIT MIT License |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 34 | * @link https://codeigniter.com |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 35 | * @since Version 1.0.0 |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 36 | * @filesource |
| 37 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 38 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 39 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 40 | /** |
| 41 | * Router Class |
| 42 | * |
| 43 | * Parses URIs and determines routing |
| 44 | * |
| 45 | * @package CodeIgniter |
| 46 | * @subpackage Libraries |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 47 | * @category Libraries |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 48 | * @author EllisLab Dev Team |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 49 | * @link https://codeigniter.com/user_guide/general/routing.html |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 50 | */ |
| 51 | class CI_Router { |
| 52 | |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 53 | /** |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 54 | * CI_Config class object |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 55 | * |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 56 | * @var object |
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 $config; |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 59 | |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 60 | /** |
| 61 | * List of routes |
| 62 | * |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 63 | * @var array |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 64 | */ |
Timothy Warren | 48a7fbb | 2012-04-23 11:58:16 -0400 | [diff] [blame] | 65 | public $routes = array(); |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 66 | |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 67 | /** |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 68 | * Current class name |
| 69 | * |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 70 | * @var string |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 71 | */ |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 72 | public $class = ''; |
| 73 | |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 74 | /** |
| 75 | * Current method name |
| 76 | * |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 77 | * @var string |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 78 | */ |
Timothy Warren | 48a7fbb | 2012-04-23 11:58:16 -0400 | [diff] [blame] | 79 | public $method = 'index'; |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 80 | |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 81 | /** |
| 82 | * Sub-directory that contains the requested controller class |
| 83 | * |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 84 | * @var string |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 85 | */ |
Andrey Andreev | 2ef5ed4 | 2015-07-17 14:24:26 +0300 | [diff] [blame] | 86 | public $directory; |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 87 | |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 88 | /** |
| 89 | * Default controller (and method if specific) |
| 90 | * |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 91 | * @var string |
David Behler | 07b5342 | 2011-08-15 00:25:06 +0200 | [diff] [blame] | 92 | */ |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 93 | public $default_controller; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 94 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 95 | /** |
Andrey Andreev | 08fec7b | 2013-07-19 16:25:51 +0300 | [diff] [blame] | 96 | * Translate URI dashes |
| 97 | * |
| 98 | * Determines whether dashes in controller & method segments |
| 99 | * should be automatically replaced by underscores. |
| 100 | * |
| 101 | * @var bool |
| 102 | */ |
| 103 | public $translate_uri_dashes = FALSE; |
| 104 | |
Andrey Andreev | 30d5324 | 2014-01-16 14:41:46 +0200 | [diff] [blame] | 105 | /** |
| 106 | * Enable query strings flag |
| 107 | * |
ftwbzhao | bf0488b | 2015-07-06 17:48:08 +0800 | [diff] [blame] | 108 | * Determines whether to use GET parameters or segment URIs |
Andrey Andreev | 30d5324 | 2014-01-16 14:41:46 +0200 | [diff] [blame] | 109 | * |
| 110 | * @var bool |
| 111 | */ |
| 112 | public $enable_query_strings = FALSE; |
| 113 | |
Andrey Andreev | 08fec7b | 2013-07-19 16:25:51 +0300 | [diff] [blame] | 114 | // -------------------------------------------------------------------- |
| 115 | |
| 116 | /** |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 117 | * Class constructor |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 118 | * |
| 119 | * Runs the route mapping function. |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 120 | * |
Andrey Andreev | f0f93f5 | 2015-12-07 12:12:44 +0200 | [diff] [blame] | 121 | * @param array $routing |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 122 | * @return void |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 123 | */ |
Andrey Andreev | c26b9eb | 2014-02-24 11:31:36 +0200 | [diff] [blame] | 124 | public function __construct($routing = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 125 | { |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 126 | $this->config =& load_class('Config', 'core'); |
| 127 | $this->uri =& load_class('URI', 'core'); |
Andrey Andreev | 30d5324 | 2014-01-16 14:41:46 +0200 | [diff] [blame] | 128 | |
| 129 | $this->enable_query_strings = ( ! is_cli() && $this->config->item('enable_query_strings') === TRUE); |
Andrey Andreev | 2ef5ed4 | 2015-07-17 14:24:26 +0300 | [diff] [blame] | 130 | |
| 131 | // If a directory override is configured, it has to be set before any dynamic routing logic |
| 132 | is_array($routing) && isset($routing['directory']) && $this->set_directory($routing['directory']); |
Andrey Andreev | 08fec7b | 2013-07-19 16:25:51 +0300 | [diff] [blame] | 133 | $this->_set_routing(); |
Andrey Andreev | 4e6c528 | 2014-01-10 19:29:49 +0200 | [diff] [blame] | 134 | |
| 135 | // Set any routing overrides that may exist in the main index file |
Andrey Andreev | c26b9eb | 2014-02-24 11:31:36 +0200 | [diff] [blame] | 136 | if (is_array($routing)) |
Andrey Andreev | 4e6c528 | 2014-01-10 19:29:49 +0200 | [diff] [blame] | 137 | { |
Andrey Andreev | 2ef5ed4 | 2015-07-17 14:24:26 +0300 | [diff] [blame] | 138 | empty($routing['controller']) OR $this->set_class($routing['controller']); |
| 139 | empty($routing['function']) OR $this->set_method($routing['function']); |
Andrey Andreev | 4e6c528 | 2014-01-10 19:29:49 +0200 | [diff] [blame] | 140 | } |
| 141 | |
Andrey Andreev | 90726b8 | 2015-01-20 12:39:22 +0200 | [diff] [blame] | 142 | log_message('info', 'Router Class Initialized'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 143 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 144 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 145 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 146 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 147 | /** |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 148 | * Set route mapping |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 149 | * |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 150 | * Determines what should be served based on the URI request, |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 151 | * as well as any "routes" that have been set in the routing config file. |
| 152 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 153 | * @return void |
| 154 | */ |
Andrey Andreev | 08fec7b | 2013-07-19 16:25:51 +0300 | [diff] [blame] | 155 | protected function _set_routing() |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 156 | { |
Andrey Andreev | f2239fe | 2015-09-14 13:48:03 +0300 | [diff] [blame] | 157 | // Load the routes.php file. It would be great if we could |
| 158 | // skip this for enable_query_strings = TRUE, but then |
| 159 | // default_controller would be empty ... |
| 160 | if (file_exists(APPPATH.'config/routes.php')) |
| 161 | { |
| 162 | include(APPPATH.'config/routes.php'); |
| 163 | } |
| 164 | |
| 165 | if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/routes.php')) |
| 166 | { |
| 167 | include(APPPATH.'config/'.ENVIRONMENT.'/routes.php'); |
| 168 | } |
| 169 | |
| 170 | // Validate & get reserved routes |
| 171 | if (isset($route) && is_array($route)) |
| 172 | { |
| 173 | isset($route['default_controller']) && $this->default_controller = $route['default_controller']; |
| 174 | isset($route['translate_uri_dashes']) && $this->translate_uri_dashes = $route['translate_uri_dashes']; |
| 175 | unset($route['default_controller'], $route['translate_uri_dashes']); |
| 176 | $this->routes = $route; |
| 177 | } |
| 178 | |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 179 | // 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] | 180 | // 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] | 181 | // If this feature is enabled, we will gather the directory/class/method a little differently |
Andrey Andreev | 30d5324 | 2014-01-16 14:41:46 +0200 | [diff] [blame] | 182 | if ($this->enable_query_strings) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 183 | { |
Andrey Andreev | 2ef5ed4 | 2015-07-17 14:24:26 +0300 | [diff] [blame] | 184 | // If the directory is set at this time, it means an override exists, so skip the checks |
| 185 | if ( ! isset($this->directory)) |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 186 | { |
Andrey Andreev | 2ef5ed4 | 2015-07-17 14:24:26 +0300 | [diff] [blame] | 187 | $_d = $this->config->item('directory_trigger'); |
| 188 | $_d = isset($_GET[$_d]) ? trim($_GET[$_d], " \t\n\r\0\x0B/") : ''; |
| 189 | |
| 190 | if ($_d !== '') |
| 191 | { |
| 192 | $this->uri->filter_uri($_d); |
| 193 | $this->set_directory($_d); |
| 194 | } |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 195 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 196 | |
Andrey Andreev | bfa233f | 2014-12-05 12:00:11 +0200 | [diff] [blame] | 197 | $_c = trim($this->config->item('controller_trigger')); |
Andrey Andreev | 30d5324 | 2014-01-16 14:41:46 +0200 | [diff] [blame] | 198 | if ( ! empty($_GET[$_c])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 199 | { |
Andrey Andreev | bfa233f | 2014-12-05 12:00:11 +0200 | [diff] [blame] | 200 | $this->uri->filter_uri($_GET[$_c]); |
| 201 | $this->set_class($_GET[$_c]); |
Andrey Andreev | 30d5324 | 2014-01-16 14:41:46 +0200 | [diff] [blame] | 202 | |
Andrey Andreev | bfa233f | 2014-12-05 12:00:11 +0200 | [diff] [blame] | 203 | $_f = trim($this->config->item('function_trigger')); |
Andrey Andreev | 30d5324 | 2014-01-16 14:41:46 +0200 | [diff] [blame] | 204 | if ( ! empty($_GET[$_f])) |
| 205 | { |
Andrey Andreev | bfa233f | 2014-12-05 12:00:11 +0200 | [diff] [blame] | 206 | $this->uri->filter_uri($_GET[$_f]); |
| 207 | $this->set_method($_GET[$_f]); |
Andrey Andreev | 30d5324 | 2014-01-16 14:41:46 +0200 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | $this->uri->rsegments = array( |
| 211 | 1 => $this->class, |
| 212 | 2 => $this->method |
| 213 | ); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 214 | } |
Andrey Andreev | 30d5324 | 2014-01-16 14:41:46 +0200 | [diff] [blame] | 215 | else |
| 216 | { |
| 217 | $this->_set_default_controller(); |
| 218 | } |
| 219 | |
| 220 | // Routing rules don't apply to query strings and we don't need to detect |
| 221 | // directories, so we're done here |
| 222 | return; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 223 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 224 | |
Andrey Andreev | 30d5324 | 2014-01-16 14:41:46 +0200 | [diff] [blame] | 225 | // Is there anything to parse? |
| 226 | if ($this->uri->uri_string !== '') |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 227 | { |
Andrey Andreev | 30d5324 | 2014-01-16 14:41:46 +0200 | [diff] [blame] | 228 | $this->_parse_routes(); |
| 229 | } |
| 230 | else |
| 231 | { |
| 232 | $this->_set_default_controller(); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | // -------------------------------------------------------------------- |
| 237 | |
| 238 | /** |
| 239 | * Set request route |
| 240 | * |
| 241 | * Takes an array of URI segments as input and sets the class/method |
| 242 | * to be called. |
| 243 | * |
| 244 | * @used-by CI_Router::_parse_routes() |
| 245 | * @param array $segments URI segments |
| 246 | * @return void |
| 247 | */ |
| 248 | protected function _set_request($segments = array()) |
| 249 | { |
| 250 | $segments = $this->_validate_request($segments); |
| 251 | // If we don't have any segments left - try the default controller; |
| 252 | // WARNING: Directories get shifted out of the segments array! |
| 253 | if (empty($segments)) |
| 254 | { |
| 255 | $this->_set_default_controller(); |
| 256 | return; |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 257 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 258 | |
Andrey Andreev | 30d5324 | 2014-01-16 14:41:46 +0200 | [diff] [blame] | 259 | if ($this->translate_uri_dashes === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 260 | { |
Andrey Andreev | 30d5324 | 2014-01-16 14:41:46 +0200 | [diff] [blame] | 261 | $segments[0] = str_replace('-', '_', $segments[0]); |
| 262 | if (isset($segments[1])) |
| 263 | { |
| 264 | $segments[1] = str_replace('-', '_', $segments[1]); |
| 265 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 266 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 267 | |
Andrey Andreev | 30d5324 | 2014-01-16 14:41:46 +0200 | [diff] [blame] | 268 | $this->set_class($segments[0]); |
| 269 | if (isset($segments[1])) |
| 270 | { |
| 271 | $this->set_method($segments[1]); |
| 272 | } |
Andrey Andreev | 9cab427 | 2014-03-30 18:58:23 +0300 | [diff] [blame] | 273 | else |
| 274 | { |
| 275 | $segments[1] = 'index'; |
| 276 | } |
Andrey Andreev | 30d5324 | 2014-01-16 14:41:46 +0200 | [diff] [blame] | 277 | |
| 278 | array_unshift($segments, NULL); |
| 279 | unset($segments[0]); |
| 280 | $this->uri->rsegments = $segments; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 281 | } |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 282 | |
| 283 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 284 | |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 285 | /** |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 286 | * Set default controller |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 287 | * |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 288 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 289 | */ |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 290 | protected function _set_default_controller() |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 291 | { |
Andrey Andreev | 533bf2d | 2012-11-02 22:44:29 +0200 | [diff] [blame] | 292 | if (empty($this->default_controller)) |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 293 | { |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 294 | 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] | 295 | } |
Andrey Andreev | d1097a1 | 2012-11-01 19:55:42 +0200 | [diff] [blame] | 296 | |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 297 | // Is the method being specified? |
Andrey Andreev | 533bf2d | 2012-11-02 22:44:29 +0200 | [diff] [blame] | 298 | if (sscanf($this->default_controller, '%[^/]/%s', $class, $method) !== 2) |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 299 | { |
Andrey Andreev | 533bf2d | 2012-11-02 22:44:29 +0200 | [diff] [blame] | 300 | $method = 'index'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 301 | } |
Andrey Andreev | 533bf2d | 2012-11-02 22:44:29 +0200 | [diff] [blame] | 302 | |
Andrey Andreev | 30d5324 | 2014-01-16 14:41:46 +0200 | [diff] [blame] | 303 | if ( ! file_exists(APPPATH.'controllers/'.$this->directory.ucfirst($class).'.php')) |
| 304 | { |
| 305 | // This will trigger 404 later |
| 306 | return; |
| 307 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 308 | |
Andrey Andreev | 30d5324 | 2014-01-16 14:41:46 +0200 | [diff] [blame] | 309 | $this->set_class($class); |
| 310 | $this->set_method($method); |
| 311 | |
| 312 | // Assign routed segments, index starting from 1 |
| 313 | $this->uri->rsegments = array( |
| 314 | 1 => $class, |
| 315 | 2 => $method |
| 316 | ); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 317 | |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 318 | log_message('debug', 'No URI present. Default controller set.'); |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 319 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 320 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 321 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 322 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 323 | /** |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 324 | * Validate request |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 325 | * |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 326 | * Attempts validate the URI request and determine the controller path. |
| 327 | * |
Andrey Andreev | 30d5324 | 2014-01-16 14:41:46 +0200 | [diff] [blame] | 328 | * @used-by CI_Router::_set_request() |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 329 | * @param array $segments URI segments |
Andrey Andreev | 30d5324 | 2014-01-16 14:41:46 +0200 | [diff] [blame] | 330 | * @return mixed URI segments |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 331 | */ |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 332 | protected function _validate_request($segments) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 333 | { |
Andrey Andreev | 30d5324 | 2014-01-16 14:41:46 +0200 | [diff] [blame] | 334 | $c = count($segments); |
Andrey Andreev | 2ef5ed4 | 2015-07-17 14:24:26 +0300 | [diff] [blame] | 335 | $directory_override = isset($this->directory); |
| 336 | |
Andrey Andreev | 30d5324 | 2014-01-16 14:41:46 +0200 | [diff] [blame] | 337 | // Loop through our segments and return as soon as a controller |
| 338 | // is found or when such a directory doesn't exist |
| 339 | while ($c-- > 0) |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 340 | { |
Andrey Andreev | 30d5324 | 2014-01-16 14:41:46 +0200 | [diff] [blame] | 341 | $test = $this->directory |
| 342 | .ucfirst($this->translate_uri_dashes === TRUE ? str_replace('-', '_', $segments[0]) : $segments[0]); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 343 | |
Andrey Andreev | 2ef5ed4 | 2015-07-17 14:24:26 +0300 | [diff] [blame] | 344 | if ( ! file_exists(APPPATH.'controllers/'.$test.'.php') |
| 345 | && $directory_override === FALSE |
| 346 | && is_dir(APPPATH.'controllers/'.$this->directory.$segments[0]) |
| 347 | ) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 348 | { |
Andrey Andreev | 30d5324 | 2014-01-16 14:41:46 +0200 | [diff] [blame] | 349 | $this->set_directory(array_shift($segments), TRUE); |
| 350 | continue; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 351 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 352 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 353 | return $segments; |
| 354 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 355 | |
Andrey Andreev | 30d5324 | 2014-01-16 14:41:46 +0200 | [diff] [blame] | 356 | // This means that all segments were actually directories |
| 357 | return $segments; |
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 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 361 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 362 | /** |
Andrey Andreev | 7b53d04 | 2012-03-26 23:02:32 +0300 | [diff] [blame] | 363 | * Parse Routes |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 364 | * |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 365 | * Matches any routes that may exist in the config/routes.php file |
| 366 | * against the URI to determine if the class/method need to be remapped. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 367 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 368 | * @return void |
| 369 | */ |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 370 | protected function _parse_routes() |
Jonatas Miguel | 59dbe85 | 2012-08-07 12:13:32 +0100 | [diff] [blame] | 371 | { |
| 372 | // Turn the segment array into a URI string |
| 373 | $uri = implode('/', $this->uri->segments); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 374 | |
Fatih Kalifa | 442b50b | 2013-11-01 02:44:56 +0700 | [diff] [blame] | 375 | // Get HTTP verb |
Andrey Andreev | c761a20 | 2013-11-11 14:02:15 +0200 | [diff] [blame] | 376 | $http_verb = isset($_SERVER['REQUEST_METHOD']) ? strtolower($_SERVER['REQUEST_METHOD']) : 'cli'; |
Fatih Kalifa | 442b50b | 2013-11-01 02:44:56 +0700 | [diff] [blame] | 377 | |
vlakoff | c941d85 | 2013-08-06 14:44:40 +0200 | [diff] [blame] | 378 | // Loop through the route array looking for wildcards |
Jonatas Miguel | 59dbe85 | 2012-08-07 12:13:32 +0100 | [diff] [blame] | 379 | foreach ($this->routes as $key => $val) |
| 380 | { |
Andrey Andreev | abc299b | 2015-08-05 12:26:22 +0300 | [diff] [blame] | 381 | // Check if route format is using HTTP verbs |
Fatih Kalifa | 442b50b | 2013-11-01 02:44:56 +0700 | [diff] [blame] | 382 | if (is_array($val)) |
| 383 | { |
Andrey Andreev | abc299b | 2015-08-05 12:26:22 +0300 | [diff] [blame] | 384 | $val = array_change_key_case($val, CASE_LOWER); |
Fatih Kalifa | 442b50b | 2013-11-01 02:44:56 +0700 | [diff] [blame] | 385 | if (isset($val[$http_verb])) |
| 386 | { |
| 387 | $val = $val[$http_verb]; |
| 388 | } |
Fatih Kalifa | 442b50b | 2013-11-01 02:44:56 +0700 | [diff] [blame] | 389 | else |
| 390 | { |
Fatih Kalifa | 442b50b | 2013-11-01 02:44:56 +0700 | [diff] [blame] | 391 | continue; |
| 392 | } |
| 393 | } |
| 394 | |
vlakoff | c941d85 | 2013-08-06 14:44:40 +0200 | [diff] [blame] | 395 | // Convert wildcards to RegEx |
Andrey Andreev | 7676c2d | 2012-10-30 13:42:01 +0200 | [diff] [blame] | 396 | $key = str_replace(array(':any', ':num'), array('[^/]+', '[0-9]+'), $key); |
Derek Jones | c773840 | 2010-03-02 13:55:13 -0600 | [diff] [blame] | 397 | |
Jonatas Miguel | 59dbe85 | 2012-08-07 12:13:32 +0100 | [diff] [blame] | 398 | // Does the RegEx match? |
| 399 | if (preg_match('#^'.$key.'$#', $uri, $matches)) |
| 400 | { |
Jonatas Miguel | 59dbe85 | 2012-08-07 12:13:32 +0100 | [diff] [blame] | 401 | // Are we using callbacks to process back-references? |
Andrey Andreev | da5562a | 2012-11-08 12:34:38 +0200 | [diff] [blame] | 402 | if ( ! is_string($val) && is_callable($val)) |
Jonatas Miguel | 59dbe85 | 2012-08-07 12:13:32 +0100 | [diff] [blame] | 403 | { |
| 404 | // Remove the original string from the matches array. |
| 405 | array_shift($matches); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 406 | |
Jonatas Miguel | 59dbe85 | 2012-08-07 12:13:32 +0100 | [diff] [blame] | 407 | // Execute the callback using the values in matches as its parameters. |
| 408 | $val = call_user_func_array($val, $matches); |
| 409 | } |
Andrey Andreev | da5562a | 2012-11-08 12:34:38 +0200 | [diff] [blame] | 410 | // Are we using the default routing method for back-references? |
| 411 | elseif (strpos($val, '$') !== FALSE && strpos($key, '(') !== FALSE) |
| 412 | { |
| 413 | $val = preg_replace('#^'.$key.'$#', $val, $uri); |
| 414 | } |
Jonatas Miguel | c0d98b2 | 2012-08-06 15:42:50 +0100 | [diff] [blame] | 415 | |
Andrey Andreev | 30d5324 | 2014-01-16 14:41:46 +0200 | [diff] [blame] | 416 | $this->_set_request(explode('/', $val)); |
| 417 | return; |
Jonatas Miguel | 59dbe85 | 2012-08-07 12:13:32 +0100 | [diff] [blame] | 418 | } |
| 419 | } |
Jonatas Miguel | c0d98b2 | 2012-08-06 15:42:50 +0100 | [diff] [blame] | 420 | |
Jonatas Miguel | 59dbe85 | 2012-08-07 12:13:32 +0100 | [diff] [blame] | 421 | // If we got this far it means we didn't encounter a |
| 422 | // matching route so we'll set the site default route |
Andrey Andreev | a9237cb | 2014-01-18 19:07:20 +0200 | [diff] [blame] | 423 | $this->_set_request(array_values($this->uri->segments)); |
Jonatas Miguel | 59dbe85 | 2012-08-07 12:13:32 +0100 | [diff] [blame] | 424 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 425 | |
| 426 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 427 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 428 | /** |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 429 | * Set class name |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 430 | * |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 431 | * @param string $class Class name |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 432 | * @return void |
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 set_class($class) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 435 | { |
Derek Jones | 2615e41 | 2010-10-06 17:51:16 -0500 | [diff] [blame] | 436 | $this->class = str_replace(array('/', '.'), '', $class); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 437 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 438 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 | /** |
| 442 | * Fetch the current class |
| 443 | * |
Andrey Andreev | 0e4237f | 2013-04-04 16:53:21 +0300 | [diff] [blame] | 444 | * @deprecated 3.0.0 Read the 'class' property instead |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 445 | * @return string |
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 fetch_class() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 448 | { |
| 449 | return $this->class; |
| 450 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 451 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 455 | * Set method name |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 456 | * |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 457 | * @param string $method Method name |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 458 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 459 | */ |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 460 | public function set_method($method) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 461 | { |
| 462 | $this->method = $method; |
| 463 | } |
| 464 | |
| 465 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 466 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 467 | /** |
Andrey Andreev | 7b53d04 | 2012-03-26 23:02:32 +0300 | [diff] [blame] | 468 | * Fetch the current method |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 469 | * |
Andrey Andreev | 0e4237f | 2013-04-04 16:53:21 +0300 | [diff] [blame] | 470 | * @deprecated 3.0.0 Read the 'method' property instead |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 471 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 472 | */ |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 473 | public function fetch_method() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 474 | { |
Andrey Andreev | 0e4237f | 2013-04-04 16:53:21 +0300 | [diff] [blame] | 475 | return $this->method; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 479 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 480 | /** |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 481 | * Set directory name |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 482 | * |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 483 | * @param string $dir Directory name |
Calvin Tam | 55bc505 | 2015-07-24 02:27:24 -0700 | [diff] [blame] | 484 | * @param bool $append Whether we're appending rather than setting the full value |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 485 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 486 | */ |
Andrey Andreev | 30d5324 | 2014-01-16 14:41:46 +0200 | [diff] [blame] | 487 | public function set_directory($dir, $append = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 488 | { |
Andrey Andreev | 30d5324 | 2014-01-16 14:41:46 +0200 | [diff] [blame] | 489 | if ($append !== TRUE OR empty($this->directory)) |
| 490 | { |
| 491 | $this->directory = str_replace('.', '', trim($dir, '/')).'/'; |
| 492 | } |
| 493 | else |
| 494 | { |
| 495 | $this->directory .= str_replace('.', '', trim($dir, '/')).'/'; |
| 496 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 500 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 501 | /** |
Andrey Andreev | b9fe7e9 | 2012-10-27 18:45:59 +0300 | [diff] [blame] | 502 | * Fetch directory |
| 503 | * |
| 504 | * Feches the sub-directory (if any) that contains the requested |
| 505 | * controller class. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 506 | * |
Andrey Andreev | 0e4237f | 2013-04-04 16:53:21 +0300 | [diff] [blame] | 507 | * @deprecated 3.0.0 Read the 'directory' property instead |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 508 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 509 | */ |
Andrey Andreev | ba6c041 | 2012-01-07 21:10:09 +0200 | [diff] [blame] | 510 | public function fetch_directory() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 511 | { |
| 512 | return $this->directory; |
| 513 | } |
| 514 | |
| 515 | } |