如何在Apigility ApiProblem响应上启用Json漂亮的打印



我想知道如何将ApiProblem响应设置为默认情况下漂亮的打印Json。

有一个用于在 ZF-Hal 中渲染的HalJsonStrategy。此策略扩展了默认 ZF2 JsonStrategy 。此策略呈现类型为 HalJsonModel 的视图模型,该模型扩展了默认的 ZF2 JsonModel 类。渲染使用扩展默认 ZF2 JsonRenderer类的HalJsonRenderer进行。

您可以通过将渲染策略更改为某些自定义策略或通过在HalJsonStrategy内设置另一个渲染器(扩展现有渲染器的自定义渲染器)来自定义所有这些。

不知道什么是最好的方法。
由于所有这些 hal-json 渲染逻辑都建立在默认的 ZF2 json 渲染逻辑之上,因此很可能通过更改配置来更改当前渲染器输出 json 的方式,就像通常在 ZF2 中所做的那样,以实现漂亮的打印输出。

也许这里的这个页面会帮助你实现你想要的。

总结:

归根结底是将JsonRenderer类中render方法中的Json::encode调用(或渲染调用)交换为Json::prettyPrint


注意:这样做的原因可能是为了在浏览器窗口中以可读的方式查看 json 代码。有许多 json 插件可以帮助您解决这个问题,这将是一个更简单的解决方案。

我对类ApiProblemResponse进行了一些更改。我将属性$jsonFlags设置为 128,这与JSON_PRETTY_PRINT代码有关。

以下是包含我更改的完整类代码:

<?php
namespace ZendApiProblem;
use ZendHttpResponse;
/**
 * Represents an ApiProblem response payload
 */
class ApiProblemResponse extends Response
{
    /**
     * @var ApiProblem
     */
    protected $apiProblem;
    /**
     * Flags to use with json_encode JSON_PRETTY_PRINT
     *
     * @var int
     */
    protected $jsonFlags = 128;
    /**
     * @param ApiProblem $apiProblem
     */
    public function __construct(ApiProblem $apiProblem)
    {
        $this->apiProblem = $apiProblem;
        $this->setCustomStatusCode($apiProblem->status);
        $this->setReasonPhrase($apiProblem->title);
        // Just comment/remove these lines to prevent flags from being overwrited
        //if (defined('JSON_UNESCAPED_SLASHES')) {
          //$this->jsonFlags = constant('JSON_UNESCAPED_SLASHES');
        //}
    }
    /**
     * @return ApiProblem
     */
    public function getApiProblem()
    {
        return $this->apiProblem;
    }
    /**
     * Retrieve the content
     *
     * Serializes the composed ApiProblem instance to JSON.
     *
     * @return string
     */
    public function getContent()
    {
        return json_encode($this->apiProblem->toArray(), $this->jsonFlags);
    }
    /**
     * Retrieve headers
     *
     * Proxies to parent class, but then checks if we have an content-type
     * header; if not, sets it, with a value of "application/problem+json".
     *
     * @return ZendHttpHeaders
     */
    public function getHeaders()
    {
        $headers = parent::getHeaders();
        if (!$headers->has('content-type')) {
            $headers->addHeaderLine('content-type', 'application/problem+json');
        }
        return $headers;
    }
    /**
     * Override reason phrase handling
     *
     * If no corresponding reason phrase is available for the current status
     * code, return "Unknown Error".
     *
     * @return string
     */
    public function getReasonPhrase()
    {
        if (! empty($this->reasonPhrase)) {
            return $this->reasonPhrase;
        }
        if (isset($this->recommendedReasonPhrases[$this->statusCode])) {
            return $this->recommendedReasonPhrases[$this->statusCode];
        }
        return 'Unknown Error';
    }
}

在我的控制器中,我使用了:

return new ApiProblemResponse(
  new ApiProblem(ApiProblemResponse::STATUS_CODE_501, 'Example of JSON_PRETTY_PRINT response.')
);

并做到了:

{
    "type": "http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html",
    "title": "Not Implemented",
    "status": 501,
    "detail": "Example of JSON_PRETTY_PRINT response."
}

最新更新