Commit 664d2aa0 by Qiang Xue

Supported adding a new response formatter without the need to reconfigure existing formatters

parent 5fbb65ad
......@@ -13,6 +13,7 @@ Yii Framework 2 Change Log
- Enh #3108: Added `yii\debug\Module::enableDebugLogs` to disable logging debug logs by default (qiangxue)
- Enh #3132: `yii\rbac\PhpManager` now supports more compact data file format (qiangxue)
- Enh: Added support for using sub-queries when building a DB query with `IN` condition (qiangxue)
- Enh: Supported adding a new response formatter without the need to reconfigure existing formatters (qiangxue)
- Chg: Replaced `clearAll()` and `clearAllAssignments()` in `yii\rbac\ManagerInterface` with `removeAll()`, `removeAllRoles()`, `removeAllPermissions()`, `removeAllRules()` and `removeAllAssignments()` (qiangxue)
......
......@@ -118,15 +118,7 @@ class Response extends \yii\base\Response
* for creating the formatter objects.
* @see format
*/
public $formatters = [
self::FORMAT_HTML => 'yii\web\HtmlResponseFormatter',
self::FORMAT_XML => 'yii\web\XmlResponseFormatter',
self::FORMAT_JSON => 'yii\web\JsonResponseFormatter',
self::FORMAT_JSONP => [
'class' => 'yii\web\JsonResponseFormatter',
'useJsonp' => true,
],
];
public $formatters = [];
/**
* @var mixed the original response data. When this is not null, it will be converted into [[content]]
* according to [[format]] when the response is being sent out.
......@@ -259,6 +251,8 @@ class Response extends \yii\base\Response
if ($this->charset === null) {
$this->charset = Yii::$app->charset;
}
$formatters = $this->defaultFormatters();
$this->formatters = empty($this->formatters) ? $formatters : array_merge($formatters, $this->formatters);
}
/**
......@@ -841,6 +835,22 @@ class Response extends \yii\base\Response
}
/**
* @return array the formatters that are supported by default
*/
protected function defaultFormatters()
{
return [
self::FORMAT_HTML => 'yii\web\HtmlResponseFormatter',
self::FORMAT_XML => 'yii\web\XmlResponseFormatter',
self::FORMAT_JSON => 'yii\web\JsonResponseFormatter',
self::FORMAT_JSONP => [
'class' => 'yii\web\JsonResponseFormatter',
'useJsonp' => true,
],
];
}
/**
* Prepares for sending the response.
* The default implementation will convert [[data]] into [[content]] and set headers accordingly.
* @throws InvalidConfigException if the formatter for the specified format is invalid or [[format]] is not supported
......
......@@ -41,6 +41,9 @@ use yii\helpers\Url;
*/
class View extends \yii\base\View
{
/**
* @event Event an event that is triggered by [[beginBody()]].
*/
const EVENT_BEGIN_BODY = 'beginBody';
/**
* @event Event an event that is triggered by [[endBody()]].
......
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