php - 未捕获的 Symfony\Component\Debug\Exception\FatalThrowableError:
问题描述
在 Symfony3.4 中,支持自动布线时出现以下错误。
相关部分未作特别改动。
我应该做点什么吗?
后记:
services.yml中有一个地方指定了对应部分的服务。
改变这部分似乎可以解决问题,但我想不出一个计划。
https://symfony.com/doc/3.4/service_container/3.3-di-changes.html
错误
Fatal error: Uncaught Symfony\Component\Debug\Exception\FatalThrowableError:
Type error: Argument 2 passed to
App\Ahi\Sp\AdminBundle\Listener\AdminExceptionListener::onKernelException() must implement interface
Symfony\Component\HttpKernel\KernelInterface, string given, called in
/home/vagrant/Symfony2/vendor/symfony/symfony/src/Symfony/Component/EventDispatcher/Debug/WrappedListener.php on line 115 in
/home/vagrant/Symfony2/src/Ahi/Sp/AdminBundle/Listener/AdminExceptionListener.php:24
AdminExceptionListener.php
namespace App\Ahi\Sp\AdminBundle\Listener;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\KernelInterface;
/**
* Management screen exception listener
*/
class AdminExceptionListener
{
private $templating;
private $container;
public function __construct($templating, $container)
{
$this->templating = $templating;
$this->container = $container;
}
//line24
public function onKernelException(GetResponseForExceptionEvent $event, KernelInterface $kernel)
{
// Default exception handling in debug mode
if ($kernel->isDebug()) {
return;
}
// Handle only exceptions that occur on the management screen
if (!preg_match('/^\/admin\//', $event->getRequest()->getPathInfo())) {
return;
}
...
}
}
服务.yml
services:
# this makes public all the services defined in this file
_defaults:
autowire: true
autoconfigure: true
public: false
App\:
resource: '../../src/*'
# you can exclude directories or files
# but if a service is unused, it's removed anyway
exclude: '../../src/{ Entity,Repository }'
App\Ahi\Sp\AdminBundle\Controller\:
resource: '../../src/Ahi/Sp/AdminBundle/Controller'
public: true
tags: ['controller.service_arguments']
App\Ahi\Sp\AdminBundle\Listener\AdminExceptionListener:
arguments: ['@templating', '@service_container']
tags:
- {
name: kernel.event_listener,
event: kernel.exception,
method: onKernelException,
}
解决方案
It worked by passing only KernelInterface to __construct. Thanks to Snroki.
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Templating\EngineInterface;
/**
*/
class AdminExceptionListener
{
protected $templating;
protected $kernel;
protected $container;
public function __construct(KernelInterface $kernel, ContainerInterface $container, EngineInterface $templating)
{
$this->kernel = $kernel;
$this->templating = $templating;
$this->container = $container;
}
public function onKernelException(GetResponseForExceptionEvent $event)
{
if ($this->kernel->isDebug()) {
return;
}
推荐阅读
- c# - WPF删除按钮突出显示但保持边框
- amazon-web-services - AWS Terraform Lambda 预置并发
- php - 使用 MYSQLAdmin 或 php 将 sql 文件导入 MYSQL 数据库
- django - Django celery 任务 api 调用
- javascript - Firefox 中的英特尔:DateTimeFormat.format() 中的值不是有限的
- powershell - 立即禁用新 Microsoft 365 组的欢迎电子邮件
- ruby-on-rails - 忽略/跳过 updated_at 在 Paper Trail gem 中不起作用
- javascript - ReactJS 使用 MaterialUI 处理表单错误
- docker - 如何在 Github 中对服务进行健康检查?
- sql - Oracle SQL 开发人员查询