From 6a656fe2671f868f15eb54548a870a2668c4285d Mon Sep 17 00:00:00 2001 From: Jonatas Miguel Date: Mon, 6 Aug 2012 12:37:48 +0100 Subject: Moved callback routing feature to a sustom MY_Router.php class. --- application/core/MY_Router.php | 62 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 application/core/MY_Router.php diff --git a/application/core/MY_Router.php b/application/core/MY_Router.php new file mode 100644 index 000000000..2a20f579f --- /dev/null +++ b/application/core/MY_Router.php @@ -0,0 +1,62 @@ +uri->segments); + + // Is there a literal match? If so we're done + if (isset($this->routes[$uri])) + { + return $this->_set_request(explode('/', $this->routes[$uri])); + } + + // Loop through the route array looking for wild-cards + foreach ($this->routes as $key => $val) + { + // Convert wild-cards to RegEx + $key = str_replace(':any', '.+', str_replace(':num', '[0-9]+', $key)); + + // Does the RegEx match? + if (preg_match('#^'.$key.'$#', $uri)) + { + // Are we using a callback? + $callable = is_callable($val); + + // Determine the appropriate preg_replace to use. + $preg_replace_type = $callable? 'preg_replace_callback': 'preg_replace'; + + // Are we using callbacks to process the matches? + if($callable){ + $val = function($matches)use($val){ + // Remove the string we are matching against from the matches array. + array_shift($matches); + + // Distribute the matches to the arguments of the user's callback. + return call_user_func_array($val, $matches); + }; + } + + // Do we have a back-reference? + if ($callable OR (strpos($val, '$') !== FALSE AND strpos($key, '(') !== FALSE)) + { + $val = call_user_func($preg_replace_type, '#^'.$key.'$#', $val, $uri); + } + + return $this->_set_request(explode('/', $val)); + } + } + + // If we got this far it means we didn't encounter a + // matching route so we'll set the site default route + $this->_set_request($this->uri->segments); + } +} \ No newline at end of file -- cgit v1.2.3-24-g4f1b