From 1be72a205aa3a154a549dc3a2eb3940b850ed55c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Geffroy?= <81738559+raphael-geffroy@users.noreply.github.com> Date: Fri, 6 Sep 2024 22:36:36 +0200 Subject: [PATCH 1/2] Add examples for flashbag peek and peekAll methods --- session.rst | 38 ++++++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/session.rst b/session.rst index a212acf9993..854c84d4f3d 100644 --- a/session.rst +++ b/session.rst @@ -181,7 +181,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 `: +by the :ref:`Twig global app variable `. +Alternatively, 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-block:: @@ -196,6 +199,13 @@ by the :ref:`Twig global app variable `: {% endfor %} + {# same but without clearing them from the flash bag #} + {% for message in app.session.flashbag.peek('notice') %} +
+ {{ message }} +
+ {% endfor %} + {# read and display several types of flash messages #} {% for label, messages in app.flashes(['success', 'warning']) %} {% for message in messages %} @@ -214,6 +224,15 @@ by the :ref:`Twig global app variable `: {% endfor %} {% endfor %} + {# or without clearing the flash bag #} + {% for label, messages in app.session.flashbag.peekAll() %} + {% for message in messages %} +
+ {{ message }} +
+ {% endfor %} + {% endfor %} + .. code-block:: php-standalone // display warnings @@ -221,6 +240,11 @@ by the :ref:`Twig global app variable `: echo '
'.$message.'
'; } + // display warnings without clearing them from the flash bag + foreach ($session->getFlashBag()->peek('warning', []) as $message) { + echo '
'.$message.'
'; + } + // display errors foreach ($session->getFlashBag()->get('error', []) as $message) { echo '
'.$message.'
'; @@ -233,15 +257,17 @@ by the :ref:`Twig global app variable `: } } + // display all flashes at once without clearing the flash bag + foreach ($session->getFlashBag()->peekAll() as $type => $messages) { + foreach ($messages as $message) { + echo '
'.$message.'
'; + } + } + 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 ------------- From cc96d7c59b4893a499c9bb2dc5fce240edffcf15 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Fri, 27 Sep 2024 12:45:29 +0200 Subject: [PATCH 2/2] Minor tweak --- session.rst | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/session.rst b/session.rst index 854c84d4f3d..78f71b9d46d 100644 --- a/session.rst +++ b/session.rst @@ -184,7 +184,7 @@ read any flash messages from the session using the ``flashes()`` method provided by the :ref:`Twig global app variable `. Alternatively, you can use the :method:`Symfony\\Component\\HttpFoundation\\Session\\Flash\\FlashBagInterface::peek` -method instead to retrieve the message while keeping it in the bag. +method to retrieve the message while keeping it in the bag: .. configuration-block:: @@ -268,7 +268,6 @@ 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. - Configuration -------------