Skip to content

Commit 96e0254

Browse files
committed
minor symfony#7432 Explain how to get all flash messages (javiereguiluz)
This PR was squashed before being merged into the 2.7 branch (closes symfony#7432). Discussion ---------- Explain how to get all flash messages Alternative to symfony#7430. Commits ------- e5c1ed9 Explain how to get all flash messages
2 parents 9526dad + e5c1ed9 commit 96e0254

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

controller.rst

+24-2
Original file line numberDiff line numberDiff line change
@@ -425,21 +425,43 @@ read any flash messages from the session:
425425
.. code-block:: html+twig
426426

427427
{# app/Resources/views/base.html.twig #}
428-
{% for flash_message in app.session.flashBag.get('notice') %}
428+
429+
{# you can read and display just one flash message type... #}
430+
{% for flash_message in app.session.flash('notice') %}
429431
<div class="flash-notice">
430432
{{ flash_message }}
431433
</div>
432434
{% endfor %}
433435

436+
{# ...or you can read and display every flash message available #}
437+
{% for type, flash_messages in app.session.flashes %}
438+
{% for flash_message in flash_messages %}
439+
<div class="flash-{{ type }}">
440+
{{ flash_message }}
441+
</div>
442+
{% endif %}
443+
{% endfor %}
444+
434445
.. code-block:: html+php
435446

436447
<!-- app/Resources/views/base.html.php -->
448+
449+
// you can read and display just one flash message type...
437450
<?php foreach ($view['session']->getFlash('notice') as $message): ?>
438451
<div class="flash-notice">
439-
<?php echo "<div class='flash-error'>$message</div>" ?>
452+
<?php echo $message ?>
440453
</div>
441454
<?php endforeach ?>
442455

456+
// ...or you can read and display every flash message available
457+
<?php foreach ($view['session']->getFlashes() as $type => $flash_messages): ?>
458+
<?php foreach ($flash_messages as $flash_message): ?>
459+
<div class="flash-<?php echo $type ?>">
460+
<?php echo $message ?>
461+
</div>
462+
<?php endforeach ?>
463+
<?php endforeach ?>
464+
443465
.. note::
444466

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

0 commit comments

Comments
 (0)