CI_Router Class Reference

List of all members.


Public Member Functions

 CI_Router ()
 Constructor.
 _set_routing ()
 Set the route mapping.
 _set_request ($segments=array())
 Set the Route.
 _validate_request ($segments)
 Validates the supplied segments.
 _parse_routes ()
 Parse Routes.
 set_class ($class)
 Set the class name.
 fetch_class ()
 Fetch the current class.
 set_method ($method)
 Set the method name.
 fetch_method ()
 Fetch the current method.
 set_directory ($dir)
 Set the directory name.
 fetch_directory ()
 Fetch the sub-directory (if any) that contains the requested controller class.

Public Attributes

 $config
 $routes = array()
 $error_routes = array()
 $class = ''
 $method = 'index'
 $directory = ''
 $uri_protocol = 'auto'
 $default_controller
 $scaffolding_request = FALSE

Detailed Description

Definition at line 29 of file Router.php.


Member Function Documentation

CI_Router::_parse_routes (  ) 

Parse Routes.

This function matches any routes that may exist in the config/routes.php file against the URI to determine if the class/method need to be remapped.

private

Returns:
void

Definition at line 250 of file Router.php.

References _set_request().

Referenced by _set_routing().

00251         {
00252                 // Do we even have any custom routing to deal with?
00253                 // There is a default scaffolding trigger, so we'll look just for 1
00254                 if (count($this->routes) == 1)
00255                 {
00256                         $this->_set_request($this->uri->segments);
00257                         return;
00258                 }
00259 
00260                 // Turn the segment array into a URI string
00261                 $uri = implode('/', $this->uri->segments);
00262 
00263                 // Is there a literal match?  If so we're done
00264                 if (isset($this->routes[$uri]))
00265                 {
00266                         $this->_set_request(explode('/', $this->routes[$uri]));         
00267                         return;
00268                 }
00269                                 
00270                 // Loop through the route array looking for wild-cards
00271                 foreach ($this->routes as $key => $val)
00272                 {                                               
00273                         // Convert wild-cards to RegEx
00274                         $key = str_replace(':any', '.+', str_replace(':num', '[0-9]+', $key));
00275                         
00276                         // Does the RegEx match?
00277                         if (preg_match('#^'.$key.'$#', $uri))
00278                         {                       
00279                                 // Do we have a back-reference?
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                 // If we got this far it means we didn't encounter a
00291                 // matching route so we'll set the site default route
00292                 $this->_set_request($this->uri->segments);
00293         }

Here is the call graph for this function:

Here is the caller graph for this function:

CI_Router::_set_request ( segments = array()  ) 

Set the Route.

This function takes an array of URI segments as input, and sets the current class/method

private

Parameters:
array 
bool 
Returns:
void

Definition at line 146 of file Router.php.

References _validate_request(), set_class(), and set_method().

Referenced by _parse_routes().

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                         // A scaffolding request. No funny business with the URL
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                                 // A standard method request
00168                                 $this->set_method($segments[1]);
00169                         }
00170                 }
00171                 else
00172                 {
00173                         // This lets the "routed" segment array identify that the default
00174                         // index method is being used.
00175                         $segments[1] = 'index';
00176                 }
00177                 
00178                 // Update our "routed" segment array to contain the segments.
00179                 // Note: If there is no custom routing, this array will be
00180                 // identical to $this->uri->segments
00181                 $this->uri->rsegments = $segments;
00182         }

Here is the call graph for this function:

Here is the caller graph for this function:

CI_Router::_set_routing (  ) 

Set the route mapping.

This function determines what should be served based on the URI request, as well as any "routes" that have been set in the routing config file.

private

Returns:
void

Definition at line 65 of file Router.php.

References $route, _parse_routes(), _validate_request(), log_message(), set_class(), set_method(), and show_error().

Referenced by CI_Router().

00066         {
00067                 // Are query strings enabled in the config file?
00068                 // If so, we're done since segment based URIs are not used with query strings.
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                 // Load the routes.php file.
00082                 @include(APPPATH.'config/routes'.EXT);
00083                 $this->routes = ( ! isset($route) OR ! is_array($route)) ? array() : $route;
00084                 unset($route);
00085 
00086                 // Set the default controller so we can display it in the event
00087                 // the URI doesn't correlated to a valid controller.
00088                 $this->default_controller = ( ! isset($this->routes['default_controller']) OR $this->routes['default_controller'] == '') ? FALSE : strtolower($this->routes['default_controller']);     
00089                 
00090                 // Fetch the complete URI string
00091                 $this->uri->_fetch_uri_string();
00092         
00093                 // Is there a URI string? If not, the default controller specified in the "routes" file will be shown.
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                         // Turn the default route into an array.  We explode it in the event that
00102                         // the controller is located in a subfolder
00103                         $segments = $this->_validate_request(explode('/', $this->default_controller));
00104 
00105                         // Set the class and method
00106                         $this->set_class($segments[0]);
00107                         $this->set_method('index');
00108                         
00109                         // Assign the segments to the URI class
00110                         $this->uri->rsegments = $segments;
00111                         
00112                         // re-index the routed segments array so it starts with 1 rather than 0
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                 // Do we need to remove the URL suffix?
00121                 $this->uri->_remove_url_suffix();
00122                 
00123                 // Compile the segments into an array
00124                 $this->uri->_explode_segments();
00125                 
00126                 // Parse any custom routing that may exist
00127                 $this->_parse_routes();         
00128                 
00129                 // Re-index the segment array so that it starts with 1 rather than 0
00130                 $this->uri->_reindex_segments();
00131         }

Here is the call graph for this function:

Here is the caller graph for this function:

CI_Router::_validate_request ( segments  ) 

Validates the supplied segments.

Attempts to determine the path to the controller.

private

Parameters:
array 
Returns:
array

Definition at line 194 of file Router.php.

References fetch_directory(), set_class(), set_directory(), set_method(), and show_404().

Referenced by _set_request(), and _set_routing().

00195         {
00196                 // Does the requested controller exist in the root folder?
00197                 if (file_exists(APPPATH.'controllers/'.$segments[0].EXT))
00198                 {
00199                         return $segments;
00200                 }
00201 
00202                 // Is the controller in a sub-folder?
00203                 if (is_dir(APPPATH.'controllers/'.$segments[0]))
00204                 {               
00205                         // Set the directory and remove it from the segment array
00206                         $this->set_directory($segments[0]);
00207                         $segments = array_slice($segments, 1);
00208                         
00209                         if (count($segments) > 0)
00210                         {
00211                                 // Does the requested controller exist in the sub-folder?
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                                 // Does the default controller exist in the sub-folder?
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                 // Can't find the requested controller...
00235                 show_404($segments[0]);
00236         }

Here is the call graph for this function:

Here is the caller graph for this function:

CI_Router::CI_Router (  ) 

Constructor.

Runs the route mapping function.

Definition at line 46 of file Router.php.

References _set_routing(), load_class(), and log_message().

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         }

Here is the call graph for this function:

CI_Router::fetch_class (  ) 

Fetch the current class.

public

Returns:
string

Definition at line 317 of file Router.php.

Referenced by fetch_method().

00318         {
00319                 return $this->class;
00320         }

Here is the caller graph for this function:

CI_Router::fetch_directory (  ) 

Fetch the sub-directory (if any) that contains the requested controller class.

public

Returns:
string

Definition at line 376 of file Router.php.

Referenced by _validate_request().

00377         {
00378                 return $this->directory;
00379         }

Here is the caller graph for this function:

CI_Router::fetch_method (  ) 

Fetch the current method.

public

Returns:
string

Definition at line 344 of file Router.php.

References fetch_class().

00345         {
00346                 if ($this->method == $this->fetch_class())
00347                 {
00348                         return 'index';
00349                 }
00350 
00351                 return $this->method;
00352         }

Here is the call graph for this function:

CI_Router::set_class ( class  ) 

Set the class name.

public

Parameters:
string 
Returns:
void

Definition at line 304 of file Router.php.

References $class.

Referenced by _set_request(), _set_routing(), and _validate_request().

00305         {
00306                 $this->class = $class;
00307         }

Here is the caller graph for this function:

CI_Router::set_directory ( dir  ) 

Set the directory name.

public

Parameters:
string 
Returns:
void

Definition at line 363 of file Router.php.

Referenced by _validate_request().

00364         {
00365                 $this->directory = $dir.'/';
00366         }

Here is the caller graph for this function:

CI_Router::set_method ( method  ) 

Set the method name.

public

Parameters:
string 
Returns:
void

Definition at line 331 of file Router.php.

References $method.

Referenced by _set_request(), _set_routing(), and _validate_request().

00332         {
00333                 $this->method = $method;
00334         }

Here is the caller graph for this function:


Member Data Documentation

CI_Router::$class = ''

Definition at line 34 of file Router.php.

Referenced by set_class().

CI_Router::$config

Definition at line 31 of file Router.php.

CI_Router::$default_controller

Definition at line 38 of file Router.php.

CI_Router::$directory = ''

Definition at line 36 of file Router.php.

CI_Router::$error_routes = array()

Definition at line 33 of file Router.php.

CI_Router::$method = 'index'

Definition at line 35 of file Router.php.

Referenced by set_method().

CI_Router::$routes = array()

Definition at line 32 of file Router.php.

CI_Router::$scaffolding_request = FALSE

Definition at line 39 of file Router.php.

CI_Router::$uri_protocol = 'auto'

Definition at line 37 of file Router.php.


The documentation for this class was generated from the following file: