f1monkey/request-handle-bundle

此包已被废弃,不再维护。未建议替代包。

常见的请求反序列化和异常处理

安装: 46

依赖项: 0

建议者: 0

安全性: 0

星标: 0

关注者: 2

分支: 0

开放问题: 0

类型:symfony-bundle

1.1.0 2020-06-30 23:57 UTC

This package is auto-updated.

Last update: 2021-08-29 02:17:07 UTC


README

安装

$ composer require f1monkey/request-handle-bundle

添加到config/bundles.php

<?php
return [
    // ...
    F1Monkey\RequestHandleBundle\RequestHandleBundle::class => ['all' => true],
];

创建配置文件(例如config/packages/request_handle.yaml

配置

request_handle:
    value_resolver:
        # controller arguments implementing this interface will be deserialized using RequestDeserializationValueResolver
        request_class: App\Model\RequestInterface
    request_validator:
        # controller arguments implementing this interface will be validated using RequestValidationListener
        request_class: App\Model\RequestInterface
    exception_log:
        # logger service id for exception logging (default value = @logger)
        logger: 'logger'

ExceptionListener

自定义错误响应

如果您需要自定义错误响应,您应该在应用程序的services.yaml中重写异常响应工厂

services:
    F1Monkey\RequestHandleBundle\Factory\ErrorResponseFactoryInterface:
        class: App\Factory\ErrorResponseFactory

自定义异常消息

默认情况下,错误消息根据HTTP响应代码覆盖默认HTTP消息。要设置自定义错误消息,您应该在您的异常类中实现UserFriendlyExceptionInterface

X-Debug头

由于所有异常都转换为“用户友好”的响应,调试变得更加困难:symfony错误页面将完全不会出现。

  • kernel.debug参数设置为true(这是dev环境的默认设置)
  • X-Debug头存在于请求中

ExceptionLogListener

自定义日志记录

要自定义日志记录,您应该覆盖日志上下文提供者服务

services:
    F1Monkey\RequestHandleBundle\Service\LogContextProviderInterface:
        class: App\Service\LogContextProvider