簡體   English   中英

api響應中json或xml的可讀視圖。 Yii2

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

在指南中,以清晰的視圖顯示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
        ],
    ],
],

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM