Skip to content

Commit

Permalink
Added support for request method overriding via X-HTTP-Method-Override
Browse files Browse the repository at this point in the history
This type of override is supported by MS MVC3 and is recommended by Google.
Also added ability to override request method via ?_method= when
request is made via GET.
  • Loading branch information
Miha Vrhovnik committed May 19, 2011
1 parent 8268675 commit 65ed6f7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Symfony/Component/HttpFoundation/Request.php
Expand Up @@ -602,7 +602,9 @@ public function getMethod()
if (null === $this->method) {
$this->method = strtoupper($this->server->get('REQUEST_METHOD', 'GET'));
if ('POST' === $this->method) {
$this->method = strtoupper($this->request->get('_method', 'POST'));
$this->method = strtoupper($this->server->get('X-HTTP-METHOD-OVERRIDE', $this->request->get('_method', 'POST')));
} else if ('GET' === $this->method) {
$this->method = strtoupper($this->query->get('_method', 'GET'));
}
}

Expand Down
14 changes: 14 additions & 0 deletions tests/Symfony/Tests/Component/HttpFoundation/RequestTest.php
Expand Up @@ -453,6 +453,20 @@ public function testGetSetMethod()
$request->setMethod('POST');
$request->request->set('_method', 'purge');
$this->assertEquals('PURGE', $request->getMethod(), '->getMethod() returns the method from _method if defined and POST');

$request->setMethod('POST');
$request->server->set('X-HTTP-METHOD-OVERRIDE', 'delete');
$this->assertEquals('DELETE', $request->getMethod(), '->getMethod() returns the method from X-HTTP-Method-Override even though _method is set if defined and POST');

$request = new Request();
$request->setMethod('POST');
$request->server->set('X-HTTP-METHOD-OVERRIDE', 'delete');
$this->assertEquals('DELETE', $request->getMethod(), '->getMethod() returns the method from X-HTTP-Method-Override if defined and POST');

$request = new Request();
$request->setMethod('GET');
$request->query->set('_method', 'purge');
$this->assertEquals('PURGE', $request->getMethod(), '->getMethod() returns the method from _method if defined and GET');
}

/**
Expand Down

0 comments on commit 65ed6f7

Please sign in to comment.