mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
Merge pull request #8065 from cakephp/2.7-method-override
2.7- Better method overriding emulation for GET
This commit is contained in:
commit
1b2fcda4d2
2 changed files with 28 additions and 0 deletions
|
@ -173,9 +173,13 @@ class CakeRequest implements ArrayAccess {
|
|||
if (ini_get('magic_quotes_gpc') === '1') {
|
||||
$this->data = stripslashes_deep($this->data);
|
||||
}
|
||||
|
||||
$override = null;
|
||||
if (env('HTTP_X_HTTP_METHOD_OVERRIDE')) {
|
||||
$this->data['_method'] = env('HTTP_X_HTTP_METHOD_OVERRIDE');
|
||||
$override = $this->data['_method'];
|
||||
}
|
||||
|
||||
$isArray = is_array($this->data);
|
||||
if ($isArray && isset($this->data['_method'])) {
|
||||
if (!empty($_SERVER)) {
|
||||
|
@ -183,8 +187,14 @@ class CakeRequest implements ArrayAccess {
|
|||
} else {
|
||||
$_ENV['REQUEST_METHOD'] = $this->data['_method'];
|
||||
}
|
||||
$override = $this->data['_method'];
|
||||
unset($this->data['_method']);
|
||||
}
|
||||
|
||||
if ($override && !in_array($override, array('POST', 'PUT', 'PATCH', 'DELETE'))) {
|
||||
$this->data = array();
|
||||
}
|
||||
|
||||
if ($isArray && isset($this->data['data'])) {
|
||||
$data = $this->data['data'];
|
||||
if (count($this->data) <= 1) {
|
||||
|
|
|
@ -2444,6 +2444,24 @@ XML;
|
|||
$request->allowMethod('POST');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that overriding the method to GET will clean all request
|
||||
* data, to better simulate a GET request.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testMethodOverrideEmptyData() {
|
||||
$_POST = array('_method' => 'GET', 'foo' => 'bar');
|
||||
$_SERVER['REQUEST_METHOD'] = 'PUT';
|
||||
$request = new CakeRequest('/posts/edit/1');
|
||||
$this->assertEmpty($request->data);
|
||||
|
||||
$_POST = array('foo' => 'bar');
|
||||
$_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'] = 'GET';
|
||||
$request = new CakeRequest('/posts/edit/1');
|
||||
$this->assertEmpty($request->data);
|
||||
}
|
||||
|
||||
/**
|
||||
* loadEnvironment method
|
||||
*
|
||||
|
|
Loading…
Reference in a new issue