Symfony 检测到服务的循环引用“;kernel.listener.your“u listener”u name“;,

Symfony 检测到服务的循环引用“;kernel.listener.your“u listener”u name“;,,symfony,symfony-3.4,symfony-3.3,Symfony,Symfony 3.4,Symfony 3.3,我在我的服务中添加了以下行。yml # default configuration for services in *this* file _defaults: # automatically injects dependencies in your services autowire: true # automatically registers your services as commands, event subscribers, etc. autocon

我在我的服务中添加了以下行。yml

# default configuration for services in *this* file
_defaults:
    # automatically injects dependencies in your services
    autowire: true
    # automatically registers your services as commands, event subscribers, etc.
    autoconfigure: true
    # this means you cannot fetch services directly from the container via $container->get()
    # if you need to do this, you can override this setting on individual services
    #public: false
运行以下命令/bin/console cach:clear时,我收到以下错误:

  [Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException]                                                                              
  Circular reference detected for service "kernel.listener.your_listener_name", path: "kernel.listener.your_listener_name -> kernel.listener.your_listener_name". 
以下是服务的定义,仅供参考:

kernel.listener.your_listener_name:
    class: DEL\Bundle\ApiBundle\Exception\ApiException
    tags:
        - { name: kernel.event_listener, event: kernel.exception, method: onKernelException }
这是该服务的类别:

<?php

namespace DEL\Bundle\ApiBundle\Exception;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;

class ApiException extends HttpException
{
    public function __construct($statusCode = 0, $message = null, \Exception $previous = null, array $headers = array(), $code = 0)
    {
        parent::__construct($statusCode, $message, $previous, $headers, $code);
    }

    public function onKernelException(GetResponseForExceptionEvent $event)
    {
        // You get the exception object from the received event
        $exception = $event->getException();

        if ($exception instanceof APIExceptionInterface) {
            $xml = new \SimpleXMLElement('<xml/>');
            $error = $xml->addChild('error');
            $error->addChild('id', $exception::ID);
            $error->addChild('error', $exception::GLOBAL_ERROR_MESSAGE);
            $error->addChild('message', $exception->getMessage());
            $message = $xml->asXML();
            // Customize your response object to display the exception details
            $response = new Response();
            $response->setContent($message);
            // HttpExceptionInterface is a special type of exception that
            // holds status code and header details
            if ($exception instanceof HttpExceptionInterface) {
                $response->setStatusCode($exception->getStatusCode());
                $response->headers->replace($exception->getHeaders());
            } else {
                $response->setStatusCode(Response::HTTP_INTERNAL_SERVER_ERROR);
            }
            // Send the modified response object to the event
            $event->setResponse($response);
        }
    }
}

可能是因为您试图将异常注入异常?但更重要的是,你想让一个例外成为一个聆听者?似乎没有多大意义。@Cerad我迁移到symfony 3.4,因此这是一个遗留代码,我想应用自动连接,问题是“ApiException”扩展了父类“HttpException”,但为什么它会扩展异常?毫无意义。我看不到父类中的任何东西在哪里被使用,尽管可能有更多的代码没有发布。将ApiException更改为ApiListener并去掉构造函数。它应该很好用。也许你需要一个ApiException在你的代码中的其他地方,但是你当然不需要它来做一个监听器。我想发布一个更详细的例子,但我没有一个可以运行的3.4应用程序来测试。如果您真的想最小化更改,那么只需手动提供参数,事情应该像以前一样进行。