Symfony2 FOSUserBundle FlashBag

时间:2016-07-03 22:25:46

标签: symfony login notifications fosuserbundle logout

我使用FosUserBundle,我想在登录时添加通知(flashbag)("你现在已连接")并注销("你现在已经断开连接&#34 ;)

我尝试使用Handler但是,我在登录时发出了两条消息,并且没有注销

service.yml:

logout_service:
   class:  App\UserBundle\Service\LogoutService
   arguments: [@security.context,@session]

类:

class LogoutService implements LogoutSuccessHandlerInterface {
    private $security;
    private $session;

    public function __construct(SecurityContext $security,Session $session) 
    {
        $this->security = $security;
        $this->session = $session;
    }

    public function onLogoutSuccess(Request $request) 
    {
        $this->session->getFlashBag()->add('notice, 'You're now disconnected.');
        return new RedirectResponse('app');
    }
}

在我的布局中:

{% for flashMessage in app.session.flashbag.get('notice') %}
    <div class="alert alert-success alert-dismissible">
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
        {{ flashMessage }}
    </div>
{% endfor %}

我已就此主题进行任何研究..但我没有找到正确答案。

你能解释一下我该怎么办?

1 个答案:

答案 0 :(得分:1)

您的解决方案是正确的,但默认情况下,注销过程会使用户的会话无效。因此不会显示您的Flash消息。您需要在security.yml中设置invalidate_session: false

logout:
    path: /logout
    target: /
    invalidate_session: false
    success_handler: logout_service