Search code examples
symfonytwigsymfony-3.4

Symfony 3.4 Array to string conversion when trying to display flash messages


I have to be missing something here, just trying to set a flash message in a new project.

In controller:

$this->addFlash(
    'success',
    'Your entry was added!'
);

In my template, I include:

{% for message in app.flashes(['success', 'notice']) %}
    <div class="alert alert-success">
        <p align="center">
            <b>Success! </b><br/>
            {{ message }}
        </p>
    </div>
{% endfor %}

I get the following exception every time:

"An exception has been thrown during the rendering of a template ("Notice: Array to string conversion")."

enter image description here

I checked the length of message and it's zero. If I dump it, it's an empty array as it should be. Anyone see what I'm missing here, driving me crazy. Also, if I set a flash message it gives me the same error.


Solution

  • Read and display several types of flash messages:

    {% for label, messages in app.flashes(['success', 'notice']) %}
        {% for message in messages %}
            <div class="alert alert-success">
                <p align="center">
                    <b>Success! </b><br/>
                    {{ message }}
                </p>
            </div>
        {% endfor %}
    {% endfor %}
    

    See: https://symfony.com/doc/current/controller.html#managing-the-session