Commit 7dd1f884 by Qiang Xue

Added support for rest to Html.

parent f21499dd
...@@ -9,6 +9,7 @@ namespace yii\helpers\base; ...@@ -9,6 +9,7 @@ namespace yii\helpers\base;
use Yii; use Yii;
use yii\base\InvalidParamException; use yii\base\InvalidParamException;
use yii\web\Request;
/** /**
* Html provides a set of static methods for generating commonly used HTML tags. * Html provides a set of static methods for generating commonly used HTML tags.
...@@ -276,7 +277,10 @@ class Html ...@@ -276,7 +277,10 @@ class Html
/** /**
* Generates a form start tag. * Generates a form start tag.
* @param array|string $action the form action URL. This parameter will be processed by [[url()]]. * @param array|string $action the form action URL. This parameter will be processed by [[url()]].
* @param string $method the form submission method, either "post" or "get" (case-insensitive) * @param string $method the form submission method, such as "post", "get", "put", "delete" (case-insensitive).
* Since most browsers only support "post" and "get", if other methods are given, they will
* be simulated using "post", and a hidden input will be added which contains the actual method type.
* See [[\yii\web\Request::restVar]] for more details.
* @param array $options the tag options in terms of name-value pairs. These will be rendered as * @param array $options the tag options in terms of name-value pairs. These will be rendered as
* the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. * the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]].
* If a value is null, the corresponding attribute will not be rendered. * If a value is null, the corresponding attribute will not be rendered.
...@@ -287,15 +291,24 @@ class Html ...@@ -287,15 +291,24 @@ class Html
{ {
$action = static::url($action); $action = static::url($action);
$hiddenInputs = array();
if (strcasecmp($method, 'get') && strcasecmp($method, 'post')) {
// simulate PUT, DELETE, etc. via POST
if (($request = Yii::$app->getRequest()) instanceof Request) {
$hiddenInputs[] = static::hiddenInput($request->restVar, $method);
$method = 'post';
}
}
if (!strcasecmp($method, 'get') && ($pos = strpos($action, '?')) !== false) {
// query parameters in the action are ignored for GET method // query parameters in the action are ignored for GET method
// we use hidden fields to add them back // we use hidden fields to add them back
$hiddens = array();
if (!strcasecmp($method, 'get') && ($pos = strpos($action, '?')) !== false) {
foreach (explode('&', substr($action, $pos + 1)) as $pair) { foreach (explode('&', substr($action, $pos + 1)) as $pair) {
if (($pos1 = strpos($pair, '=')) !== false) { if (($pos1 = strpos($pair, '=')) !== false) {
$hiddens[] = static::hiddenInput(urldecode(substr($pair, 0, $pos1)), urldecode(substr($pair, $pos1 + 1))); $hiddenInputs[] = static::hiddenInput(urldecode(substr($pair, 0, $pos1)), urldecode(substr($pair, $pos1 + 1)));
} else { } else {
$hiddens[] = static::hiddenInput(urldecode($pair), ''); $hiddenInputs[] = static::hiddenInput(urldecode($pair), '');
} }
} }
$action = substr($action, 0, $pos); $action = substr($action, 0, $pos);
...@@ -304,8 +317,8 @@ class Html ...@@ -304,8 +317,8 @@ class Html
$options['action'] = $action; $options['action'] = $action;
$options['method'] = $method; $options['method'] = $method;
$form = static::beginTag('form', $options); $form = static::beginTag('form', $options);
if ($hiddens !== array()) { if ($hiddenInputs !== array()) {
$form .= "\n" . implode("\n", $hiddens); $form .= "\n" . implode("\n", $hiddenInputs);
} }
return $form; return $form;
......
...@@ -27,8 +27,7 @@ class Request extends \yii\base\Request ...@@ -27,8 +27,7 @@ class Request extends \yii\base\Request
public $cookieValidationKey; public $cookieValidationKey;
/** /**
* @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 or DELETE
* request tunneled through POST. If false, it means disabling REST request tunneled through POST. * request tunneled through POST. Default to '_method'.
* Default to '_method'.
* @see getRequestMethod * @see getRequestMethod
* @see getRestParams * @see getRestParams
*/ */
...@@ -60,7 +59,7 @@ class Request extends \yii\base\Request ...@@ -60,7 +59,7 @@ class Request extends \yii\base\Request
*/ */
public function getRequestMethod() public function getRequestMethod()
{ {
if ($this->restVar !== false && isset($_POST[$this->restVar])) { if (isset($_POST[$this->restVar])) {
return strtoupper($_POST[$this->restVar]); return strtoupper($_POST[$this->restVar]);
} else { } else {
return isset($_SERVER['REQUEST_METHOD']) ? strtoupper($_SERVER['REQUEST_METHOD']) : 'GET'; return isset($_SERVER['REQUEST_METHOD']) ? strtoupper($_SERVER['REQUEST_METHOD']) : 'GET';
...@@ -123,7 +122,7 @@ class Request extends \yii\base\Request ...@@ -123,7 +122,7 @@ class Request extends \yii\base\Request
public function getRestParams() public function getRestParams()
{ {
if ($this->_restParams === null) { if ($this->_restParams === null) {
if ($this->restVar !== false && isset($_POST[$this->restVar])) { if (isset($_POST[$this->restVar])) {
$this->_restParams = $_POST; $this->_restParams = $_POST;
} else { } else {
$this->_restParams = array(); $this->_restParams = array();
......
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