Search code examples
phpsymfonyauthenticationflash-message

Display flash messages after login/logout


I have been following this tutorial from the Symfony Book to manage my users.

Now, I would like to set a flash message after the login has succeeded OR if somebody attempts to access a forbidden area.

What I understand is that authentication and authorization are not managed inside controllers. As a consequence, I do not know where to put my code to display a simple "Sorry, you are not connected" message.


Solution

  • It will take some time and some coding just to display a simple message. If you want to do it by Symfony way you should look at this security configuration first, especially these ones:

    1. entry_point (under firewall) - which usually redirects users to login page whenever they try to access the secured pages. You can set flash messages here.
    2. success_handler under form_login (if you're using it) to show your successful login message
    3. success_handler under logout to show your logout message

    Some references: