Skip to content

Commit e7aceec

Browse files
committed
Merge branch '7.1' into 7.2
* 7.1: Add examples for flashbag peek and peekAll methods
2 parents 4fcdb8f + 239d042 commit e7aceec

File tree

1 file changed

+32
-7
lines changed

1 file changed

+32
-7
lines changed

session.rst

+32-7
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,10 @@ can be anything. You'll use this key to retrieve the message.
178178

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

183186
.. configuration-block::
184187

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

199+
{# same but without clearing them from the flash bag #}
200+
{% for message in app.session.flashbag.peek('notice') %}
201+
<div class="flash-notice">
202+
{{ message }}
203+
</div>
204+
{% endfor %}
205+
196206
{# read and display several types of flash messages #}
197207
{% for label, messages in app.flashes(['success', 'warning']) %}
198208
{% for message in messages %}
@@ -211,13 +221,27 @@ by the :ref:`Twig global app variable <twig-app-variable>`:
211221
{% endfor %}
212222
{% endfor %}
213223

224+
{# or without clearing the flash bag #}
225+
{% for label, messages in app.session.flashbag.peekAll() %}
226+
{% for message in messages %}
227+
<div class="flash-{{ label }}">
228+
{{ message }}
229+
</div>
230+
{% endfor %}
231+
{% endfor %}
232+
214233
.. code-block:: php-standalone
215234
216235
// display warnings
217236
foreach ($session->getFlashBag()->get('warning', []) as $message) {
218237
echo '<div class="flash-warning">'.$message.'</div>';
219238
}
220239
240+
// display warnings without clearing them from the flash bag
241+
foreach ($session->getFlashBag()->peek('warning', []) as $message) {
242+
echo '<div class="flash-warning">'.$message.'</div>';
243+
}
244+
221245
// display errors
222246
foreach ($session->getFlashBag()->get('error', []) as $message) {
223247
echo '<div class="flash-error">'.$message.'</div>';
@@ -230,16 +254,17 @@ by the :ref:`Twig global app variable <twig-app-variable>`:
230254
}
231255
}
232256
257+
// display all flashes at once without clearing the flash bag
258+
foreach ($session->getFlashBag()->peekAll() as $type => $messages) {
259+
foreach ($messages as $message) {
260+
echo '<div class="flash-'.$type.'">'.$message.'</div>';
261+
}
262+
}
263+
233264
It's common to use ``notice``, ``warning`` and ``error`` as the keys of the
234265
different types of flash messages, but you can use any key that fits your
235266
needs.
236267

237-
.. tip::
238-
239-
You can use the
240-
:method:`Symfony\\Component\\HttpFoundation\\Session\\Flash\\FlashBagInterface::peek`
241-
method instead to retrieve the message while keeping it in the bag.
242-
243268
Configuration
244269
-------------
245270

0 commit comments

Comments
 (0)