Skip to content

Commit c648742

Browse files
committed
minor #20206 Add examples for flashbag peek and peekAll methods (raphael-geffroy)
This PR was submitted for the 7.2 branch but it was squashed and merged into the 5.4 branch instead. Discussion ---------- Add examples for flashbag peek and peekAll methods Fixes #20198 Commits ------- 1be72a2 Add examples for flashbag peek and peekAll methods
2 parents 1d8d4b6 + 1be72a2 commit c648742

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

session.rst

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

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

186189
.. configuration-block::
187190

@@ -196,6 +199,13 @@ by the :ref:`Twig global app variable <twig-app-variable>`:
196199
</div>
197200
{% endfor %}
198201

202+
{# same but without clearing them from the flash bag #}
203+
{% for message in app.session.flashbag.peek('notice') %}
204+
<div class="flash-notice">
205+
{{ message }}
206+
</div>
207+
{% endfor %}
208+
199209
{# read and display several types of flash messages #}
200210
{% for label, messages in app.flashes(['success', 'warning']) %}
201211
{% for message in messages %}
@@ -214,13 +224,27 @@ by the :ref:`Twig global app variable <twig-app-variable>`:
214224
{% endfor %}
215225
{% endfor %}
216226

227+
{# or without clearing the flash bag #}
228+
{% for label, messages in app.session.flashbag.peekAll() %}
229+
{% for message in messages %}
230+
<div class="flash-{{ label }}">
231+
{{ message }}
232+
</div>
233+
{% endfor %}
234+
{% endfor %}
235+
217236
.. code-block:: php-standalone
218237
219238
// display warnings
220239
foreach ($session->getFlashBag()->get('warning', []) as $message) {
221240
echo '<div class="flash-warning">'.$message.'</div>';
222241
}
223242
243+
// display warnings without clearing them from the flash bag
244+
foreach ($session->getFlashBag()->peek('warning', []) as $message) {
245+
echo '<div class="flash-warning">'.$message.'</div>';
246+
}
247+
224248
// display errors
225249
foreach ($session->getFlashBag()->get('error', []) as $message) {
226250
echo '<div class="flash-error">'.$message.'</div>';
@@ -233,15 +257,17 @@ by the :ref:`Twig global app variable <twig-app-variable>`:
233257
}
234258
}
235259
260+
// display all flashes at once without clearing the flash bag
261+
foreach ($session->getFlashBag()->peekAll() as $type => $messages) {
262+
foreach ($messages as $message) {
263+
echo '<div class="flash-'.$type.'">'.$message.'</div>';
264+
}
265+
}
266+
236267
It's common to use ``notice``, ``warning`` and ``error`` as the keys of the
237268
different types of flash messages, but you can use any key that fits your
238269
needs.
239270

240-
.. tip::
241-
242-
You can use the
243-
:method:`Symfony\\Component\\HttpFoundation\\Session\\Flash\\FlashBagInterface::peek`
244-
method instead to retrieve the message while keeping it in the bag.
245271

246272
Configuration
247273
-------------

0 commit comments

Comments
 (0)