Skip to content

Commit

Permalink
Polish changes from PR #2712
Browse files Browse the repository at this point in the history
  • Loading branch information
narfbg committed Nov 11, 2013
1 parent 61c124b commit c761a20
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 31 deletions.
2 changes: 1 addition & 1 deletion application/config/user_agents.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,4 +224,4 @@
);

/* End of file user_agents.php */
/* Location: ./application/config/user_agents.php */
/* Location: ./application/config/user_agents.php */
6 changes: 2 additions & 4 deletions system/core/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ protected function _parse_routes()
$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]))
Expand All @@ -356,7 +356,7 @@ protected function _parse_routes()
{
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]));
Expand All @@ -369,12 +369,10 @@ protected function _parse_routes()
// 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;
Expand Down
2 changes: 1 addition & 1 deletion user_guide_src/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +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 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.
Expand Down
37 changes: 12 additions & 25 deletions user_guide_src/source/general/routing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
===============
Expand Down

0 comments on commit c761a20

Please sign in to comment.