Skip to content

Commit 5f41570

Browse files
author
Drak
committed
Document start on demand feature.
1 parent 6b4794c commit 5f41570

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

components/http_foundation/session_configuration.rst

Lines changed: 36 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,41 @@ 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 :class:`Symfony\\Component\\HttpFoundation\\Session\\Storage\\SessionStorageInterface`
231+
232+
The settings are as follows:
233+
234+
- `SessionStorageInterface::NO_START_ON_DEMAND_STRICT` - The session will not be started on demand
235+
and any attempt to read or write session data will result in a `\RuntimeException`
236+
- `SessionStorageInterface::START_ON_DEMAND` - The session will be started if it hasn't already been
237+
when any attempt is made to read ro write session data.
238+
- `SessionStorageInterface::NO_START_ON_DEMAND_LAX` - The sessions will not be started on demand
239+
when session data is read or written to. It will allow access to the unitialized `BagInterface`.
240+
If this session is subsequently started manually after data is written to a `BagInterface` will
241+
be overwritten (by the session data read from persistence).
242+
243+
You can configure these by injecting a configured storage engine into the session::
244+
245+
<?php
246+
use Symfony\Component\HttpFoundation\Session\Session;
247+
use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
248+
use Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface;
249+
use Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler;
250+
251+
$storage = new NativeSessionStorage(array(), new PdoSessionHandler(), SessionStorageInterface::NO_START_ON_DEMAND_STRICT);
252+
$session = new Session($storage);
253+
254+
220255
PHP 5.4 compatibility
221256
~~~~~~~~~~~~~~~~~~~~~
222257

reference/configuration/framework.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ Configuration
2626
* enabled
2727
* field_name
2828
* `session`_
29+
* `auto_start`_
30+
* `on_demand`_
2931
* `cookie_lifetime`_
3032
* `cookie_path`_
3133
* `cookie_domain`_
@@ -462,6 +464,8 @@ Full Default Configuration
462464
session:
463465
storage_id: session.storage.native
464466
handler_id: session.handler.native_file
467+
auto_start: false
468+
on_demand: on #on, off or off_lax
465469
name: ~
466470
cookie_lifetime: ~
467471
cookie_path: ~

0 commit comments

Comments
 (0)