Router.php
Go to the documentation of this file.00001 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 class CI_Router {
00030
00031 var $config;
00032 var $routes = array();
00033 var $error_routes = array();
00034 var $class = '';
00035 var $method = 'index';
00036 var $directory = '';
00037 var $uri_protocol = 'auto';
00038 var $default_controller;
00039 var $scaffolding_request = FALSE;
00040
00041
00042
00043
00044
00045
00046 function CI_Router()
00047 {
00048 $this->config =& load_class('Config');
00049 $this->uri =& load_class('URI');
00050 $this->_set_routing();
00051 log_message('debug', "Router Class Initialized");
00052 }
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065 function _set_routing()
00066 {
00067
00068
00069 if ($this->config->item('enable_query_strings') === TRUE AND isset($_GET[$this->config->item('controller_trigger')]))
00070 {
00071 $this->set_class(trim($this->uri->_filter_uri($_GET[$this->config->item('controller_trigger')])));
00072
00073 if (isset($_GET[$this->config->item('function_trigger')]))
00074 {
00075 $this->set_method(trim($this->uri->_filter_uri($_GET[$this->config->item('function_trigger')])));
00076 }
00077
00078 return;
00079 }
00080
00081
00082 @include(APPPATH.'config/routes'.EXT);
00083 $this->routes = ( ! isset($route) OR ! is_array($route)) ? array() : $route;
00084 unset($route);
00085
00086
00087
00088 $this->default_controller = ( ! isset($this->routes['default_controller']) OR $this->routes['default_controller'] == '') ? FALSE : strtolower($this->routes['default_controller']);
00089
00090
00091 $this->uri->_fetch_uri_string();
00092
00093
00094 if ($this->uri->uri_string == '')
00095 {
00096 if ($this->default_controller === FALSE)
00097 {
00098 show_error("Unable to determine what should be displayed. A default route has not been specified in the routing file.");
00099 }
00100
00101
00102
00103 $segments = $this->_validate_request(explode('/', $this->default_controller));
00104
00105
00106 $this->set_class($segments[0]);
00107 $this->set_method('index');
00108
00109
00110 $this->uri->rsegments = $segments;
00111
00112
00113 $this->uri->_reindex_segments();
00114
00115 log_message('debug', "No URI present. Default controller set.");
00116 return;
00117 }
00118 unset($this->routes['default_controller']);
00119
00120
00121 $this->uri->_remove_url_suffix();
00122
00123
00124 $this->uri->_explode_segments();
00125
00126
00127 $this->_parse_routes();
00128
00129
00130 $this->uri->_reindex_segments();
00131 }
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146 function _set_request($segments = array())
00147 {
00148 $segments = $this->_validate_request($segments);
00149
00150 if (count($segments) == 0)
00151 {
00152 return;
00153 }
00154
00155 $this->set_class($segments[0]);
00156
00157 if (isset($segments[1]))
00158 {
00159
00160 if ($this->routes['scaffolding_trigger'] == $segments[1] AND $segments[1] != '_ci_scaffolding')
00161 {
00162 $this->scaffolding_request = TRUE;
00163 unset($this->routes['scaffolding_trigger']);
00164 }
00165 else
00166 {
00167
00168 $this->set_method($segments[1]);
00169 }
00170 }
00171 else
00172 {
00173
00174
00175 $segments[1] = 'index';
00176 }
00177
00178
00179
00180
00181 $this->uri->rsegments = $segments;
00182 }
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194 function _validate_request($segments)
00195 {
00196
00197 if (file_exists(APPPATH.'controllers/'.$segments[0].EXT))
00198 {
00199 return $segments;
00200 }
00201
00202
00203 if (is_dir(APPPATH.'controllers/'.$segments[0]))
00204 {
00205
00206 $this->set_directory($segments[0]);
00207 $segments = array_slice($segments, 1);
00208
00209 if (count($segments) > 0)
00210 {
00211
00212 if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].EXT))
00213 {
00214 show_404($this->fetch_directory().$segments[0]);
00215 }
00216 }
00217 else
00218 {
00219 $this->set_class($this->default_controller);
00220 $this->set_method('index');
00221
00222
00223 if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT))
00224 {
00225 $this->directory = '';
00226 return array();
00227 }
00228
00229 }
00230
00231 return $segments;
00232 }
00233
00234
00235 show_404($segments[0]);
00236 }
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250 function _parse_routes()
00251 {
00252
00253
00254 if (count($this->routes) == 1)
00255 {
00256 $this->_set_request($this->uri->segments);
00257 return;
00258 }
00259
00260
00261 $uri = implode('/', $this->uri->segments);
00262
00263
00264 if (isset($this->routes[$uri]))
00265 {
00266 $this->_set_request(explode('/', $this->routes[$uri]));
00267 return;
00268 }
00269
00270
00271 foreach ($this->routes as $key => $val)
00272 {
00273
00274 $key = str_replace(':any', '.+', str_replace(':num', '[0-9]+', $key));
00275
00276
00277 if (preg_match('#^'.$key.'$#', $uri))
00278 {
00279
00280 if (strpos($val, '$') !== FALSE AND strpos($key, '(') !== FALSE)
00281 {
00282 $val = preg_replace('#^'.$key.'$#', $val, $uri);
00283 }
00284
00285 $this->_set_request(explode('/', $val));
00286 return;
00287 }
00288 }
00289
00290
00291
00292 $this->_set_request($this->uri->segments);
00293 }
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304 function set_class($class)
00305 {
00306 $this->class = $class;
00307 }
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317 function fetch_class()
00318 {
00319 return $this->class;
00320 }
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331 function set_method($method)
00332 {
00333 $this->method = $method;
00334 }
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344 function fetch_method()
00345 {
00346 if ($this->method == $this->fetch_class())
00347 {
00348 return 'index';
00349 }
00350
00351 return $this->method;
00352 }
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363 function set_directory($dir)
00364 {
00365 $this->directory = $dir.'/';
00366 }
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376 function fetch_directory()
00377 {
00378 return $this->directory;
00379 }
00380
00381 }
00382
00383
00384
00385