定制 phpro/api-problem-bundle 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

phpro/api-problem-bundle

最新稳定版本:1.11.0

Composer 安装命令:

composer require phpro/api-problem-bundle

包简介

RFC7807 Problem details integration for Symfony

README 文档

README

Installs Packagist

Api Problem Bundle

This package provides a RFC7807 Problem details exception listener for Symfony. Internal, this package uses the models provided by phpro/api-problem. When an ApiProblemException is triggered, this bundle will return the correct response.

Installation

composer require phpro/api-problem-bundle

If you are not using symfony/flex, you'll have to manually add the bundle to your bundles file:

// config/bundles.php

return [
    // ...
    Phpro\ApiProblemBundle\ApiProblemBundle::class => ['all' => true],
];

Supported response formats

  • application/problem+json

How it works

use Phpro\ApiProblem\Exception\ApiProblemException;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;

class SomeController
{
    /**
     * @Route('/some-route', defaults={"_format" = "json"})
     */
    public function someAction() {
        throw new ApiProblemException(
            new HttpApiProblem('400', 'It aint all bad ...')
        );
    }
}

When the controller is marked as a "json" format, the request Content-Type is */json or the request Accept header first value contains json (i.e. application/json, text/html), this bundle kicks in. It will transform the exception to following response:

Headers:

Content-Type: application/problem+json

Body:

{
    "status": 400,
    "type": "http:\/\/www.w3.org\/Protocols\/rfc2616\/rfc2616-sec10.html",
    "title": "Bad Request",
    "detail": "It ain't all bad ..."
}

As an alternative, use ApiProblemHttpException instead of ApiProblemException, to make it possible to exclude the specific status code from the log

Adding custom exception transformations

Currently, we automatically transform exceptions from following packages to an ApiProblem instance:

  • phpro/api-problem
  • symfony/http-kernel
  • symfony/security

Besides that, all other errors are transformed to a basic ExceptionApiProblem instance.

If you want to add custom transformations, you can implement the ExceptionTransformerInterface and register it in the symfony container with the phpro.api_problem.exception_transformer tag.

use Phpro\ApiProblemBundle\Transformer\ExceptionTransformerInterface;

class MyTransformer implements ExceptionTransformerInterface
{
    public function transform(\Throwable $exception): ApiProblemInterface
    {
        return new MyApiProblem($exception);
    }

    public function accepts(\Throwable $exception): bool
    {
        return $exception instanceof MyException;
    }
}

About

Submitting bugs and feature requests

Bugs and feature request are tracked on GitHub. Please take a look at our rules before contributing your code.

License

api-problem-bundle is licensed under the MIT License.

统计信息

  • 总下载量: 504.2k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 25
  • 点击次数: 1
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 25
  • Watchers: 4
  • Forks: 11
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-08-31