I am attempting to output a flash message to the screen using the FlashBagInterface, however I am having no luck in doing so.
This is my current setup, as you can see my controller sets the message and then my Twig template is attempting to output it. In practice i'm only going to have one message on this page, so a for
loop is probably not required but I can't see why this code is not working for me.
I don't get any errors, just nothing output to the screen.
Also, if I die
out in the code on $message
I get my message that I want to show.
In my controller:
$message = $this->get('translator')->trans('ContactThanksMessage');
$this->get('session')->getFlashBag()->set('contact_thanks', array('message' => $message));
return $this->redirect($this->generateUrl("ayrshireminis_contact"));
In my Twig template:
{% if app.session.flashbag.get('contact_thanks') %}
{% for flashMessage in app.session.flashbag.get('contact_thanks') %}
<p>{{ flashMessage }}</p>
{% endfor %}
{% else %}
What you need to know is when you get a flash message, it is removed in a auto expire strategy (the default one). So, in your template when you do your if
, the flash message is removed with the get
method... You should rely on the has
method to check if the flash message exists and then use get
to consume it.