diff options
author | Andrey Andreev <narf@devilix.net> | 2013-07-19 15:25:51 +0200 |
---|---|---|
committer | Andrey Andreev <narf@devilix.net> | 2013-07-19 18:06:55 +0200 |
commit | 08fec7bdf846daa3dfa4114310f065294ac092fc (patch) | |
tree | 59dbd8b39a0064651339f7543993df8f67871db5 /system/core/Router.php | |
parent | 279c76cb3565672594c4e18dd070cb1e1fe38db9 (diff) |
Router improvements
- Make dashes-to-underscores URI segment replacement configurable via ['translate_uri_dashes'].
- Make _set_routing() protected and move the call to the class constructor.
- Remove redudant calls to set_class() and set_method().
- Clean-up/optimize the routes loading procedure.
(fixes issue #2503)
Diffstat (limited to 'system/core/Router.php')
-rw-r--r-- | system/core/Router.php | 56 |
1 files changed, 38 insertions, 18 deletions
diff --git a/system/core/Router.php b/system/core/Router.php index c86ab9c20..cc3916f86 100644 --- a/system/core/Router.php +++ b/system/core/Router.php @@ -82,6 +82,18 @@ class CI_Router { public $default_controller; /** + * Translate URI dashes + * + * Determines whether dashes in controller & method segments + * should be automatically replaced by underscores. + * + * @var bool + */ + public $translate_uri_dashes = FALSE; + + // -------------------------------------------------------------------- + + /** * Class constructor * * Runs the route mapping function. @@ -92,6 +104,7 @@ class CI_Router { { $this->config =& load_class('Config', 'core'); $this->uri =& load_class('URI', 'core'); + $this->_set_routing(); log_message('debug', 'Router Class Initialized'); } @@ -105,7 +118,7 @@ class CI_Router { * * @return void */ - public function _set_routing() + protected function _set_routing() { // Are query strings enabled in the config file? Normally CI doesn't utilize query strings // since URI segments are more search-engine friendly, but they can optionally be used. @@ -143,12 +156,14 @@ class CI_Router { include(APPPATH.'config/'.ENVIRONMENT.'/routes.php'); } - $this->routes = (empty($route) OR ! is_array($route)) ? array() : $route; - unset($route); - - // Set the default controller so we can display it in the event - // the URI doesn't correlated to a valid controller. - $this->default_controller = empty($this->routes['default_controller']) ? FALSE : $this->routes['default_controller']; + // Validate & get reserved routes + if (isset($route) && is_array($route)) + { + isset($route['default_controller']) && $this->default_controller = $route['default_controller']; + isset($route['translate_uri_dashes']) && $this->translate_uri_dashes = $route['translate_uri_dashes']; + unset($route['default_controller'], $route['translate_uri_dashes']); + $this->routes = $route; + } // Were there any query string segments? If so, we'll validate them and bail out since we're done. if (count($segments) > 0) @@ -191,8 +206,6 @@ class CI_Router { $method = 'index'; } - $this->set_class($class); - $this->set_method($method); $this->_set_request(array($class, $method)); // re-index the routed segments array so it starts with 1 rather than 0 @@ -221,8 +234,16 @@ class CI_Router { return $this->_set_default_controller(); } - $this->set_class($segments[0]); + if ($this->translate_uri_dashes === TRUE) + { + $segments[0] = str_replace('-', '_', $segments[0]); + if (isset($segments[1])) + { + $segments[1] = str_replace('-', '_', $segments[1]); + } + } + $this->set_class($segments[0]); isset($segments[1]) OR $segments[1] = 'index'; $this->set_method($segments[1]); @@ -249,13 +270,12 @@ class CI_Router { return $segments; } - $temp = str_replace('-', '_', $segments[0]); + $test = ($this->translate_uri_dashes === TRUE) + ? str_replace('-', '_', $segments[0]) : $segments[0]; // Does the requested controller exist in the root folder? - if (file_exists(APPPATH.'controllers/'.$temp.'.php')) + if (file_exists(APPPATH.'controllers/'.$test.'.php')) { - $segments[0] = $temp; - empty($segments[1]) OR $segments[1] = str_replace('-', '_', $segments[1]); return $segments; } @@ -266,11 +286,11 @@ class CI_Router { $this->set_directory(array_shift($segments)); if (count($segments) > 0) { - $segments[0] = str_replace('-', '_', $segments[0]); - empty($segments[1]) OR $segments[1] = str_replace('-', '_', $segments[1]); + $test = ($this->translate_uri_dashes === TRUE) + ? str_replace('-', '_', $segments[0]) : $segments[0]; - // Does the requested controller exist in the sub-folder? - if ( ! file_exists(APPPATH.'controllers/'.$this->directory.$segments[0].'.php')) + // Does the requested controller exist in the sub-directory? + if ( ! file_exists(APPPATH.'controllers/'.$this->directory.$test.'.php')) { if ( ! empty($this->routes['404_override'])) { |