Commit 6ce60c16 authored by Carsten Brandt's avatar Carsten Brandt

added support for HTTP verb PATCH

To provider better REST API support PATCH method is added which is used
for partial updates, while a PUT should only update whole record.

issues #303 and yiisoft/yii#2664
parent 9aa7b261
...@@ -43,7 +43,7 @@ class Request extends \yii\base\Request ...@@ -43,7 +43,7 @@ class Request extends \yii\base\Request
*/ */
public $enableCookieValidation = true; public $enableCookieValidation = true;
/** /**
* @var string|boolean the name of the POST parameter that is used to indicate if a request is a PUT or DELETE * @var string|boolean the name of the POST parameter that is used to indicate if a request is a PUT, PATCH or DELETE
* request tunneled through POST. Default to '_method'. * request tunneled through POST. Default to '_method'.
* @see getMethod * @see getMethod
* @see getRestParams * @see getRestParams
...@@ -73,8 +73,8 @@ class Request extends \yii\base\Request ...@@ -73,8 +73,8 @@ class Request extends \yii\base\Request
} }
/** /**
* Returns the method of the current request (e.g. GET, POST, HEAD, PUT, DELETE). * Returns the method of the current request (e.g. GET, POST, HEAD, PUT, PATCH, DELETE).
* @return string request method, such as GET, POST, HEAD, PUT, DELETE. * @return string request method, such as GET, POST, HEAD, PUT, PATCH, DELETE.
* The value returned is turned into upper case. * The value returned is turned into upper case.
*/ */
public function getMethod() public function getMethod()
...@@ -113,6 +113,15 @@ class Request extends \yii\base\Request ...@@ -113,6 +113,15 @@ class Request extends \yii\base\Request
return $this->getMethod() === 'PUT'; return $this->getMethod() === 'PUT';
} }
/**
* Returns whether this is a PATCH request.
* @return boolean whether this is a PATCH request.
*/
public function getIsPatch()
{
return $this->getMethod() === 'PATCH';
}
/** /**
* Returns whether this is an AJAX (XMLHttpRequest) request. * Returns whether this is an AJAX (XMLHttpRequest) request.
* @return boolean whether this is an AJAX (XMLHttpRequest) request. * @return boolean whether this is an AJAX (XMLHttpRequest) request.
...@@ -239,6 +248,17 @@ class Request extends \yii\base\Request ...@@ -239,6 +248,17 @@ class Request extends \yii\base\Request
return $this->getIsPut() ? $this->getRestParam($name, $defaultValue) : null; return $this->getIsPut() ? $this->getRestParam($name, $defaultValue) : null;
} }
/**
* Returns the named PATCH parameter value.
* @param string $name the PATCH parameter name
* @param mixed $defaultValue the default parameter value if the PATCH parameter does not exist.
* @return mixed the PATCH parameter value
*/
public function getPatch($name, $defaultValue = null)
{
return $this->getIsPatch() ? $this->getRestParam($name, $defaultValue) : null;
}
private $_hostInfo; private $_hostInfo;
/** /**
...@@ -914,7 +934,7 @@ class Request extends \yii\base\Request ...@@ -914,7 +934,7 @@ class Request extends \yii\base\Request
return; return;
} }
$method = $this->getMethod(); $method = $this->getMethod();
if ($method === 'POST' || $method === 'PUT' || $method === 'DELETE') { if ($method === 'POST' || $method === 'PUT' || $method === 'PATCH' || $method === 'DELETE') {
$cookies = $this->getCookies(); $cookies = $this->getCookies();
switch ($method) { switch ($method) {
case 'POST': case 'POST':
...@@ -923,6 +943,9 @@ class Request extends \yii\base\Request ...@@ -923,6 +943,9 @@ class Request extends \yii\base\Request
case 'PUT': case 'PUT':
$token = $this->getPut($this->csrfTokenName); $token = $this->getPut($this->csrfTokenName);
break; break;
case 'PATCH':
$token = $this->getPatch($this->csrfTokenName);
break;
case 'DELETE': case 'DELETE':
$token = $this->getDelete($this->csrfTokenName); $token = $this->getDelete($this->csrfTokenName);
} }
......
...@@ -49,7 +49,7 @@ class UrlManager extends Component ...@@ -49,7 +49,7 @@ class UrlManager extends Component
* For example, `'PUT post/<id:\d+>' => 'post/update'`. * For example, `'PUT post/<id:\d+>' => 'post/update'`.
* You may specify multiple verbs by separating them with comma * You may specify multiple verbs by separating them with comma
* like this: `'POST,PUT post/index' => 'post/create'`. * like this: `'POST,PUT post/index' => 'post/create'`.
* The supported verbs in the shortcut format are: GET, HEAD, POST, PUT and DELETE. * The supported verbs in the shortcut format are: GET, HEAD, POST, PUT, PATCH and DELETE.
* Note that [[UrlRule::mode|mode]] will be set to PARSING_ONLY when specifying verb in this way * Note that [[UrlRule::mode|mode]] will be set to PARSING_ONLY when specifying verb in this way
* so you normally would not specify a verb for normal GET request. * so you normally would not specify a verb for normal GET request.
* *
...@@ -142,7 +142,7 @@ class UrlManager extends Component ...@@ -142,7 +142,7 @@ class UrlManager extends Component
$rule = array( $rule = array(
'route' => $rule, 'route' => $rule,
); );
if (preg_match('/^((?:(GET|HEAD|POST|PUT|DELETE),)*(GET|HEAD|POST|PUT|DELETE))\s+(.*)$/', $key, $matches)) { if (preg_match('/^((?:(GET|HEAD|POST|PUT|PATCH|DELETE),)*(GET|HEAD|POST|PUT|PATCH|DELETE))\s+(.*)$/', $key, $matches)) {
$rule['verb'] = explode(',', $matches[1]); $rule['verb'] = explode(',', $matches[1]);
$rule['mode'] = UrlRule::PARSING_ONLY; $rule['mode'] = UrlRule::PARSING_ONLY;
$key = $matches[4]; $key = $matches[4];
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment