From 08fec7bdf846daa3dfa4114310f065294ac092fc Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Fri, 19 Jul 2013 16:25:51 +0300 Subject: 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) --- system/core/CodeIgniter.php | 1 - system/core/Router.php | 56 ++++++++++++++++++++++++++++++--------------- 2 files changed, 38 insertions(+), 19 deletions(-) (limited to 'system') diff --git a/system/core/CodeIgniter.php b/system/core/CodeIgniter.php index 3fe5c0648..c68266408 100644 --- a/system/core/CodeIgniter.php +++ b/system/core/CodeIgniter.php @@ -165,7 +165,6 @@ defined('BASEPATH') OR exit('No direct script access allowed'); * ------------------------------------------------------ */ $RTR =& load_class('Router', 'core'); - $RTR->_set_routing(); // Set any routing overrides that may exist in the main index file if (isset($routing)) 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 @@ -81,6 +81,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 * @@ -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'])) { -- cgit v1.2.3-24-g4f1b