Flash-Messages in Symfony2 doesn't seem to work in my twig-template
Asked Answered
I

7

10

I want to add support for flash messages on our pages. I implemented this by following the documentation found here.

I added the following snipplet to my base layout. (i also tried to add it to a specific action template).

{% if app.session.hasFlash('notice') %} 
    <div id="flashmessage" class="flash-notice"> 
       {{ app.session.flash('notice') }} 
   </div> 
{% endif %} 

After adding the following error is thrown

Twig_Error_Runtime: Item "hasFlash" for "" does not exist in "MyBundle::layout.html.twig" at line 66

Is there anything else i need to do ?

Imprecise answered 23/11, 2011 at 9:49 Comment(2)
what does var_dump($this->get('session')); output in your controller action?Seaward
Running a 2.0 symfony I use either hasFlash() as in your example or {% for flashMessage in app.session.getFlashes() %} {{ {{ flashMessage }} {% endfor %}Schlessel
E
18

do you use symfony 2.0 or 2.1 (currently master branch)?

for symfony 2.1 the documentation is located here: http://symfony.com/doc/2.1/book/controller.html#flash-messages

flash messages are shown like this:

{% for flashMessage in app.session.flashbag.get('notice') %}
    <div class="flash-notice">
        {{ flashMessage }}
    </div>
{% endfor %}
Emmaemmalee answered 31/7, 2012 at 8:1 Comment(0)
S
6

Mmm check in your config file that you have auto-started the session:

session:
    default_locale: %locale%
    auto_start:     true

Because the error seems to be that Twig doesn't find the session class, not something about the hasFlash function. In fact I have almost exactly the same code in my layout.

Secure answered 23/11, 2011 at 17:6 Comment(1)
I agree with that. It seems app.session is null inside the Twig template.Tadich
Q
3

This is pretty old at time of writing so imagine you've worked it out by now, but for reference sake, it's has rather than hasFlash. So..

{% if app.session.flashbag.has('notice') %} 
    <div id="flashmessage" class="flash-notice"> 
       {{ app.session.flashbag.get('notice') }} 
   </div> 
{% endif %} 
Queenie answered 20/3, 2015 at 22:22 Comment(1)
Doesn't work (2.6) because flashbag.get returns an array. See Manual.Boykins
V
3

By symfony 2.6 +

{% if app.session.flashbag.has('notice') %}
    {{ app.session.flashbag.get('notice').0 }}<br/>
{% endif %}

Because flashbag is by this version array you need foreach it or use index. I m using index because i dont need something more.

Veridical answered 5/9, 2015 at 19:25 Comment(0)
S
1

In controller

$this->get('session')->getFlashBag()->add('notice', 'Your message!');

In your Twig file

{% for flashMessage in app.session.flashbag.get('notice') %}
    <div class="alert alert-warning">{{ flashMessage }}</div>
{% endfor %}  
Superpatriot answered 27/11, 2014 at 23:31 Comment(0)
S
0

I just figure out that flash messages are not working if intercept_redirects is true in debug mode.

Sanious answered 17/5, 2012 at 17:0 Comment(0)
C
-1

Did you set the flash message somewhere in your action?

$this->get('session')->setFlash('notice', 'Your changes were saved!');

Remember that flash messages will be stored on the user's session for exactly one additional request.

Celebrant answered 23/11, 2011 at 16:32 Comment(1)
if you read the question properly, app.session does not exist in the twig template, its nothing to do with setting itSeaward

© 2022 - 2024 — McMap. All rights reserved.