Skip to content

Commit

Permalink
Merge branch '5.4' into 6.4
Browse files Browse the repository at this point in the history
* 5.4:
  Add examples for flashbag peek and peekAll methods
  • Loading branch information
javiereguiluz committed Sep 27, 2024
2 parents bf1f8e5 + cc96d7c commit f7e7839
Showing 1 changed file with 32 additions and 7 deletions.
39 changes: 32 additions & 7 deletions session.rst
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,10 @@ can be anything. You'll use this key to retrieve the message.

In the template of the next page (or even better, in your base layout template),
read any flash messages from the session using the ``flashes()`` method provided
by the :ref:`Twig global app variable <twig-app-variable>`:
by the :ref:`Twig global app variable <twig-app-variable>`.
Alternatively, you can use the
:method:`Symfony\\Component\\HttpFoundation\\Session\\Flash\\FlashBagInterface::peek`
method to retrieve the message while keeping it in the bag:

.. configuration-block::

Expand All @@ -193,6 +196,13 @@ by the :ref:`Twig global app variable <twig-app-variable>`:
</div>
{% endfor %}

{# same but without clearing them from the flash bag #}
{% for message in app.session.flashbag.peek('notice') %}
<div class="flash-notice">
{{ message }}
</div>
{% endfor %}

{# read and display several types of flash messages #}
{% for label, messages in app.flashes(['success', 'warning']) %}
{% for message in messages %}
Expand All @@ -211,13 +221,27 @@ by the :ref:`Twig global app variable <twig-app-variable>`:
{% endfor %}
{% endfor %}

{# or without clearing the flash bag #}
{% for label, messages in app.session.flashbag.peekAll() %}
{% for message in messages %}
<div class="flash-{{ label }}">
{{ message }}
</div>
{% endfor %}
{% endfor %}

.. code-block:: php-standalone
// display warnings
foreach ($session->getFlashBag()->get('warning', []) as $message) {
echo '<div class="flash-warning">'.$message.'</div>';
}
// display warnings without clearing them from the flash bag
foreach ($session->getFlashBag()->peek('warning', []) as $message) {
echo '<div class="flash-warning">'.$message.'</div>';
}
// display errors
foreach ($session->getFlashBag()->get('error', []) as $message) {
echo '<div class="flash-error">'.$message.'</div>';
Expand All @@ -230,16 +254,17 @@ by the :ref:`Twig global app variable <twig-app-variable>`:
}
}
// display all flashes at once without clearing the flash bag
foreach ($session->getFlashBag()->peekAll() as $type => $messages) {
foreach ($messages as $message) {
echo '<div class="flash-'.$type.'">'.$message.'</div>';
}
}
It's common to use ``notice``, ``warning`` and ``error`` as the keys of the
different types of flash messages, but you can use any key that fits your
needs.

.. tip::

You can use the
:method:`Symfony\\Component\\HttpFoundation\\Session\\Flash\\FlashBagInterface::peek`
method instead to retrieve the message while keeping it in the bag.

Configuration
-------------

Expand Down

0 comments on commit f7e7839

Please sign in to comment.