api响应中json或xml的可读视图.Yii2


Readable view of json or xml in api response. Yii2

In guides在可读视图中显示api的响应。我想知道,这样做只是为了显示响应的结构,还是有一种方法可以使视图可读。

终端示例:我的代码:中的响应

"date_imported":"2016-03-19 18:30:22"}],"_links":{"self":{"href":"http://localhost/tweets?page=1"},"next":{"href":"http://localhost/tweets?page=2"},"last":{"href":"http://localhost/tweets?page=23"}},"_meta":{"totalCount":450,"pageCount":23,"currentPage":1,"perPage":20}}

docs:相同代码的响应

"_links": {
        "self": {
            "href": "http://localhost/users?page=1"
        },
        "next": {
            "href": "http://localhost/users?page=2"
        },
        "last": {
            "href": "http://localhost/users?page=50"
        }
    },
    "_meta": {
        "totalCount": 1000,
        "pageCount": 50,
        "currentPage": 1,
        "perPage": 20
    }
}

它只是用于演示,而实际响应具有字符串视图,还是我做错了什么?

如果你真的想要一个"可读"的json响应,你应该简单地修改yii'web'JsonResponseFormatter:的$prettyPrint属性

$prettyPrint(从2.0.7版本开始提供):是否将输出格式化为可读的"漂亮"格式。

要配置此属性,可以配置response应用程序组件,如下所示:

'response' => [
    'formatters' => [
        'yii'web'Response::FORMAT_JSON => [
             'class' => 'yii'web'JsonResponseFormatter',
             'prettyPrint' => YII_DEBUG, // use "pretty" output in debug mode
        ],
    ],
],