@@ -1150,6 +1150,126 @@ This is the recommended migration workflow:
1150
1150
#. After verifying that the sessions in your application are working, switch
1151
1151
from the migrating handler to the new handler.
1152
1152
1153
+ .. _session-configure-ttl :
1154
+
1155
+ .. index ::
1156
+ single: Sessions, defining TTL
1157
+
1158
+ Configuring the Session TTL
1159
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
1160
+
1161
+ Symfony by default will use PHP's ini setting ``session.gc_maxlifetime `` as
1162
+ session lifetime. When you store sessions in a database, you can also
1163
+ configure your own TTL in the framework configuration or even at runtime.
1164
+
1165
+ .. note ::
1166
+
1167
+ Changing the ini setting is not possible once the session is started so
1168
+ if you want to use a different TTL depending on which user is logged
1169
+ in, you must do it at runtime using the callback method below.
1170
+
1171
+ Configure the TTL
1172
+ .................
1173
+
1174
+ You need to pass the TTL in the options array of the session handler you are using:
1175
+
1176
+ .. configuration-block ::
1177
+
1178
+ .. code-block :: yaml
1179
+
1180
+ # config/services.yaml
1181
+ services :
1182
+ # ...
1183
+ Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler :
1184
+ arguments :
1185
+ - ' @Redis'
1186
+ - { 'ttl': 600 }
1187
+
1188
+ .. code-block :: xml
1189
+
1190
+ <!-- config/services.xml -->
1191
+ <services >
1192
+ <service id =" Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler" >
1193
+ <argument type =" service" id =" Redis" />
1194
+ <argument type =" collection" >
1195
+ <argument key =" ttl" >600</argument >
1196
+ </argument >
1197
+ </service >
1198
+ </services >
1199
+
1200
+ .. code-block :: php
1201
+
1202
+ // config/services.php
1203
+ use Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler;
1204
+
1205
+ $services
1206
+ ->set(RedisSessionHandler::class)
1207
+ ->args([
1208
+ service('Redis'),
1209
+ ['ttl' => 600],
1210
+ ]);
1211
+
1212
+ Configure the TTL Dynamically at Runtime
1213
+ ........................................
1214
+
1215
+ If you would like to have a different TTL for different users or sessions
1216
+ for whatever reason, this is also possible by passing a callback as the TTL
1217
+ value. The callback will be called right before the session is written and
1218
+ has to return an integer which will be used as TTL.
1219
+
1220
+ .. configuration-block ::
1221
+
1222
+ .. code-block :: yaml
1223
+
1224
+ # config/services.yaml
1225
+ services :
1226
+ # ...
1227
+ Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler :
1228
+ arguments :
1229
+ - ' @Redis'
1230
+ - { 'ttl': !closure '@my.ttl.handler' }
1231
+
1232
+ my.ttl.handler :
1233
+ class : Some\InvokableClass # some class with an __invoke() method
1234
+ arguments :
1235
+ # Inject whatever dependencies you need to be able to resolve a TTL for the current session
1236
+ - ' @security'
1237
+
1238
+ .. code-block :: xml
1239
+
1240
+ <!-- config/services.xml -->
1241
+ <services >
1242
+ <service id =" Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler" >
1243
+ <argument type =" service" id =" Redis" />
1244
+ <argument type =" collection" >
1245
+ <argument key =" ttl" type =" closure" id =" my.ttl.handler" />
1246
+ </argument >
1247
+ </service >
1248
+ <!-- some class with an __invoke() method -->
1249
+ <service id =" my.ttl.handler" class =" Some\InvokableClass" >
1250
+ <!-- Inject whatever dependencies you need to be able to resolve a TTL for the current session -->
1251
+ <argument type =" service" id =" security" />
1252
+ </service >
1253
+ </services >
1254
+
1255
+ .. code-block :: php
1256
+
1257
+ // config/services.php
1258
+ use Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler;
1259
+
1260
+ $services
1261
+ ->set(RedisSessionHandler::class)
1262
+ ->args([
1263
+ service('Redis'),
1264
+ ['ttl' => closure(service('my.ttl.handler'))],
1265
+ ]);
1266
+
1267
+ $services
1268
+ // some class with an __invoke() method
1269
+ ->set('my.ttl.handler', 'Some\InvokableClass')
1270
+ // Inject whatever dependencies you need to be able to resolve a TTL for the current session
1271
+ ->args([service('security')]);
1272
+
1153
1273
.. index ::
1154
1274
single: Sessions, saving locale
1155
1275
0 commit comments