Skip to content

Commit

Permalink
Add HTTP PATCH support
Browse files Browse the repository at this point in the history
  • Loading branch information
StyxUA authored and 16 committed May 13, 2013
1 parent 0e66382 commit 334fa42
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
8 changes: 7 additions & 1 deletion README.mkd
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ So they make the glue between an URL + a HTTP method, and the code provided in a
// Delete something
}

dispatch_patch('/', 'my_patch_function');
function my_patch_function()
{
// Patch something
}


Routes are matched in the order they are declared.
The search is performed with a path given through browser URL:
Expand All @@ -75,7 +81,7 @@ The search is performed with a path given through browser URL:
http://localhost/my_app/index.php?/my/path
http://localhost/my_app/?/my/path

When `PUT` or `DELETE` methods are not supported (like in HTML form submision), you can use the `_method` parameter in `POST` requests: it will override the `POST` method.
When `PUT`,`DELETE` or `PATCH` methods are not supported (like in HTML form submision), you can use the `_method` parameter in `POST` requests: it will override the `POST` method.

<form action="<?php echo url_for('profile_update'); ?>" method="post">
<p><input type="hidden" name="_method" value="PUT" id="_method"></p>
Expand Down
28 changes: 26 additions & 2 deletions lib/limonade.php
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ function stop_and_exit($exit = true)
* Returns limonade environment variables:
*
* 'SERVER', 'FILES', 'REQUEST', 'SESSION', 'ENV', 'COOKIE',
* 'GET', 'POST', 'PUT', 'DELETE'
* 'GET', 'POST', 'PUT', 'DELETE', 'PATCH'
*
* If a null argument is passed, reset and rebuild environment
*
Expand Down Expand Up @@ -977,14 +977,25 @@ function request_is_head($env = null)
return request_method($env) == "HEAD";
}

/**
* Checks if request method is PATCH
*
* @param string $env
* @return bool
*/
function request_is_patch($env = null)
{
return request_method($env) == "PATCH";
}

/**
* Returns allowed request methods
*
* @return array
*/
function request_methods()
{
return array("GET","POST","PUT","DELETE", "HEAD");
return array("GET","POST","PUT","DELETE","HEAD","PATCH");
}

/**
Expand Down Expand Up @@ -1163,6 +1174,19 @@ function dispatch_delete($path_or_array, $callback, $options = array())
route("DELETE", $path_or_array, $callback, $options);
}

/**
* Add a PATCH route
*
* @param string $path_or_array
* @param string $callback
* @param array $options (optional). See {@link route()} for available options.
* @return void
*/
function dispatch_patch($path_or_array, $callback, $options = array())
{
route("PATCH", $path_or_array, $callback, $options);
}


/**
* Add route if required params are provided.
Expand Down
6 changes: 5 additions & 1 deletion tests/request.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function before_each_test_in_request()
function test_request_methods()
{
$m = request_methods();
assert_length_of($m, 5);
assert_length_of($m, 6);
}

function test_request_method_is_allowed()
Expand All @@ -22,6 +22,7 @@ function test_request_method_is_allowed()
assert_true(request_method_is_allowed("PUT"));
assert_true(request_method_is_allowed("DELETE"));
assert_true(request_method_is_allowed("HEAD"));
assert_true(request_method_is_allowed("PATCH"));
}

function test_request_method()
Expand All @@ -47,6 +48,9 @@ function test_request_method()
$env['POST']['_method'] = "DELETE";
assert_equal(request_method($env), "DELETE");

$env['POST']['_method'] = "PATCH";
assert_equal(request_method($env), "PATCH");

$env['POST']['_method'] = "UNKOWN";
assert_trigger_error('request_method', array($env));
assert_false(request_method());
Expand Down

0 comments on commit 334fa42

Please sign in to comment.