Symfony 在 LogoutListener 中注入 EntityManager

Symfony inject EntityManager in LogoutListener

使用 Symfony 3.4,我需要在注销前保留用户,所以我实现了以下侦听器:

<?php
namespace c975L\UserBundle\Listeners;

use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Http\Logout\LogoutHandlerInterface;
use c975L\UserBundle\Entity\User;

class LogoutListener implements LogoutHandlerInterface
{
    private $em;

    public function __construct(EntityManagerInterface $em)
    {
        $this->em = $em;
    }

    public function logout(Request $request, Response $response, TokenInterface $token)
    {
        $user = $token->getUser();
        if ($user instanceof User) {
            $user->setLatestSignout(new \DateTime());
            $this->em->persist($user);
            $this->em->flush();
        }
    }
}

我已在 services.yml

中自动装配
services:
    _defaults:
        autowire: true
        autoconfigure: true
        public: true
    c975L\UserBundle\Listeners\:
        resource: '../../Listeners/*'

但我收到以下编译错误

Compile Error: ContainerNxlf3ac\appDevDebugProjectContainer::load(): Failed opening required '/.../var/cache/dev/ContainerNxlf3ac/getLogoutlistener2Service.php' (include_path='.:/usr/share/php')

看来我必须为服务添加别名,但我做不到...

我在使用 parent::__construct();

时得到了相同的结果

如果删除了 __construct() 但未定义 $this->em,则不会出现错误...

[编辑 - 添加了防火墙部分]

security.yml

firewalls:
    dev:
        pattern: ^/(_(profiler|wdt)|css|fonts|images|js)/
        security: false
    main:
        pattern: ^/
        provider: c975_l_userbundle
        form_login:
            login_path: user_signin
            check_path: user_signin
            default_target_path: user_dashboard
            csrf_token_generator: security.csrf.token_manager
        remember_me:
            secret: '%secret%'
            lifetime: 31536000
            path: /
            secure: true
        anonymous:    true
        logout_on_user_change: true
        logout:
            path: user_signout
            handlers: [c975L\UserBundle\Listeners\Logoutlistener]

问题出在防火墙部分。使用 "l"(小写)代替 "L"(大写)作为 LogoutListener 名称是错误的。设置以下有效。

handlers: [c975L\UserBundle\Listeners\LogoutListener]

[编辑] 有一个错误,已由 https://github.com/symfony/symfony/pull/26355

解决