|
| 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\HttpFoundation\Session\Storage\Handler\RedisSessionHandler; |
| 50 | +
|
| 51 | + $services |
| 52 | + ->set(RedisSessionHandler::class) |
| 53 | + ->args([ |
| 54 | + service('Redis'), |
| 55 | + ['ttl' => 600], |
| 56 | + ]); |
| 57 | +
|
| 58 | +.. _configuring-the-TTL-dynamically-at-runtime: |
| 59 | + |
| 60 | +Configuring the TTL dynamically at runtime |
| 61 | +------------------------------------------ |
| 62 | + |
| 63 | +If you would like to have a different TTL for different |
| 64 | +users or sessions for whatever reason, this is also possible |
| 65 | +by passing a callback as the TTL value. The callback then has |
| 66 | +to return an integer which will be used as TTL. |
| 67 | + |
| 68 | +The callback will be called right before the session is written. |
| 69 | + |
| 70 | +.. configuration-block:: |
| 71 | + |
| 72 | + .. code-block:: yaml |
| 73 | +
|
| 74 | + # config/services.yaml |
| 75 | + services: |
| 76 | + # ... |
| 77 | + Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler: |
| 78 | + arguments: |
| 79 | + - '@Redis' |
| 80 | + - { 'ttl': !closure '@my.ttl.handler' } |
| 81 | + |
| 82 | + my.ttl.handler: |
| 83 | + class: Some\InvokableClass # some class with an __invoke() method |
| 84 | + arguments: |
| 85 | + # Inject whatever dependencies you need to be able to resolve a TTL for the current session |
| 86 | + - '@security' |
| 87 | +
|
| 88 | + .. code-block:: xml |
| 89 | +
|
| 90 | + <!-- config/services.xml --> |
| 91 | + <services> |
| 92 | + <service id="Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler"> |
| 93 | + <argument type="service" id="Redis"/> |
| 94 | + <argument type="collection"> |
| 95 | + <argument key="ttl" type="closure" id="my.ttl.handler"/> |
| 96 | + </argument> |
| 97 | + </service> |
| 98 | + <!-- some class with an __invoke() method --> |
| 99 | + <service id="my.ttl.handler" class="Some\InvokableClass"> |
| 100 | + <!-- Inject whatever dependencies you need to be able to resolve a TTL for the current session --> |
| 101 | + <argument type="service" id="security"/> |
| 102 | + </service> |
| 103 | + </services> |
| 104 | +
|
| 105 | + .. code-block:: php |
| 106 | +
|
| 107 | + // config/services.php |
| 108 | + use Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler; |
| 109 | +
|
| 110 | + $services |
| 111 | + ->set(RedisSessionHandler::class) |
| 112 | + ->args([ |
| 113 | + service('Redis'), |
| 114 | + ['ttl' => closure(service('my.ttl.handler'))], |
| 115 | + ]); |
| 116 | +
|
| 117 | + $services |
| 118 | + // some class with an __invoke() method |
| 119 | + ->set('my.ttl.handler', 'Some\InvokableClass') |
| 120 | + // Inject whatever dependencies you need to be able to resolve a TTL for the current session |
| 121 | + ->args([service('security')]); |
0 commit comments