@@ -19,29 +19,62 @@ installed and configured the `phpredis extension`_.
19
19
20
20
First, define a Symfony service for the connection to the Redis server:
21
21
22
- .. code-block :: yaml
23
-
24
- # config/services.yaml
25
- services :
26
- # ...
27
- Redis :
28
- class : Redis
29
- calls :
30
- - method : connect
31
- arguments :
32
- - ' %env(REDIS_HOST)%'
33
- - ' %env(int:REDIS_PORT)%'
34
-
35
- # uncomment the following if your Redis server requires a password
36
- # - method: auth
37
- # arguments:
38
- # - '%env(REDIS_PASSWORD)%'
39
-
40
- # uncomment the following if your Redis server defines a key prefix
41
- # - method: setOption
42
- # arguments:
43
- # - !php/const Redis::OPT_PREFIX
44
- # - 'my_prefix'
22
+ .. configuration-block ::
23
+
24
+ .. code-block :: yaml
25
+
26
+ # config/services.yaml
27
+ services :
28
+ # ...
29
+ Redis :
30
+ # you can also use \RedisArray, \RedisCluster or \Predis\Client classes
31
+ class : Redis
32
+ calls :
33
+ - method : connect
34
+ arguments :
35
+ - ' %env(REDIS_HOST)%'
36
+ - ' %env(int:REDIS_PORT)%'
37
+
38
+ # uncomment the following if your Redis server requires a password
39
+ # - method: auth
40
+ # arguments:
41
+ # - '%env(REDIS_PASSWORD)%'
42
+
43
+ .. code-block :: xml
44
+
45
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
46
+ <container xmlns =" http://symfony.com/schema/dic/services"
47
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
48
+ xsi : schemaLocation =" http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd" >
49
+
50
+ <services >
51
+ <!-- you can also use \RedisArray, \RedisCluster or \Predis\Client classes -->
52
+ <service id =" Redis" class =" Redis" >
53
+ <call method =" connect" >
54
+ <argument >%env(REDIS_HOST)%</argument >
55
+ <argument >%env(int:REDIS_PORT)%</argument >
56
+ </call >
57
+
58
+ <!-- uncomment the following if your Redis server requires a password:
59
+ <call method="auth">
60
+ <argument>%env(REDIS_PASSWORD)%</argument>
61
+ </call> -->
62
+ </service >
63
+ </services >
64
+ </container >
65
+
66
+ .. code-block :: php
67
+
68
+ use Symfony\Component\DependencyInjection\Reference;
69
+
70
+ // ...
71
+ $container
72
+ // you can also use \RedisArray, \RedisCluster or \Predis\Client classes
73
+ ->register('Redis', \Redis::class)
74
+ ->addMethodCall('connect', ['%env(REDIS_HOST)%', '%env(int:REDIS_PORT)%'])
75
+ // uncomment the following if your Redis server requires a password:
76
+ // ->addMethodCall('auth', ['%env(REDIS_PASSWORD)%'])
77
+ ;
45
78
46
79
Now pass this Redis connection as an argument of the service associated to the
47
80
:class: `Symfony\C omponent\H ttpFoundation\S ession\S torage\H andler\R edisSessionHandler `:
@@ -56,13 +89,21 @@ Now pass this Redis connection as an argument of the service associated to the
56
89
Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler :
57
90
arguments :
58
91
- ' @Redis'
92
+ # you can optionally pass an array of options. The only option is 'prefix',
93
+ # which defines the prefix to use for the keys to avoid collision on the Redis server
94
+ # - { prefix: 'my_prefix' }
59
95
60
96
.. code-block :: xml
61
97
62
98
<!-- config/services.xml -->
63
99
<services >
64
100
<service id =" Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler" >
65
101
<argument type =" service" id =" Redis" />
102
+ <!-- you can optionally pass an array of options. The only option is 'prefix',
103
+ which defines the prefix to use for the keys to avoid collision on the Redis server:
104
+ <argument type="collection">
105
+ <argument key="prefix">my_prefix</argument>
106
+ </argument> -->
66
107
</service >
67
108
</services >
68
109
@@ -73,7 +114,12 @@ Now pass this Redis connection as an argument of the service associated to the
73
114
74
115
$container
75
116
->register(RedisSessionHandler::class)
76
- ->addArgument(new Reference('Redis'));
117
+ ->addArgument(
118
+ new Reference('Redis'),
119
+ // you can optionally pass an array of options. The only option is 'prefix',
120
+ // which defines the prefix to use for the keys to avoid collision on the Redis server:
121
+ // ['prefix' => 'my_prefix'],
122
+ );
77
123
78
124
Next, use the :ref: `handler_id <config-framework-session-handler-id >`
79
125
configuration option to tell Symfony to use this service as the session handler:
@@ -425,7 +471,7 @@ the MongoDB connection as argument:
425
471
426
472
$storageDefinition = $container->autowire(MongoDbSessionHandler::class)
427
473
->setArguments([
428
- '...' ,
474
+ new Reference('doctrine_mongodb.odm.default_connection') ,
429
475
])
430
476
;
431
477
@@ -464,6 +510,13 @@ configuration option to tell Symfony to use this service as the session handler:
464
510
],
465
511
]);
466
512
513
+ .. note ::
514
+
515
+ MongoDB ODM 1.x only works with the legacy driver, which is no longer
516
+ supported by the Symfony session class. Install the ``alcaeus/mongo-php-adapter ``
517
+ package to retrieve the underlying ``\MongoDB\Client `` object or upgrade to
518
+ MongoDB ODM 2.0.
519
+
467
520
That's all! Symfony will now use your MongoDB server to read and write the
468
521
session data. You do not need to do anything to initialize your session
469
522
collection. However, you may want to add an index to improve garbage collection
0 commit comments