Skip to content

Explain how to get all flash messages #7432

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions controller.rst
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ For more information, see the :doc:`Routing chapter </routing>`.

.. caution::

The ``redirect()`` method does not check its destination in any way. If you
redirect to some URL provided by the end-users, your application may be open
The ``redirect()`` method does not check its destination in any way. If you
redirect to some URL provided by the end-users, your application may be open
to the `unvalidated redirects security vulnerability`_.


Expand Down Expand Up @@ -425,21 +425,43 @@ read any flash messages from the session:
.. code-block:: html+twig

{# app/Resources/views/base.html.twig #}
{% for flash_message in app.session.flashBag.get('notice') %}

{# you can read and display just one flash message type... #}
{% for flash_message in app.session.flash('notice') %}
<div class="flash-notice">
{{ flash_message }}
</div>
{% endfor %}

{# ...or you can read and display every flash message available #}
{% for type, flash_messages in app.session.flashes %}
{% for flash_message in flash_messages %}
<div class="flash-{{ type }}">
{{ flash_message }}
</div>
{% endif %}
{% endfor %}

.. code-block:: html+php

<!-- app/Resources/views/base.html.php -->

// you can read and display just one flash message type...
<?php foreach ($view['session']->getFlash('notice') as $message): ?>
<div class="flash-notice">
<?php echo "<div class='flash-error'>$message</div>" ?>
<?php echo $message ?>
</div>
<?php endforeach ?>

// ...or you can read and display every flash message available
<?php foreach ($view['session']->getFlashes() as $type => $flash_messages): ?>
<?php foreach ($flash_messages as $flash_message): ?>
<div class="flash-<?php echo $type ?>">
<?php echo $message ?>
</div>
<?php endforeach ?>
<?php endforeach ?>

.. note::

It's common to use ``notice``, ``warning`` and ``error`` as the keys of the
Expand Down