diegograssato / sf-api-problem
适用于 IETF HTTP APIs 问题细节 RFC 的 Symfony 兼容响应
1.0
2016-07-04 02:21 UTC
Requires
- symfony/symfony: 3.1.*
This package is auto-updated.
Last update: 2024-08-28 13:59:48 UTC
README
介绍
此库提供了一个与 Symfony 3 兼容的 Response 类,它接受一个 ApiProblem 对象并返回一个符合 Problem Details for HTTP APIs 规范的 JSON 对象。
它基于并使用了来自 ZF Campus zf-api-problem
库的 ApiProblem 类,该库是为 Zend Framework 2 定制的。
安装
运行以下命令使用 composer
安装
$ composer require "diegograssato/sf-api-problem:~1.0"
或者将以下行添加到您的 composer.json
"require": { "diegograssato/sf-api-problem": "~1.0" }
用法
use DTUX\ApiProblem\ApiProblem; use DTUX\ApiProblem\ApiProblemResponse; class FakeController { public function fetch($id) { $entity = $this->service->fetch($id); if (!$entity) { return new ApiProblemResponse(new ApiProblem(404, 'Entity not found')); } return $entity; } }
配置监听器
services: you_bundle.kernel.listener.api_exception: class: DTUX\ApiProblem\EventListener\ApiExceptionListener arguments: - '@logger' tags: - { name: kernel.event_listener, event: kernel.exception, method: onKernelException }
许可证
本项目以 BSD 3-Clause 许可证发布,并基于 ZF Campus zf-api-problem
库的部分内容,该库是为 Zend Framework 2 定制的。