Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Symfony 3.3.8忽略自定义规格化器_Php_Symfony_Symfony 3.3 - Fatal编程技术网

Php Symfony 3.3.8忽略自定义规格化器

Php Symfony 3.3.8忽略自定义规格化器,php,symfony,symfony-3.3,Php,Symfony,Symfony 3.3,为什么symfony忽略我的自定义规范化器 src/AppBundle/Serializer/Normalizer/ExceptionNormalizer.php <?php namespace AppBundle\Serializer\Normalizer; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; /** * Class ExceptionNormalizer */ class Excep

为什么symfony忽略我的自定义规范化器

src/AppBundle/Serializer/Normalizer/ExceptionNormalizer.php

<?php

namespace AppBundle\Serializer\Normalizer;

use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

/**
 * Class ExceptionNormalizer
 */
class ExceptionNormalizer implements NormalizerInterface
{
    /**
     * {@inheritdoc}
     */
    public function normalize($object, $format = null, array $context = array()): array
    {
        return [];
    }

    /**
     * {@inheritdoc}
     */
    public function supportsNormalization($data, $format = null): bool
    {
        return $data instanceof \Exception;
    }
}
app/config/config.yml

services:
    ...
    app.normalizer.exception:
        class: AppBundle\Serializer\Normalizer\ExceptionNormalizer
        tags:
            - { name: serializer.normalizer }
imports:
    - { resource: parameters.yml }
    - { resource: security.yml }
    #- { resource: services.yml } exclude default services file
    - { resource: "@AppBundle/Resources/config/services.yml" }
异常输出

{“error”:{“code”:404,“message”:“Not Found”,“exception”:[{“message”:“AppBundle\Entity\User object Not Found.”,“class”:“Symfony\Component…”

预期的异常输出

{}


您不应该规范化异常。相反,为此类异常创建侦听器,处理它(例如,写入日志),并返回所需的输出作为响应

class ExceptionListener
{
/** @var LoggerInterface */
private $logger;


public function __construct(LoggerInterface $logger)
{
    $this->logger = $logger;
}

public function onKernelException(GetResponseForExceptionEvent $event)
{
    $e = $event->getException();
    if ($e instanceof ValidationException) {
        $event->setResponse(new JsonResponse(['error' => $e->getViolations()], 422)
    } elseif ($e instanceof DomainException) {
        $this->logger->warning('Exception ' . get_class($e) , ['message' => $e->getMessage()]);
        $event->setResponse(
        new JsonResponse(['error' => 'Something is wrong with your request.'], 400);
    } elseif ($e instanceof NotFoundHttpException) {
        $event->setResponse(new JsonResponse(['error' => 'Not found.'], 404);
    } else {
        $event->setResponse(new JsonResponse(['error' => $this->translator->trans('http.internal_server_error')], 500);
    }
}
}

更新服务.yml

  app.exception_listener:
    class: Application\Listeners\ExceptionListener
    arguments: ['@domain.logger']
    tags:
      - { name: kernel.event_listener, event: kernel.exception }
关于听众和事件的进一步阅读


您的规范化程序很可能被忽略,因为您没有在序列化程序中注册它。

欢迎使用堆栈溢出。请花一些时间阅读发布指南,否则您可能会获得负面投票。看起来它与规范化程序无关-它没有试图工作的用户实体with@JasonRoman它的例外ormalizer,而不是用户的规范化程序。我尝试在其他内容上重新定义异常输出,但我得到了与exception infoShow相同的字符串。显示定义和调用序列化程序或此规范化程序的代码。