Skip to content

Commit bd3e58e

Browse files
committed
Merge pull request #2475 from drak/session_demand
[WCM] Document start on demand feature.
2 parents 73e5095 + 35513bc commit bd3e58e

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed

components/http_foundation/session_configuration.rst

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ examples if you wish to write your own.
7979
Example usage::
8080

8181
use Symfony\Component\HttpFoundation\Session\Session;
82-
use Symfony\Component\HttpFoundation\Session\Storage\SessionStorage;
82+
use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
8383
use Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler;
8484

8585
$storage = new NativeSessionStorage(array(), new PdoSessionHandler());
@@ -217,6 +217,45 @@ particular cookie by reading the ``getLifetime()`` method::
217217
The expiry time of the cookie can be determined by adding the created
218218
timestamp and the lifetime.
219219

220+
Session start-on-demand
221+
~~~~~~~~~~~~~~~~~~~~~~~
222+
223+
.. versionadded:: 2.3
224+
Control over session "start-on-demand" was added in Symfony 2.3.
225+
226+
In versions 2.1-2.2, Symfony Sessions automatically invoked ``$session->start()`` when
227+
any attempt was made to access session data (effectively 'start on demand').
228+
From Symfony 2.3 this behaviour can be controlled.
229+
230+
There are three modes defined by
231+
:class:`Symfony\\Component\\HttpFoundation\\Session\\Storage\\SessionStorageInterface`
232+
233+
The settings are as follows:
234+
235+
- ``SessionStorageInterface::NO_START_ON_DEMAND_STRICT`` - The session will not be started on demand
236+
and any attempt to read or write session data will result in a ``\RuntimeException``
237+
- ``SessionStorageInterface::START_ON_DEMAND`` - The session will be started if it hasn't already been
238+
when any attempt is made to read or write session data. This setting reflects the default behaviour
239+
since Symfony 2.1
240+
- ``SessionStorageInterface::NO_START_ON_DEMAND_LAX`` - The sessions will not be started on demand
241+
when session data is read or written to. It will allow access to the unitialized ``BagInterface``.
242+
If this session is subsequently started manually after data is written to a ``BagInterface`` will
243+
be overwritten (by the session data read from persistence).
244+
245+
You can configure these by injecting a configured storage engine into the session::
246+
247+
<?php
248+
use Symfony\Component\HttpFoundation\Session\Session;
249+
use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
250+
use Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface;
251+
use Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler;
252+
253+
$storage = new NativeSessionStorage(array(),
254+
new PdoSessionHandler(),
255+
SessionStorageInterface::NO_START_ON_DEMAND_STRICT);
256+
$session = new Session($storage);
257+
258+
220259
PHP 5.4 compatibility
221260
~~~~~~~~~~~~~~~~~~~~~
222261

reference/configuration/framework.rst

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ Configuration
2626
* enabled
2727
* field_name
2828
* `session`_
29+
* `name`_
30+
* `mock_name`_
31+
* `auto_start`_
32+
* `on_demand`_
2933
* `cookie_lifetime`_
3034
* `cookie_path`_
3135
* `cookie_domain`_
@@ -152,6 +156,43 @@ csrf_protection
152156
session
153157
~~~~~~~
154158

159+
on_demand
160+
.........
161+
162+
**type**: ``string`` **default**: ``on``
163+
164+
Can be values
165+
166+
- ``on`` - start automatically if not started upon session read/write
167+
- ``off`` - do not start session automatically on data read/write, if an attempt is
168+
make to do so, throw a ``\RuntimeException``
169+
- ``off_lax`` - do not start session automatically on data read/write, but if an attempt
170+
is made to read or write to the session, allow access to the relevent bag.
171+
If data is written to the bags and a session is subsequently started, it will be
172+
overwritten.
173+
174+
auto_start
175+
..........
176+
177+
**type**: ``Boolean`` **default**: ``false``
178+
179+
This controls the ``SessionListener`` which will automatically start the session
180+
during the Request cycle.
181+
182+
name
183+
....
184+
185+
**type**: ``string``
186+
187+
Sets the session cookie name
188+
189+
mock_name
190+
.........
191+
192+
**type**: ``string``
193+
194+
Sets the mock session cookie name
195+
155196
cookie_lifetime
156197
...............
157198

@@ -466,6 +507,8 @@ Full Default Configuration
466507
session:
467508
storage_id: session.storage.native
468509
handler_id: session.handler.native_file
510+
auto_start: false
511+
on_demand: on #on, off or off_lax
469512
name: ~
470513
cookie_lifetime: ~
471514
cookie_path: ~

0 commit comments

Comments
 (0)