mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-21 00:54:53 +08:00
Merge branch 'master' of github.com:yiisoft/yii2
This commit is contained in:
@@ -73,6 +73,7 @@ Yii Framework 2 Change Log
|
||||
- Enh #1973: `yii message/extract` is now able to generate `.po` files (SergeiKutanov, samdark)
|
||||
- Enh #1984: ActionFilter will now mark event as handled when action run is aborted (cebe)
|
||||
- Enh #2003: Added `filter` property to `ExistValidator` and `UniqueValidator` to support adding additional filtering conditions (qiangxue)
|
||||
- Enh #2043: Added support for custom request body parsers (danschmidt5189, cebe)
|
||||
- Enh #2051: Do not save null data into database when using RBAC (qiangxue)
|
||||
- Enh: Added `favicon.ico` and `robots.txt` to default application templates (samdark)
|
||||
- Enh: Added `Widget::autoIdPrefix` to support prefixing automatically generated widget IDs (qiangxue)
|
||||
|
||||
@@ -86,6 +86,8 @@ class Pagination extends Object
|
||||
* @var array parameters (name => value) that should be used to obtain the current page number
|
||||
* and to create new pagination URLs. If not set, all parameters from $_GET will be used instead.
|
||||
*
|
||||
* In order to add hash to all links use `array_merge($_GET, ['#' => 'my-hash'])`.
|
||||
*
|
||||
* The array element indexed by [[pageVar]] is considered to be the current page number.
|
||||
* If the element does not exist, the current page number is considered 0.
|
||||
*/
|
||||
|
||||
@@ -168,6 +168,8 @@ class Sort extends Object
|
||||
* @var array parameters (name => value) that should be used to obtain the current sort directions
|
||||
* and to create new sort URLs. If not set, $_GET will be used instead.
|
||||
*
|
||||
* In order to add hash to all links use `array_merge($_GET, ['#' => 'my-hash'])`.
|
||||
*
|
||||
* The array element indexed by [[sortVar]] is considered to be the current sort directions.
|
||||
* If the element does not exist, the [[defaults|default order]] will be used.
|
||||
*
|
||||
|
||||
@@ -47,6 +47,7 @@ class ActiveFixture extends BaseActiveFixture
|
||||
*/
|
||||
private $_table;
|
||||
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
|
||||
@@ -28,6 +28,7 @@ abstract class DbFixture extends Fixture
|
||||
*/
|
||||
public $db = 'db';
|
||||
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
|
||||
@@ -35,6 +35,7 @@ class Fixture extends Component
|
||||
*/
|
||||
public $depends = [];
|
||||
|
||||
|
||||
/**
|
||||
* Loads the fixture.
|
||||
* This method is called before performing every test method.
|
||||
|
||||
@@ -38,6 +38,7 @@ trait FixtureTrait
|
||||
*/
|
||||
private $_fixtureAliases;
|
||||
|
||||
|
||||
/**
|
||||
* Returns the value of an object property.
|
||||
*
|
||||
|
||||
@@ -34,7 +34,7 @@ class InitDbFixture extends DbFixture
|
||||
public $initScript = '@app/tests/fixtures/initdb.php';
|
||||
/**
|
||||
* @var array list of database schemas that the test tables may reside in. Defaults to
|
||||
* [''], meaning using the default schema (an empty string refers to the
|
||||
* `['']`, meaning using the default schema (an empty string refers to the
|
||||
* default schema). This property is mainly used when turning on and off integrity checks
|
||||
* so that fixture data can be populated into the database without causing problem.
|
||||
*/
|
||||
|
||||
49
framework/web/JsonParser.php
Normal file
49
framework/web/JsonParser.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
/**
|
||||
* @link http://www.yiiframework.com/
|
||||
* @copyright Copyright (c) 2008 Yii Software LLC
|
||||
* @license http://www.yiiframework.com/license/
|
||||
*/
|
||||
|
||||
namespace yii\web;
|
||||
|
||||
use yii\base\InvalidParamException;
|
||||
use yii\helpers\Json;
|
||||
|
||||
/**
|
||||
* Parses a raw HTTP request using [[yii\helpers\Json::decode()]]
|
||||
*
|
||||
* @author Dan Schmidt <danschmidt5189@gmail.com>
|
||||
* @since 2.0
|
||||
*/
|
||||
class JsonParser implements RequestParserInterface
|
||||
{
|
||||
/**
|
||||
* @var boolean whether to return objects in terms of associative arrays.
|
||||
*/
|
||||
public $asArray = true;
|
||||
/**
|
||||
* @var boolean whether to throw a [[BadRequestHttpException]] if the body is invalid json
|
||||
*/
|
||||
public $throwException = true;
|
||||
|
||||
|
||||
/**
|
||||
* Parses a HTTP request body.
|
||||
* @param string $rawBody the raw HTTP request body.
|
||||
* @param string $contentType the content type specified for the request body.
|
||||
* @return array parameters parsed from the request body
|
||||
* @throws BadRequestHttpException if the body contains invalid json and [[throwException]] is `true`.
|
||||
*/
|
||||
public function parse($rawBody, $contentType)
|
||||
{
|
||||
try {
|
||||
return Json::decode($rawBody, $this->asArray);
|
||||
} catch (InvalidParamException $e) {
|
||||
if ($this->throwException) {
|
||||
throw new BadRequestHttpException('Invalid JSON data in request body: ' . $e->getMessage(), 0, $e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -128,6 +128,26 @@ class Request extends \yii\base\Request
|
||||
* @see getRestParams()
|
||||
*/
|
||||
public $restVar = '_method';
|
||||
/**
|
||||
* @var array the parsers for converting the raw HTTP request body into [[restParams]].
|
||||
* The array keys are the request `Content-Types`, and the array values are the
|
||||
* corresponding configurations for [[Yii::createObject|creating the parser objects]].
|
||||
* A parser must implement the [[RequestParserInterface]].
|
||||
*
|
||||
* To enable parsing for JSON requests you can use the [[JsonParser]] class like in the following example:
|
||||
*
|
||||
* ```
|
||||
* [
|
||||
* 'application/json' => 'yii\web\JsonParser',
|
||||
* ]
|
||||
* ```
|
||||
*
|
||||
* To register a parser for parsing all request types you can use `'*'` as the array key.
|
||||
* This one will be used as a fallback in case no other types match.
|
||||
*
|
||||
* @see getRestParams()
|
||||
*/
|
||||
public $parsers = [];
|
||||
|
||||
private $_cookies;
|
||||
|
||||
@@ -249,16 +269,33 @@ class Request extends \yii\base\Request
|
||||
|
||||
/**
|
||||
* Returns the request parameters for the RESTful request.
|
||||
*
|
||||
* Request parameters are determined using the parsers configured in [[parsers]] property.
|
||||
* If no parsers are configured for the current [[contentType]] it uses the PHP function [[mb_parse_str()]]
|
||||
* to parse the [[rawBody|request body]].
|
||||
* @return array the RESTful request parameters
|
||||
* @throws \yii\base\InvalidConfigException if a registered parser does not implement the [[RequestParserInterface]].
|
||||
* @see getMethod()
|
||||
*/
|
||||
public function getRestParams()
|
||||
{
|
||||
if ($this->_restParams === null) {
|
||||
$contentType = $this->getContentType();
|
||||
if (isset($_POST[$this->restVar])) {
|
||||
$this->_restParams = $_POST;
|
||||
} elseif(strncmp($this->getContentType(), 'application/json', 16) === 0) {
|
||||
$this->_restParams = Json::decode($this->getRawBody(), true);
|
||||
unset($this->_restParams[$this->restVar]);
|
||||
} elseif (isset($this->parsers[$contentType])) {
|
||||
$parser = Yii::createObject($this->parsers[$contentType]);
|
||||
if (!($parser instanceof RequestParserInterface)) {
|
||||
throw new InvalidConfigException("The '$contentType' request parser is invalid. It must implement the yii\\web\\RequestParserInterface.");
|
||||
}
|
||||
$this->_restParams = $parser->parse($this->getRawBody(), $contentType);
|
||||
} elseif (isset($this->parsers['*'])) {
|
||||
$parser = Yii::createObject($this->parsers['*']);
|
||||
if (!($parser instanceof RequestParserInterface)) {
|
||||
throw new InvalidConfigException("The fallback request parser is invalid. It must implement the yii\\web\\RequestParserInterface.");
|
||||
}
|
||||
$this->_restParams = $parser->parse($this->getRawBody(), $contentType);
|
||||
} else {
|
||||
$this->_restParams = [];
|
||||
mb_parse_str($this->getRawBody(), $this->_restParams);
|
||||
|
||||
25
framework/web/RequestParserInterface.php
Normal file
25
framework/web/RequestParserInterface.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
/**
|
||||
* @link http://www.yiiframework.com/
|
||||
* @copyright Copyright (c) 2008 Yii Software LLC
|
||||
* @license http://www.yiiframework.com/license/
|
||||
*/
|
||||
|
||||
namespace yii\web;
|
||||
|
||||
/**
|
||||
* Interface for classes that parse the raw request body into a parameters array.
|
||||
*
|
||||
* @author Dan Schmidt <danschmidt5189@gmail.com>
|
||||
* @since 2.0
|
||||
*/
|
||||
interface RequestParserInterface
|
||||
{
|
||||
/**
|
||||
* Parses a HTTP request body.
|
||||
* @param string $rawBody the raw HTTP request body.
|
||||
* @param string $contentType the content type specified for the request body.
|
||||
* @return array parameters parsed from the request body
|
||||
*/
|
||||
public function parse($rawBody, $contentType);
|
||||
}
|
||||
Reference in New Issue
Block a user