summaryrefslogtreecommitdiffstats
path: root/system/core/Router.php
diff options
context:
space:
mode:
authorFatih Kalifa <fatih@pvey.es>2013-10-31 20:44:56 +0100
committerFatih Kalifa <fatih@pvey.es>2013-10-31 20:44:56 +0100
commit442b50b0c78a8c630b139d3a9a30d3b731253565 (patch)
treec286e09ca0735068aeddeccd7545cb017c030a9c /system/core/Router.php
parente23ba1373b347ecca61c371c96f0e33da443e915 (diff)
Enable HTTP Verb in Routing
Using array for HTTP Verb e.g: $route['(:any)']['POST'] = "controller/post_method"; $route['path']['GET'] = "controller/path_get_method"; $route['path']['(:any)'] = "controller/path_any_method"; Using (:any) or not will make same result e.g: $route['path']['(:any)'] == $route['path'] So it won't break existing route
Diffstat (limited to 'system/core/Router.php')
-rw-r--r--system/core/Router.php33
1 files changed, 32 insertions, 1 deletions
diff --git a/system/core/Router.php b/system/core/Router.php
index 0f7278ae6..89d11afab 100644
--- a/system/core/Router.php
+++ b/system/core/Router.php
@@ -345,8 +345,20 @@ class CI_Router {
// Turn the segment array into a URI string
$uri = implode('/', $this->uri->segments);
+ // Get HTTP verb
+ $http_verb = strtolower($_SERVER['REQUEST_METHOD']);
+
// Is there a literal match? If so we're done
- if (isset($this->routes[$uri]) && is_string($this->routes[$uri]))
+ if (isset($this->routes[$uri][$http_verb]))
+ {
+ return $this->_set_request(explode('/', $this->routes[$uri][$http_verb]));
+ }
+ else if (isset($this->routes[$uri]['(:any)']))
+ {
+ return $this->_set_request(explode('/', $this->routes[$uri]['(:any)']));
+ }
+ // Fallback to default routing
+ else if (isset($this->routes[$uri]) && is_string($this->routes[$uri]))
{
return $this->_set_request(explode('/', $this->routes[$uri]));
}
@@ -354,6 +366,25 @@ class CI_Router {
// Loop through the route array looking for wildcards
foreach ($this->routes as $key => $val)
{
+ // Check if HTTP Verb is exist
+ if (is_array($val))
+ {
+ // HTTP verb included in routes
+ if (isset($val[$http_verb]))
+ {
+ $val = $val[$http_verb];
+ }
+ else if (isset($val['(:any)']))
+ {
+ $val = $val['(:any)'];
+ }
+ else
+ {
+ // HTTP Verb not found
+ continue;
+ }
+ }
+
// Convert wildcards to RegEx
$key = str_replace(array(':any', ':num'), array('[^/]+', '[0-9]+'), $key);