Skip to content

Commit 0c35171

Browse files
committed
-
1 parent fb9bec2 commit 0c35171

File tree

2 files changed

+77
-54
lines changed

2 files changed

+77
-54
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: 43 additions & 54 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

@@ -480,11 +500,17 @@ a Symfony service for the connection to the Redis server:
480500

481501
.. configuration-block::
482502

483-
.. code-block:: yaml
484-
485503
# config/services.yaml
486504
services:
487505
# ...
506+
Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler:
507+
arguments:
508+
- '@Redis'
509+
# you can optionally pass an array of options. The only options are 'prefix' and 'ttl',
510+
# which define the prefix to use for the keys to avoid collision on the Redis server
511+
# and the expiration time for any given entry (in seconds), defaults are 'sf_s' and null:
512+
# - { 'prefix': 'my_prefix', 'ttl': 600 }
513+
488514
Redis:
489515
# you can also use \RedisArray, \RedisCluster or \Predis\Client classes
490516
class: Redis
@@ -527,53 +553,7 @@ a Symfony service for the connection to the Redis server:
527553
<argument>%env(REDIS_PASSWORD)%</argument>
528554
</call> -->
529555
</service>
530-
</services>
531-
</container>
532-
533-
.. code-block:: php
534556
535-
// ...
536-
$container
537-
// you can also use \RedisArray, \RedisCluster or \Predis\Client classes
538-
->register('Redis', \Redis::class)
539-
->addMethodCall('connect', ['%env(REDIS_HOST)%', '%env(int:REDIS_PORT)%'])
540-
// uncomment the following if your Redis server requires a password:
541-
// ->addMethodCall('auth', ['%env(REDIS_PASSWORD)%'])
542-
// uncomment the following if your Redis server requires a user and a password (when user is not default):
543-
// ->addMethodCall('auth', ['%env(REDIS_USER)%', '%env(REDIS_PASSWORD)%'])
544-
;
545-
546-
Now pass this ``\Redis`` connection as an argument of the service associated to the
547-
:class:`Symfony\\Component\\HttpFoundation\\Session\\Storage\\Handler\\RedisSessionHandler`.
548-
This argument can also be a ``\RedisArray``, ``\RedisCluster``, ``\Predis\Client``,
549-
and ``RedisProxy``:
550-
551-
.. configuration-block::
552-
553-
.. code-block:: yaml
554-
555-
# config/services.yaml
556-
services:
557-
# ...
558-
Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler:
559-
arguments:
560-
- '@Redis'
561-
# you can optionally pass an array of options. The only options are 'prefix' and 'ttl',
562-
# which define the prefix to use for the keys to avoid collision on the Redis server
563-
# and the expiration time for any given entry (in seconds), defaults are 'sf_s' and null:
564-
# - { 'prefix': 'my_prefix', 'ttl': 600 }
565-
566-
.. code-block:: xml
567-
568-
<!-- config/services.xml -->
569-
<!-- config/services.xml -->
570-
<?xml version="1.0" encoding="UTF-8" ?>
571-
<container xmlns="http://symfony.com/schema/dic/services"
572-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
573-
xsi:schemaLocation="http://symfony.com/schema/dic/services
574-
https://symfony.com/schema/dic/services/services-1.0.xsd">
575-
576-
<services>
577557
<service id="Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler">
578558
<argument type="service" id="Redis"/>
579559
<!-- you can optionally pass an array of options. The only options are 'prefix' and 'ttl',
@@ -594,14 +574,23 @@ and ``RedisProxy``:
594574
use Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler;
595575
596576
$container
577+
// you can also use \RedisArray, \RedisCluster or \Predis\Client classes
578+
->register('Redis', \Redis::class)
579+
->addMethodCall('connect', ['%env(REDIS_HOST)%', '%env(int:REDIS_PORT)%'])
580+
// uncomment the following if your Redis server requires a password:
581+
// ->addMethodCall('auth', ['%env(REDIS_PASSWORD)%'])
582+
// uncomment the following if your Redis server requires a user and a password (when user is not default):
583+
// ->addMethodCall('auth', ['%env(REDIS_USER)%', '%env(REDIS_PASSWORD)%'])
584+
597585
->register(RedisSessionHandler::class)
598586
->addArgument(
599587
new Reference('Redis'),
600588
// you can optionally pass an array of options. The only options are 'prefix' and 'ttl',
601589
// which define the prefix to use for the keys to avoid collision on the Redis server
602590
// and the expiration time for any given entry (in seconds), defaults are 'sf_s' and null:
603591
// ['prefix' => 'my_prefix', 'ttl' => 600],
604-
);
592+
)
593+
;
605594
606595
Next, use the :ref:`handler_id <config-framework-session-handler-id>`
607596
configuration option to tell Symfony to use this service as the session handler:

0 commit comments

Comments
 (0)