summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--application/config/user_agents.php3
-rw-r--r--system/core/Router.php29
-rw-r--r--user_guide_src/source/changelog.rst1
-rw-r--r--user_guide_src/source/general/routing.rst23
4 files changed, 53 insertions, 3 deletions
diff --git a/application/config/user_agents.php b/application/config/user_agents.php
index 899e96a94..0953deafd 100644
--- a/application/config/user_agents.php
+++ b/application/config/user_agents.php
@@ -104,7 +104,8 @@ $browsers = array(
'hotjava' => 'HotJava',
'amaya' => 'Amaya',
'IBrowse' => 'IBrowse',
- 'Maxthon' => 'Maxthon'
+ 'Maxthon' => 'Maxthon',
+ 'Ubuntu' => 'Ubuntu Web Browser'
);
$mobiles = array(
diff --git a/system/core/Router.php b/system/core/Router.php
index 0f7278ae6..d467d60fd 100644
--- a/system/core/Router.php
+++ b/system/core/Router.php
@@ -345,15 +345,40 @@ class CI_Router {
// Turn the segment array into a URI string
$uri = implode('/', $this->uri->segments);
+ // Get HTTP verb
+ $http_verb = isset($_SERVER['REQUEST_METHOD']) ? strtolower($_SERVER['REQUEST_METHOD']) : 'cli';
+
// 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 a 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))
+ {
+ if (isset($val[$http_verb]))
+ {
+ $val = $val[$http_verb];
+ }
+ 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..5fc86b1b5 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 verbs.
- 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..0b91d3fa9 100644
--- a/user_guide_src/source/general/routing.rst
+++ b/user_guide_src/source/general/routing.rst
@@ -142,6 +142,29 @@ routing rules to process the back-references. Example::
return 'catalog/product_edit/' . strtolower($product_type) . '/' . $id;
};
+Using HTTP verbs in routes
+==========================
+
+It is possible to use HTTP verbs (request method) to define your routing rules.
+This is particularly useful when building RESTful applications. You can use standard HTTP
+verbs (GET, PUT, POST, DELETE, PATCH) or a custom one such (e.g. PURGE). HTTP verb rules
+are case-insensitive. All you need to do is to add the verb as an array key to your route.
+Example::
+
+ $route['products']['put'] = 'product/insert';
+
+In the above example, a PUT request to URI "products" would call the ``Product::insert()``
+controller method.
+
+::
+
+ $route['products/(:num)']['DELETE'] = 'product/delete/$1';
+
+A DELETE request to URL with "products" as first the segment and a number in the second will be
+mapped to the ``Product::delete()`` method, passing the numeric value as the first parameter.
+
+Using HTTP verbs is of course, optional.
+
Reserved Routes
===============