@@ -66,16 +66,11 @@ if you type-hint an argument with :class:`Symfony\\Component\\HttpFoundation\\Re
66
66
67
67
.. code-block :: php-standalone
68
68
69
- use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
70
69
use Symfony\Component\HttpFoundation\Session\Session;
71
- use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
72
70
73
71
$session = new Session();
74
72
$session->start();
75
73
76
- // Pass a storage and a bag implementation explicitely
77
- $session = new Session(new NativeSessionStorage(), new AttributeBag());
78
-
79
74
From a Symfony controller, type-hint an argument with
80
75
:class: `Symfony\\ Component\\ HttpFoundation\\ RequestStack ` or
81
76
:class: `Symfony\\ Component\\ HttpFoundation\\ Request `::
@@ -336,6 +331,19 @@ configuration <config-framework-session>` in
336
331
;
337
332
};
338
333
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
+
339
347
Setting the ``handler_id `` config option to ``null `` means that Symfony will
340
348
use the native PHP session mechanism. The session metadata files will be stored
341
349
outside of the Symfony application, in a directory controlled by PHP. Although
@@ -390,6 +398,18 @@ session metadata files:
390
398
;
391
399
};
392
400
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
+
393
413
Check out the Symfony config reference to learn more about the other available
394
414
:ref: `Session configuration options <config-framework-session >`.
395
415
@@ -480,11 +500,17 @@ a Symfony service for the connection to the Redis server:
480
500
481
501
.. configuration-block ::
482
502
483
- .. code-block :: yaml
484
-
485
503
# config/services.yaml
486
504
services:
487
505
# ...
506
+ Symfony\C omponent\H ttpFoundation\S ession\S torage\H andler\R edisSessionHandler:
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
+
488
514
Redis:
489
515
# you can also use \R edisArray, \R edisCluster or \P redis\C lient classes
490
516
class: Redis
@@ -527,53 +553,7 @@ a Symfony service for the connection to the Redis server:
527
553
<argument>%env(REDIS_PASSWORD)%</argument>
528
554
</call> -->
529
555
</service >
530
- </services >
531
- </container >
532
-
533
- .. code-block :: php
534
556
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 >
577
557
<service id =" Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler" >
578
558
<argument type =" service" id =" Redis" />
579
559
<!-- you can optionally pass an array of options. The only options are 'prefix' and 'ttl',
@@ -594,14 +574,23 @@ and ``RedisProxy``:
594
574
use Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler;
595
575
596
576
$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
+
597
585
->register(RedisSessionHandler::class)
598
586
->addArgument(
599
587
new Reference('Redis'),
600
588
// you can optionally pass an array of options. The only options are 'prefix' and 'ttl',
601
589
// which define the prefix to use for the keys to avoid collision on the Redis server
602
590
// and the expiration time for any given entry (in seconds), defaults are 'sf_s' and null:
603
591
// ['prefix' => 'my_prefix', 'ttl' => 600],
604
- );
592
+ )
593
+ ;
605
594
606
595
Next, use the :ref: `handler_id <config-framework-session-handler-id >`
607
596
configuration option to tell Symfony to use this service as the session handler:
0 commit comments