summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--system/core/Router.php31
-rw-r--r--user_guide_src/source/changelog.rst1
-rw-r--r--user_guide_src/source/general/routing.rst36
3 files changed, 66 insertions, 2 deletions
diff --git a/system/core/Router.php b/system/core/Router.php
index 0f7278ae6..9071f84b7 100644
--- a/system/core/Router.php
+++ b/system/core/Router.php
@@ -345,15 +345,42 @@ 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]))
{
- return $this->_set_request(explode('/', $this->routes[$uri]));
+ // Check default routes format
+ if (is_string($this->routes[$uri]))
+ {
+ return $this->_set_request(explode('/', $this->routes[$uri]));
+ }
+ // Is there any matching http verb?
+ elseif (is_array($this->routes[$uri]) && isset($this->routes[$uri][$http_verb]))
+ {
+ return $this->_set_request(explode('/', $this->routes[$uri][$http_verb]));
+ }
}
// Loop through the route array looking for wildcards
foreach ($this->routes as $key => $val)
{
+ // Check if route format is using http verb
+ if (is_array($val))
+ {
+ // Does the http verb match?
+ if (isset($val[$http_verb]))
+ {
+ $val = $val[$http_verb];
+ }
+ // No match, skip to next rule
+ else
+ {
+ continue;
+ }
+ }
+
// Convert wildcards to RegEx
$key = str_replace(array(':any', ':num'), array('[^/]+', '[0-9]+'), $key);
diff --git a/user_guide_src/source/changelog.rst b/user_guide_src/source/changelog.rst
index 1c1100b3d..fbea04c43 100644
--- a/user_guide_src/source/changelog.rst
+++ b/user_guide_src/source/changelog.rst
@@ -429,6 +429,7 @@ Release Date: Not Released
- :doc:`URI Routing <general/routing>` changes include:
+ - Added possibility to route requests using HTTP Verb
- Added possibility to route requests using callbacks.
- Added a new reserved route (*translate_uri_dashes*) to allow usage of dashes in the controller and method URI segments.
- Deprecated methods ``fetch_directory()``, ``fetch_class()`` and ``fetch_method()`` in favor of their respective public properties.
diff --git a/user_guide_src/source/general/routing.rst b/user_guide_src/source/general/routing.rst
index 5520f59fe..6495f1ad4 100644
--- a/user_guide_src/source/general/routing.rst
+++ b/user_guide_src/source/general/routing.rst
@@ -142,6 +142,42 @@ routing rules to process the back-references. Example::
return 'catalog/product_edit/' . strtolower($product_type) . '/' . $id;
};
+Using HTTP Verb in Routes
+=========================
+
+If you prefer you can use HTTP Verb (or method) to define your routing rules.
+This is particularly useful when building RESTful application. You can use standard HTTP
+Verb (GET, PUT, POST, DELETE) or custom HTTP Verb (e.g: PURGE). HTTP Verb rule is case
+insensitive. All you need to do is add array index using HTTP Verb rule. Example::
+
+ $route['products']['put'] = 'product/insert';
+
+In the above example, a PUT request to URI "products" would call the "product" controller
+class and "insert" method
+
+::
+
+ $route['products/(:num)']['DELETE'] = 'product/delete/$1';
+
+A DELETE request to URL with "products" as first segment and a number in the second will be
+remapped to the "product" class and "delete" method passing in the match as a variable to
+the method.
+
+::
+
+ $route['products/([a-z]+)/(\d+)']['get'] = 'product/$1/$2';
+
+A GET request to a URI similar to products/shirts/123 would call the "product" controller
+class and "shirt" method with number as method parameter
+
+Using HTTP Verb is optional, so if you want any HTTP Verb to be handled in one rule
+You could just write your routing rule without HTTP Verb. Example::
+
+ $route['product'] = 'product';
+
+This way, all incoming request using any HTTP method containing the word "product"
+in the first segment will be remapped to "product" class
+
Reserved Routes
===============