首页 > 解决方案 > 未捕获的 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,
          }

标签: phpsymfony

解决方案


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;
        }

推荐阅读