From 6ce60c166617fe4725a663507125b1a40ec36103 Mon Sep 17 00:00:00 2001 From: Carsten Brandt Date: Sat, 20 Jul 2013 22:46:32 +0200 Subject: [PATCH] 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 --- framework/yii/web/Request.php | 31 +++++++++++++++++++++++++++---- framework/yii/web/UrlManager.php | 4 ++-- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/framework/yii/web/Request.php b/framework/yii/web/Request.php index ee716f1..03d37bc 100644 --- a/framework/yii/web/Request.php +++ b/framework/yii/web/Request.php @@ -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); } diff --git a/framework/yii/web/UrlManager.php b/framework/yii/web/UrlManager.php index e3ac986..11e6f42 100644 --- a/framework/yii/web/UrlManager.php +++ b/framework/yii/web/UrlManager.php @@ -49,7 +49,7 @@ class UrlManager extends Component * For example, `'PUT post/' => '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];