简体   繁体   中英

Symfony2 FOSUserBundle FlashBag

I use FosUserBundle, and i want add a notification (flashbag) on login ("You're now connected") and logout ("You're now disconnected").

I've try with Handler but, i've two message on login.. and don't work on logout

service.yml:

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

class:

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

and in my layout:

{% 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 %}

I've make any research on this topic.. But i don't found correct answer.

Can you explain me how can i do please ?

Your solution is correct, but logout process invalidates user's session by default. So your flash message is not displayed. You need to set invalidate_session: false in your security.yml .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM