From 442b50b0c78a8c630b139d3a9a30d3b731253565 Mon Sep 17 00:00:00 2001 From: Fatih Kalifa Date: Fri, 1 Nov 2013 02:44:56 +0700 Subject: 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 --- system/core/Router.php | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) 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); -- cgit v1.2.3-24-g4f1b From 0b58b3cad456efc5ecce89f622876c6b715439c2 Mon Sep 17 00:00:00 2001 From: Fatih Kalifa Date: Tue, 5 Nov 2013 15:36:40 +0700 Subject: Fix HTTP Verb Routing Rules Fix code style, removed (:any) rule in http verb to avoid confusion, and add proposed documentation and changelog --- system/core/Router.php | 32 ++++++++++++--------------- user_guide_src/source/changelog.rst | 1 + user_guide_src/source/general/routing.rst | 36 +++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 18 deletions(-) diff --git a/system/core/Router.php b/system/core/Router.php index 89d11afab..9071f84b7 100644 --- a/system/core/Router.php +++ b/system/core/Router.php @@ -349,38 +349,34 @@ class CI_Router { $http_verb = strtolower($_SERVER['REQUEST_METHOD']); // Is there a literal match? If so we're done - if (isset($this->routes[$uri][$http_verb])) + if (isset($this->routes[$uri])) { - 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])); + // 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 HTTP Verb is exist + // Check if route format is using http verb if (is_array($val)) { - // HTTP verb included in routes + // Does the http verb match? if (isset($val[$http_verb])) { $val = $val[$http_verb]; } - else if (isset($val['(:any)'])) - { - $val = $val['(:any)']; - } + // No match, skip to next rule else { - // HTTP Verb not found continue; } } diff --git a/user_guide_src/source/changelog.rst b/user_guide_src/source/changelog.rst index 4a45a71b0..90229d206 100644 --- a/user_guide_src/source/changelog.rst +++ b/user_guide_src/source/changelog.rst @@ -420,6 +420,7 @@ Release Date: Not Released - :doc:`URI 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 =============== -- cgit v1.2.3-24-g4f1b From fdd1461d98d2b28af282ea676e85a656341f02f3 Mon Sep 17 00:00:00 2001 From: vkeranov Date: Fri, 8 Nov 2013 09:19:29 +0200 Subject: Update user_agents.php Added Ubuntu Web Browser. More info here: http://www.omgubuntu.co.uk/2013/11/ubuntu-touch-browser-finally-given-unique-user-agent-string --- application/config/user_agents.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/application/config/user_agents.php b/application/config/user_agents.php index 899e96a94..0686bf972 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( @@ -223,4 +224,4 @@ $robots = array( ); /* End of file user_agents.php */ -/* Location: ./application/config/user_agents.php */ \ No newline at end of file +/* Location: ./application/config/user_agents.php */ -- cgit v1.2.3-24-g4f1b From c761a206def7714d18623d46b05adc2bbeedce21 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Mon, 11 Nov 2013 14:02:15 +0200 Subject: Polish changes from PR #2712 --- application/config/user_agents.php | 2 +- system/core/Router.php | 6 ++--- user_guide_src/source/changelog.rst | 2 +- user_guide_src/source/general/routing.rst | 37 ++++++++++--------------------- 4 files changed, 16 insertions(+), 31 deletions(-) diff --git a/application/config/user_agents.php b/application/config/user_agents.php index 0686bf972..0953deafd 100644 --- a/application/config/user_agents.php +++ b/application/config/user_agents.php @@ -224,4 +224,4 @@ $robots = array( ); /* End of file user_agents.php */ -/* Location: ./application/config/user_agents.php */ +/* Location: ./application/config/user_agents.php */ \ No newline at end of file diff --git a/system/core/Router.php b/system/core/Router.php index 9071f84b7..d467d60fd 100644 --- a/system/core/Router.php +++ b/system/core/Router.php @@ -346,7 +346,7 @@ class CI_Router { $uri = implode('/', $this->uri->segments); // Get HTTP verb - $http_verb = strtolower($_SERVER['REQUEST_METHOD']); + $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])) @@ -356,7 +356,7 @@ class CI_Router { { return $this->_set_request(explode('/', $this->routes[$uri])); } - // Is there any matching http verb? + // 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])); @@ -369,12 +369,10 @@ class CI_Router { // 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; diff --git a/user_guide_src/source/changelog.rst b/user_guide_src/source/changelog.rst index fbea04c43..5fc86b1b5 100644 --- a/user_guide_src/source/changelog.rst +++ b/user_guide_src/source/changelog.rst @@ -429,7 +429,7 @@ Release Date: Not Released - :doc:`URI Routing ` changes include: - - Added possibility to route requests using HTTP Verb + - 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 6495f1ad4..0b91d3fa9 100644 --- a/user_guide_src/source/general/routing.rst +++ b/user_guide_src/source/general/routing.rst @@ -142,41 +142,28 @@ routing rules to process the back-references. Example:: return 'catalog/product_edit/' . strtolower($product_type) . '/' . $id; }; -Using HTTP Verb in Routes -========================= +Using HTTP verbs 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:: +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" controller -class and "insert" method +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 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'; +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. -This way, all incoming request using any HTTP method containing the word "product" -in the first segment will be remapped to "product" class +Using HTTP verbs is of course, optional. Reserved Routes =============== -- cgit v1.2.3-24-g4f1b