问题描述
我在 services.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);
}
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)