Implemented shortcut methods for returning JSON and XML (#13101)

fixes #13036
This commit is contained in:
Carsten Brandt
2016-12-01 10:33:49 +01:00
committed by Boudewijn Vahrmeijer
parent 17e2c16836
commit 081ee7c5b1
3 changed files with 90 additions and 0 deletions

View File

@ -47,6 +47,7 @@ Yii Framework 2 Change Log
- Enh #12881: Added `removeValue` method to `yii\helpers\BaseArrayHelper` (nilsburg) - Enh #12881: Added `removeValue` method to `yii\helpers\BaseArrayHelper` (nilsburg)
- Enh #12901: Added `getDefaultHelpHeader` method to the `yii\console\controllers\HelpController` class to be able to override default help header in a class heir (diezztsk) - Enh #12901: Added `getDefaultHelpHeader` method to the `yii\console\controllers\HelpController` class to be able to override default help header in a class heir (diezztsk)
- Enh #13035: Use ArrayHelper::getValue() in SluggableBehavior::getValue() (thyseus) - Enh #13035: Use ArrayHelper::getValue() in SluggableBehavior::getValue() (thyseus)
- Enh #13036: Added shortcut methods `asJson()` and `asXml()` for returning JSON and XML data in web controller actions (cebe)
- Enh #13020: Added `disabledListItemSubTagOptions` attribute for `yii\widgets\LinkPager` in order to customize the disabled list item sub tag element (nadar) - Enh #13020: Added `disabledListItemSubTagOptions` attribute for `yii\widgets\LinkPager` in order to customize the disabled list item sub tag element (nadar)
- Enh #12988: Changed `textarea` method within the `yii\helpers\BaseHtml` class to allow users to control whether html entities found within `$value` will be double-encoded or not (cyphix333) - Enh #12988: Changed `textarea` method within the `yii\helpers\BaseHtml` class to allow users to control whether html entities found within `$value` will be double-encoded or not (cyphix333)
- Enh #13074: Improved `\yii\log\SyslogTarget` with `$options` to be able to change the default `openlog` options. (timbeks) - Enh #13074: Improved `\yii\log\SyslogTarget` with `$options` to be able to change the default `openlog` options. (timbeks)

View File

@ -49,6 +49,60 @@ class Controller extends \yii\base\Controller
return $this->getView()->renderAjax($view, $params, $this); return $this->getView()->renderAjax($view, $params, $this);
} }
/**
* Send data formatted as JSON.
*
* This method is a shortcut for sending data formatted as JSON. It will return
* the [[Application::getResponse()|response]] application component after configuring
* the [[Response::$format|format]] and setting the [[Response::$data|data]] that should
* be formatted. A common usage will be:
*
* ```php
* return $this->asJson($data);
* ```
*
* @param mixed $data the data that should be formatted.
* @return Response a response that is configured to send `$data` formatted as JSON.
* @since 2.0.11
* @see Response::$format
* @see Response::FORMAT_JSON
* @see JsonResponseFormatter
*/
public function asJson($data)
{
$response = Yii::$app->getResponse();
$response->format = Response::FORMAT_JSON;
$response->data = $data;
return $response;
}
/**
* Send data formatted as XML.
*
* This method is a shortcut for sending data formatted as XML. It will return
* the [[Application::getResponse()|response]] application component after configuring
* the [[Response::$format|format]] and setting the [[Response::$data|data]] that should
* be formatted. A common usage will be:
*
* ```php
* return $this->asXml($data);
* ```
*
* @param mixed $data the data that should be formatted.
* @return Response a response that is configured to send `$data` formatted as XML.
* @since 2.0.11
* @see Response::$format
* @see Response::FORMAT_XML
* @see XmlResponseFormatter
*/
public function asXml($data)
{
$response = Yii::$app->getResponse();
$response->format = Response::FORMAT_XML;
$response->data = $data;
return $response;
}
/** /**
* Binds the parameters to the action. * Binds the parameters to the action.
* This method is invoked by [[\yii\base\Action]] when it begins to run with the given parameters. * This method is invoked by [[\yii\base\Action]] when it begins to run with the given parameters.

View File

@ -8,6 +8,8 @@
namespace yiiunit\framework\web; namespace yiiunit\framework\web;
use Yii; use Yii;
use yii\web\Controller;
use yii\web\Response;
use yiiunit\TestCase; use yiiunit\TestCase;
use yiiunit\framework\di\stubs\Qux; use yiiunit\framework\di\stubs\Qux;
use yiiunit\framework\web\stubs\Bar; use yiiunit\framework\web\stubs\Bar;
@ -38,4 +40,37 @@ class ControllerTest extends TestCase
$this->assertEquals('avaliable', $other); $this->assertEquals('avaliable', $other);
} }
public function testAsJson()
{
$this->mockWebApplication();
$controller = new Controller('test', Yii::$app);
$data = [
'test' => 123,
'example' => 'data',
];
$result = $controller->asJson($data);
$this->assertInstanceOf('yii\web\Response', $result);
$this->assertSame(Yii::$app->response, $result, 'response should be the same as Yii::$app->response');
$this->assertEquals(Response::FORMAT_JSON, $result->format);
$this->assertEquals($data, $result->data);
}
public function testAsXml()
{
$this->mockWebApplication();
$controller = new Controller('test', Yii::$app);
$data = [
'test' => 123,
'example' => 'data',
];
$result = $controller->asXml($data);
$this->assertInstanceOf('yii\web\Response', $result);
$this->assertSame(Yii::$app->response, $result, 'response should be the same as Yii::$app->response');
$this->assertEquals(Response::FORMAT_XML, $result->format);
$this->assertEquals($data, $result->data);
}
} }