From 2f13218425722750addd7b2c50e23f44fe4b700f Mon Sep 17 00:00:00 2001 From: Drak Date: Tue, 20 Mar 2012 14:39:05 +0545 Subject: [PATCH 1/2] [HttpFoundation] Update for flash message API. --- components/http_foundation.rst | 71 ++++++++++++++++++++++++++++------ 1 file changed, 59 insertions(+), 12 deletions(-) diff --git a/components/http_foundation.rst b/components/http_foundation.rst index 415c770f6ef..6fbee084609 100644 --- a/components/http_foundation.rst +++ b/components/http_foundation.rst @@ -400,10 +400,13 @@ Quick example:: $session->set('name', 'Drak'); $session->get('name'); - // set and retrieve flash messages - $session->getFlashBag()->set('notice', 'Profile updated'); + // set flash messages + $session->getFlashBag()->add('notice', 'Profile updated'); - echo $session->getFlashBag()->get('notice'); + // retrieve messages + foreach ($session->getFlashBag()->get('notice', array()) as $message) { + echo "
$message
"; + } Session API ~~~~~~~~~~~ @@ -661,32 +664,76 @@ This is however just one application for flash messages. :class:`Symfony\\Component\\HttpFoundation\\Session\\Flash\\FlashBagInterface` has a simple API +* :method:`Symfony\\Component\\HttpFoundation\\Session\\Flash\\FlashBagInterface::add`: + Adds a flash message to the stack of specified type; + * :method:`Symfony\\Component\\HttpFoundation\\Session\\Flash\\FlashBagInterface::set`: - Sets a flash by type; + Sets flashes by type; This method conveniently takes both singles messages as + a ``string`` or multiple messages in an ``array``. * :method:`Symfony\\Component\\HttpFoundation\\Session\\Flash\\FlashBagInterface::get`: - Gets a flash by type and clears the flash from the bag; + Gets flashes by type and clears those flashes from the bag; * :method:`Symfony\\Component\\HttpFoundation\\Session\\Flash\\FlashBagInterface::setAll`: - Sets an array of flashes by type => message; + Sets all flashes, accepts a keyed array of arrays ``type => array(messages)``; * :method:`Symfony\\Component\\HttpFoundation\\Session\\Flash\\FlashBagInterface::all`: - Gets all flashes and clears the flashes from the bag; + Gets all flashes (as a keyed array of arrays) and clears the flashes from the bag; * :method:`Symfony\\Component\\HttpFoundation\\Session\\Flash\\FlashBagInterface::peek`: - Gets a flash by type (read only); + Gets flashes by type (read only); * :method:`Symfony\\Component\\HttpFoundation\\Session\\Flash\\FlashBagInterface::peekAll`: - Gets all flashes (read only); + Gets all flashes (read only) as keyed array of arrays; * :method:`Symfony\\Component\\HttpFoundation\\Session\\Flash\\FlashBagInterface::has`: - Returns true if the type exists; + Returns true if the type exists, false if not; * :method:`Symfony\\Component\\HttpFoundation\\Session\\Flash\\FlashBagInterface::keys`: - Returns an array of stored types; + Returns an array of the stored flash types; * :method:`Symfony\\Component\\HttpFoundation\\Session\\Flash\\FlashBagInterface::clear`: - Clear the bag; + Clears the bag; + +For simple applications it is usually sufficient to have one flash message per +type, for example a confirmatio notice after a form is submitted. However, +flash messages are stored in a keyed array by flash `$type` which means your +application can issue multiple messages for a given type. This allows the API +to be used for more complex messaging in your application. + +Examples of setting multiple flashes:: + + use Symfony\\Component\\HttpFoundation\\Session\\Session; + + $session = new Session(); + $session->start(); + + // add flash messages + $session->getFlashBag()->add('warning', 'Your config file is writable, it should be set read-only'); + $session->getFlashBag()->add('error', 'Failed to update name'); + $session->getFlashBag()->add('error', 'Another error'); + +Displaying the flash messages as follows: + +Simple, display one type of message:: + + // display warnings + foreach ($session->getFlashBag()->get('warning', array()) as $message) { + echo "
$message
"; + } + + // display errors + foreach ($session->getFlashBag()->get('error', array()) as $message) { + echo "
$message
"; + } + +Compact method to process display all flashes at once:: + + foreach ($session->getFlashBag()->all() as $type => $messages) { + foreach ($messages as $message) { + echo "
$message
\n"; + } + } Testability ----------- From 0ac2216f61c8b210a676bdb81dde49496a79071e Mon Sep 17 00:00:00 2001 From: Drak Date: Thu, 22 Mar 2012 15:38:23 +0545 Subject: [PATCH 2/2] [HttpFoundation] Correct typos. --- components/http_foundation.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/components/http_foundation.rst b/components/http_foundation.rst index 6fbee084609..56d905753ae 100644 --- a/components/http_foundation.rst +++ b/components/http_foundation.rst @@ -696,9 +696,9 @@ has a simple API Clears the bag; For simple applications it is usually sufficient to have one flash message per -type, for example a confirmatio notice after a form is submitted. However, +type, for example a confirmation notice after a form is submitted. However, flash messages are stored in a keyed array by flash `$type` which means your -application can issue multiple messages for a given type. This allows the API +application can issue multiple messages for a given type. This allows the API to be used for more complex messaging in your application. Examples of setting multiple flashes::