diff --git a/Slim/Http/Request.php b/Slim/Http/Request.php index 2c413f083..43d2c5b79 100644 --- a/Slim/Http/Request.php +++ b/Slim/Http/Request.php @@ -48,6 +48,7 @@ class Request const METHOD_GET = 'GET'; const METHOD_POST = 'POST'; const METHOD_PUT = 'PUT'; + const METHOD_PATCH = 'PATCH'; const METHOD_DELETE = 'DELETE'; const METHOD_OPTIONS = 'OPTIONS'; const METHOD_OVERRIDE = '_METHOD'; @@ -124,6 +125,15 @@ public function isPut() return $this->getMethod() === self::METHOD_PUT; } + /** + * Is this a PATCH request? + * @return bool + */ + public function isPatch() + { + return $this->getMethod() === self::METHOD_PATCH; + } + /** * Is this a DELETE request? * @return bool @@ -275,6 +285,16 @@ public function put($key = null) return $this->post($key); } + /** + * Fetch PATCH data (alias for \Slim\Http\Request::post) + * @param string $key + * @return array|mixed|null + */ + public function patch($key = null) + { + return $this->post($key); + } + /** * Fetch DELETE data (alias for \Slim\Http\Request::post) * @param string $key diff --git a/Slim/Slim.php b/Slim/Slim.php index 367b1e8f4..786ab3c36 100644 --- a/Slim/Slim.php +++ b/Slim/Slim.php @@ -370,7 +370,7 @@ public function getLog() *******************************************************************************/ /** - * Add GET|POST|PUT|DELETE route + * Add GET|POST|PUT|PATCH|DELETE route * * Adds a new route to the router with associated callable. This * route will only be invoked when the HTTP request's method matches @@ -459,6 +459,18 @@ public function put() return $this->mapRoute($args)->via(\Slim\Http\Request::METHOD_PUT); } + /** + * Add PATCH route + * @see mapRoute() + * @return \Slim\Route + */ + public function patch() + { + $args = func_get_args(); + + return $this->mapRoute($args)->via(\Slim\Http\Request::METHOD_PATCH); + } + /** * Add DELETE route * @see mapRoute() @@ -689,7 +701,7 @@ public function view($viewClass = null) /** * Render a template * - * Call this method within a GET, POST, PUT, DELETE, NOT FOUND, or ERROR + * Call this method within a GET, POST, PUT, PATCH, DELETE, NOT FOUND, or ERROR * callable to render a template whose output is appended to the * current HTTP response body. How the template is rendered is * delegated to the current View. diff --git a/index.php b/index.php index a802b2b69..9bfd62d98 100644 --- a/index.php +++ b/index.php @@ -26,7 +26,7 @@ * * Here we define several Slim application routes that respond * to appropriate HTTP request methods. In this example, the second - * argument for `Slim::get`, `Slim::post`, `Slim::put`, and `Slim::delete` + * argument for `Slim::get`, `Slim::post`, `Slim::put`, `Slim::patch`, and `Slim::delete` * is an anonymous function. */ @@ -147,6 +147,11 @@ function () { } ); +// PATCH route +$app->patch('/patch', function () { + echo 'This is a PATCH route'; +}); + // DELETE route $app->delete( '/delete', diff --git a/tests/Http/RequestTest.php b/tests/Http/RequestTest.php index ed2f1b973..6879602e1 100644 --- a/tests/Http/RequestTest.php +++ b/tests/Http/RequestTest.php @@ -116,6 +116,18 @@ public function testIsHead() $this->assertTrue($req->isHead()); } + /** + * Test HTTP PATCH method detection + */ + public function testIsPatch() + { + $env = \Slim\Environment::mock(array( + 'REQUEST_METHOD' => 'PATCH', + )); + $req = new \Slim\Http\Request($env); + $this->assertTrue($req->isPatch()); + } + /** * Test AJAX method detection w/ header */ @@ -309,6 +321,24 @@ public function testPut() $this->assertNull($req->put('xyz')); } + /** + * Test fetch PATCH params + */ + public function testPatch() + { + $env = \Slim\Environment::mock(array( + 'REQUEST_METHOD' => 'PATCH', + 'slim.input' => 'foo=bar&abc=123', + 'CONTENT_TYPE' => 'application/x-www-form-urlencoded', + 'CONTENT_LENGTH' => 15 + )); + $req = new \Slim\Http\Request($env); + $this->assertEquals(2, count($req->patch())); + $this->assertEquals('bar', $req->patch('foo')); + $this->assertEquals('bar', $req->params('foo')); + $this->assertNull($req->patch('xyz')); + } + /** * Test fetch DELETE params */ diff --git a/tests/SlimTest.php b/tests/SlimTest.php index 692a86d65..a7a907d00 100644 --- a/tests/SlimTest.php +++ b/tests/SlimTest.php @@ -371,6 +371,27 @@ public function testPutRoute() $this->assertSame($callable, $route->getCallable()); } + /** + * Test PATCH route + */ + public function testPatchRoute() + { + \Slim\Environment::mock(array( + 'REQUEST_METHOD' => 'PATCH', + 'SCRIPT_NAME' => '/foo', //<-- Physical + 'PATH_INFO' => '/bar', //<-- Virtual + )); + $s = new \Slim\Slim(); + $mw1 = function () { echo "foo"; }; + $mw2 = function () { echo "bar"; }; + $callable = function () { echo "xyz"; }; + $route = $s->patch('/bar', $mw1, $mw2, $callable); + $s->call(); + $this->assertEquals('foobarxyz', $s->response()->body()); + $this->assertEquals('/bar', $route->getPattern()); + $this->assertSame($callable, $route->getCallable()); + } + /** * Test DELETE route */