Skip to content

Commit ddc7ce9

Browse files
committed
Add page about configuring the session TTL
1 parent 6a044b6 commit ddc7ce9

File tree

1 file changed

+119
-0
lines changed

1 file changed

+119
-0
lines changed

session/configuring_ttl.rst

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
.. index::
2+
single: Sessions, defining TTL
3+
4+
Configuring the Session TTL
5+
===========================
6+
7+
Symfony by default will use PHP's ini setting ``session.gc_maxlifetime`` as
8+
session lifetime. However if you :doc:`store sessions in a database </session/database>`
9+
you can also configure your own TTL in the framework configuration or even at runtime.
10+
11+
Changing the ini setting is not possible once the session is started so if you
12+
want to use a different TTL depending on which user is logged in, you really need
13+
to do it at runtime using the callback method below.
14+
15+
.. _configuring-the-TTL:
16+
17+
Configuring the TTL
18+
-------------------
19+
20+
You need to pass the TTL in the options array of the session handler you are using:
21+
22+
.. configuration-block::
23+
24+
.. code-block:: yaml
25+
26+
# config/services.yaml
27+
services:
28+
# ...
29+
Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler:
30+
arguments:
31+
- '@Redis'
32+
- { 'ttl': 600 }
33+
34+
.. code-block:: xml
35+
36+
<!-- config/services.xml -->
37+
<services>
38+
<service id="Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler">
39+
<argument type="service" id="Redis"/>
40+
<argument type="collection">
41+
<argument key="ttl">600</argument>
42+
</argument>
43+
</service>
44+
</services>
45+
46+
.. code-block:: php
47+
48+
// config/services.php
49+
use Symfony\Component\DependencyInjection\Reference;
50+
use Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler;
51+
52+
$container
53+
->register(RedisSessionHandler::class)
54+
->setArguments([
55+
new Reference('Redis'),
56+
['ttl' => 600],
57+
]);
58+
59+
60+
.. _configuring-the-TTL-dynamically-at-runtime:
61+
62+
Configuring the TTL dynamically at runtime
63+
------------------------------------------
64+
65+
If you would like to have a different TTL for different
66+
users or sessions for whatever reason, this is also possible
67+
by passing a callback as the TTL value. The callback then has
68+
to return an integer which will be used as TTL.
69+
70+
The callback will be called right before the session is written.
71+
72+
.. configuration-block::
73+
74+
.. code-block:: yaml
75+
76+
# config/services.yaml
77+
services:
78+
# ...
79+
Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler:
80+
arguments:
81+
- '@Redis'
82+
- { 'ttl': !closure ['@my.ttl.handler'] }
83+
84+
my.ttl.handler:
85+
class: Some\InvokableClass # some class with an __invoke() method
86+
arguments:
87+
# Inject whatever dependencies you need to be able to resolve a TTL for the current session
88+
- @security
89+
90+
.. code-block:: xml
91+
92+
<!-- config/services.xml -->
93+
<services>
94+
<service id="Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler">
95+
<argument type="service" id="Redis"/>
96+
<argument type="collection">
97+
<argument key="ttl">600</argument>
98+
</argument>
99+
</service>
100+
</services>
101+
102+
.. code-block:: php
103+
104+
// config/services.php
105+
use Symfony\Component\DependencyInjection\Reference;
106+
use Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler;
107+
108+
$container
109+
->register(RedisSessionHandler::class)
110+
->setArguments([
111+
new Reference('Redis'),
112+
['ttl' => new Reference('my.ttl.handler')],
113+
]);
114+
115+
$container
116+
// some class with an __invoke() method
117+
->register('my.ttl.handler', 'Some\InvokableClass')
118+
// Inject whatever dependencies you need to be able to resolve a TTL for the current session
119+
->addArgument(new Reference('security'));

0 commit comments

Comments
 (0)