Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8814871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:14:13+00:00 2026-06-14T04:14:13+00:00

I have a logout Listener where I’d like to set a flash message showing

  • 0

I have a logout Listener where I’d like to set a flash message showing a logout confirmation message.

namespace Acme\MyBundle\Security\Listeners;

use Symfony\Component\Security\Http\Logout\LogoutSuccessHandlerInterface;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;

class LogoutListener implements LogoutSuccessHandlerInterface
{
  private $security;  

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

  public function onLogoutSuccess(Request $request)
  {
    $request->get('session')->getFlashBag()->add('notice', 'You have been successfully been logged out.');

    $response = new RedirectResponse('login');
    return $response;
  }
}

Here is my services.yml (as it pertains to this):

logout_listener:
   class:  ACME\MyBundle\Security\Listeners\LogoutListener
   arguments: [@security.context]

This is generating an error:

Fatal error: Call to a member function getFlashBag() on a non-object

How do I set a flashBag message in this context?

Also, how do get access to the router so I can generate the url (via $this->router->generate(‘login’)) instead of passing in a hard-coded url?

Resolution Note

To get the flash to work, you must tell your security.yml config not invalidate the session on logout; otherwise, the session will be destroyed and your flash will never appear.

logout:
    path: /logout
        success_handler: logout_listener
        invalidate_session: false
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T04:14:14+00:00Added an answer on June 14, 2026 at 4:14 am

    You should inject the services for session and router into the LogoutListener and use them to perform these tasks. This is the way to do it in yml:

    logout_listener: 
    class: ACME\MyBundle\Security\Listeners\LogoutListener 
    arguments: [@security.context, @router, @session]
    

    Then in your class you write:

    class LogoutListener implements LogoutSuccessHandlerInterface
    {
        private $security;
        private $router;
        private $session;
    
        public function __construct(SecurityContext $security, Router $router, Session $session)
        {
            $this->security = $security;
            $this->router = $router;
            $this->session = $session;
        }
        [...]
    

    When you want to use the session now you can just say:

    $this->session->getFlashBag()->add('notice', 'You have been successfully been logged out.');
    

    And in the same way you can use the router service to generate routes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have set UserIsOnlineTimeWindow =2 in web.config then my logout() function has been modified
I have a logout in my mastrpage.page_load in which I disable the cache like
i have a logout link in jsp and i call the logout function like
I have a logout file for my web application. I would like to create
I have a requirement for an explicit logout button for users in a ASP.NET
I have a simple GAE app that includes a login/logout link. This app is
I have come across many articles which warn against using links to provide logout
I have detailed stats on user access to my system detailing login and logout
I have made a web application that uses master page for Login & Logout
I have a frame containing a JMenuBar. If the users clicks the Logout MenuItem,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.