Skip to content

Commit a698810

Browse files
committed
-
1 parent fb9bec2 commit a698810

File tree

2 files changed

+59
-5
lines changed

2 files changed

+59
-5
lines changed

controller.rst

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,40 @@ Request object.
394394
Managing the Session
395395
--------------------
396396

397+
You can store special messages, called "flash" messages, on the user's session.
398+
By design, flash messages are meant to be used exactly once: they vanish from
399+
the session automatically as soon as you retrieve them. This feature makes
400+
"flash" messages particularly great for storing user notifications.
401+
402+
For example, imagine you're processing a :doc:`form </forms>` submission::
403+
404+
.. configuration-block::
405+
406+
.. code-block:: php-symfony
407+
408+
use Symfony\Component\HttpFoundation\Request;
409+
use Symfony\Component\HttpFoundation\Response;
410+
// ...
411+
412+
public function update(Request $request): Response
413+
{
414+
// ...
415+
416+
if ($form->isSubmitted() && $form->isValid()) {
417+
// do some sort of processing
418+
419+
$this->addFlash(
420+
'notice',
421+
'Your changes were saved!'
422+
);
423+
// $this->addFlash() is equivalent to $request->getSession()->getFlashBag()->add()
424+
425+
return $this->redirectToRoute(/* ... */);
426+
}
427+
428+
return $this->render(/* ... */);
429+
}
430+
397431
:ref:`Reading <session-intro>` for more information about using Sessions.
398432

399433
.. index::

session.rst

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,11 @@ if you type-hint an argument with :class:`Symfony\\Component\\HttpFoundation\\Re
6666
6767
.. code-block:: php-standalone
6868
69-
use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
7069
use Symfony\Component\HttpFoundation\Session\Session;
71-
use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
7270
7371
$session = new Session();
7472
$session->start();
7573
76-
// Pass a storage and a bag implementation explicitely
77-
$session = new Session(new NativeSessionStorage(), new AttributeBag());
78-
7974
From a Symfony controller, type-hint an argument with
8075
:class:`Symfony\\Component\\HttpFoundation\\RequestStack` or
8176
:class:`Symfony\\Component\\HttpFoundation\\Request`::
@@ -336,6 +331,19 @@ configuration <config-framework-session>` in
336331
;
337332
};
338333
334+
.. code-block:: php-standalone
335+
336+
use Symfony\Component\HttpFoundation\Cookie;
337+
use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
338+
use Symfony\Component\HttpFoundation\Session\Session;
339+
use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
340+
341+
$storage = new NativeSessionStorage([
342+
'cookie_secure' => 'auto',
343+
'cookie_samesite' => Cookie::SAMESITE_LAX,
344+
]);
345+
$session = new Session($storage);
346+
339347
Setting the ``handler_id`` config option to ``null`` means that Symfony will
340348
use the native PHP session mechanism. The session metadata files will be stored
341349
outside of the Symfony application, in a directory controlled by PHP. Although
@@ -390,6 +398,18 @@ session metadata files:
390398
;
391399
};
392400
401+
.. code-block:: php-standalone
402+
403+
use Symfony\Component\HttpFoundation\Cookie;
404+
use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
405+
use Symfony\Component\HttpFoundation\Session\Session;
406+
use Symfony\Component\HttpFoundation\Session\Storage\Handler\NativeFileSessionHandler;
407+
use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
408+
409+
$handler = new NativeFileSessionHandler('/var/sessions');
410+
$storage = new NativeSessionStorage([], $handler);
411+
$session = new Session($storage);
412+
393413
Check out the Symfony config reference to learn more about the other available
394414
:ref:`Session configuration options <config-framework-session>`.
395415

0 commit comments

Comments
 (0)