Commit 6ce60c16 by 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
*/
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'.
* @see getMethod
* @see getRestParams
......@@ -73,8 +73,8 @@ class Request extends \yii\base\Request
}
/**
* Returns the method of the current request (e.g. GET, POST, HEAD, PUT, DELETE).
* @return string request method, such as 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, PATCH, DELETE.
* The value returned is turned into upper case.
*/
public function getMethod()
......@@ -114,6 +114,15 @@ class Request extends \yii\base\Request
}
/**
* 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.
* @return boolean whether this is an AJAX (XMLHttpRequest) request.
*/
......@@ -239,6 +248,17 @@ class Request extends \yii\base\Request
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;
/**
......@@ -914,7 +934,7 @@ class Request extends \yii\base\Request
return;
}
$method = $this->getMethod();
if ($method === 'POST' || $method === 'PUT' || $method === 'DELETE') {
if ($method === 'POST' || $method === 'PUT' || $method === 'PATCH' || $method === 'DELETE') {
$cookies = $this->getCookies();
switch ($method) {
case 'POST':
......@@ -923,6 +943,9 @@ class Request extends \yii\base\Request
case 'PUT':
$token = $this->getPut($this->csrfTokenName);
break;
case 'PATCH':
$token = $this->getPatch($this->csrfTokenName);
break;
case 'DELETE':
$token = $this->getDelete($this->csrfTokenName);
}
......
......@@ -49,7 +49,7 @@ class UrlManager extends Component
* For example, `'PUT post/<id:\d+>' => 'post/update'`.
* You may specify multiple verbs by separating them with comma
* 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
* so you normally would not specify a verb for normal GET request.
*
......@@ -142,7 +142,7 @@ class UrlManager extends Component
$rule = array(
'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['mode'] = UrlRule::PARSING_ONLY;
$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