Browse Source

Added support for rest to Html.

tags/2.0.0-beta
Qiang Xue 12 years ago
parent
commit
7dd1f884c4
  1. 29
      framework/helpers/base/Html.php
  2. 7
      framework/web/Request.php

29
framework/helpers/base/Html.php

@ -9,6 +9,7 @@ namespace yii\helpers\base;
use Yii;
use yii\base\InvalidParamException;
use yii\web\Request;
/**
* Html provides a set of static methods for generating commonly used HTML tags.
@ -276,7 +277,10 @@ class Html
/**
* Generates a form start tag.
* @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
* 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.
@ -287,15 +291,24 @@ class Html
{
$action = static::url($action);
// query parameters in the action are ignored for GET method
// we use hidden fields to add them back
$hiddens = array();
$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
// we use hidden fields to add them back
foreach (explode('&', substr($action, $pos + 1)) as $pair) {
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 {
$hiddens[] = static::hiddenInput(urldecode($pair), '');
$hiddenInputs[] = static::hiddenInput(urldecode($pair), '');
}
}
$action = substr($action, 0, $pos);
@ -304,8 +317,8 @@ class Html
$options['action'] = $action;
$options['method'] = $method;
$form = static::beginTag('form', $options);
if ($hiddens !== array()) {
$form .= "\n" . implode("\n", $hiddens);
if ($hiddenInputs !== array()) {
$form .= "\n" . implode("\n", $hiddenInputs);
}
return $form;

7
framework/web/Request.php

@ -27,8 +27,7 @@ class Request extends \yii\base\Request
public $cookieValidationKey;
/**
* @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.
* Default to '_method'.
* request tunneled through POST. Default to '_method'.
* @see getRequestMethod
* @see getRestParams
*/
@ -60,7 +59,7 @@ class Request extends \yii\base\Request
*/
public function getRequestMethod()
{
if ($this->restVar !== false && isset($_POST[$this->restVar])) {
if (isset($_POST[$this->restVar])) {
return strtoupper($_POST[$this->restVar]);
} else {
return isset($_SERVER['REQUEST_METHOD']) ? strtoupper($_SERVER['REQUEST_METHOD']) : 'GET';
@ -123,7 +122,7 @@ class Request extends \yii\base\Request
public function getRestParams()
{
if ($this->_restParams === null) {
if ($this->restVar !== false && isset($_POST[$this->restVar])) {
if (isset($_POST[$this->restVar])) {
$this->_restParams = $_POST;
} else {
$this->_restParams = array();

Loading…
Cancel
Save