Skip to content

Commit a704843

Browse files
committed
Fixes and improvements
1 parent 40363bf commit a704843

File tree

1 file changed

+78
-25
lines changed

1 file changed

+78
-25
lines changed

session/database.rst

Lines changed: 78 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,62 @@ installed and configured the `phpredis extension`_.
1919

2020
First, define a Symfony service for the connection to the Redis server:
2121

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+
;
4578
4679
Now pass this Redis connection as an argument of the service associated to the
4780
:class:`Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler`:
@@ -56,13 +89,21 @@ Now pass this Redis connection as an argument of the service associated to the
5689
Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler:
5790
arguments:
5891
- '@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' }
5995
6096
.. code-block:: xml
6197
6298
<!-- config/services.xml -->
6399
<services>
64100
<service id="Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler">
65101
<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> -->
66107
</service>
67108
</services>
68109
@@ -73,7 +114,12 @@ Now pass this Redis connection as an argument of the service associated to the
73114
74115
$container
75116
->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+
);
77123
78124
Next, use the :ref:`handler_id <config-framework-session-handler-id>`
79125
configuration option to tell Symfony to use this service as the session handler:
@@ -425,7 +471,7 @@ the MongoDB connection as argument:
425471
426472
$storageDefinition = $container->autowire(MongoDbSessionHandler::class)
427473
->setArguments([
428-
'...',
474+
new Reference('doctrine_mongodb.odm.default_connection'),
429475
])
430476
;
431477
@@ -464,6 +510,13 @@ configuration option to tell Symfony to use this service as the session handler:
464510
],
465511
]);
466512
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+
467520
That's all! Symfony will now use your MongoDB server to read and write the
468521
session data. You do not need to do anything to initialize your session
469522
collection. However, you may want to add an index to improve garbage collection

0 commit comments

Comments
 (0)