简体   繁体   English

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

[英]Readable view of json or xml in api response. Yii2

In guides response of api showing in readable view. 在指南中,以清晰的视图显示api的响应。 I want to understand, is it done only for showing structure of response, or there is a way to make readable view. 我想了解一下,它是仅用于显示响应的结构吗,还是有一种使视图更易读的方法。

Examples in terminal: my code: response in 终端中的示例:我的代码:响应

"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: response at same code 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
    }
}

Is it only for presentation, and actual response has string-view, or I something doing wrong? 是仅用于演示,并且实际响应具有字符串视图,还是我做错了?

If you really want to have a "readable" json response, you should simply modify $prettyPrint property of yii\\web\\JsonResponseFormatter : 如果您确实想要“可读的” json响应,则只需修改yii\\web\\JsonResponseFormatter $prettyPrint属性:

$prettyPrint (available since version 2.0.7) : Whether to format the output in a readable "pretty" format. $prettyPrint (自版本2.0.7起可用):是否以可读的“漂亮”格式格式化输出。

To configure this property, you can configure the response application component like the following: 要配置此属性,可以配置response应用程序组件,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM