使用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/*'
但是我收到以下编译错误
编译错误:ContainerNxlf3ac \ appDevDebugProjectContainer :: load():无法打开所需的&#39; /.../ var / cache / dev / ContainerNxlf3ac / getLogoutlistener2Service.php&#39; (包含路径=&#39;:在/ usr /共享/ PHP的&#39)
看起来我必须为服务添加别名,但我无法设法这样做......
使用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]
答案 0 :(得分:0)
问题出在防火墙部分。使用“l”(小写)代替LogoutListener
名称的“L”(大写)是错误的。设置以下内容有效。
handlers: [c975L\UserBundle\Listeners\LogoutListener]
<强> [编辑] 强> 有一个错误,已由https://github.com/symfony/symfony/pull/26355
解决