Skip to content

Commit c1142dc

Browse files
committed
Merge pull request symfony#2474 from drak/session_php
[WCM] Document PhpSessionStorage
2 parents ba4eadf + ae77706 commit c1142dc

File tree

6 files changed

+98
-0
lines changed

6 files changed

+98
-0
lines changed

components/http_foundation/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ HTTP Foundation
88
sessions
99
session_configuration
1010
session_testing
11+
session_php_bridge
1112
trusting_proxies
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
.. index::
2+
single: HTTP
3+
single: HttpFoundation, Sessions
4+
5+
Integrating with Legacy Sessions
6+
================================
7+
8+
Sometimes it may be necessary to integrate Symfony into a legacy application
9+
where you do not initially have the level of control you require.
10+
11+
As stated elsewhere, Symfony Sessions are designed to replace the use of
12+
PHP's native ``session_*()`` functions and use of the ``$_SESSION``
13+
superglobal. Additionally, it is mandatory for Symfony to start the session.
14+
15+
However when there really are circumstances where this is not possible, it is possible
16+
to use a special storage bridge
17+
:class:`Symfony\\Component\\HttpFoundation\\Session\\Storage\\PhpBridgeSessionStorage`
18+
which is designed to allow Symfony to work with a session started outside of
19+
the Symfony Session framework. You are warned that things can interrupt this
20+
use case unless you are careful: for example legacy application erases ``$_SESSION``.
21+
22+
Typical use of this might look as follows::
23+
24+
<?php
25+
use Symfony\Component\HttpFoundation\Session\Session;
26+
use Symfony\Component\HttpFoundation\Session\Storage\PhpBridgeSessionStorage;
27+
28+
// legacy application configures session
29+
ini_set('session.save_handler', 'files');
30+
ini_set('session.save_path', '/tmp');
31+
session_start();
32+
33+
// Get Symfony to interface with this existing session
34+
$session = new Session(new PhpBridgeSessionStorage());
35+
36+
// symfony will now interface with the existing PHP session
37+
$session->start();
38+
39+
This will allow you to start using the Symfony Session API and allow
40+
migration of your application to Symfony Sessions.
41+
42+
.. note::
43+
44+
Symfony Sessions store data like attributes in special 'Bags' which use a
45+
key in the ``$_SESSION`` superglobal. This means that a Symfony Session
46+
cannot access arbitary keys in ``$_SESSION`` that may be set by the legacy
47+
application, although all the ``$_SESSION`` contents will be saved when
48+
the session is saved.
49+

cookbook/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ The Cookbook
2525
debugging
2626
event_dispatcher/index
2727
request/index
28+
session/index
2829
profiler/index
2930
web_services/index
3031
symfony1

cookbook/map.rst.inc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@
105105

106106
* :doc:`/cookbook/request/mime_type`
107107

108+
* :doc:`/cookbook/session/index`
109+
110+
* :doc:`/cookbook/session/php_bridge`
111+
108112
* :doc:`/cookbook/routing/index`
109113

110114
* :doc:`/cookbook/routing/scheme`

cookbook/sessions/index.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Sessions
2+
========
3+
4+
.. toctree::
5+
:maxdepth: 2
6+
7+
php_bridge

cookbook/sessions/php_bridge.rst

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
.. index::
2+
single: Sessions
3+
4+
Bridge a legacy application with Symfony Sessions
5+
--------------------------------------------------------
6+
7+
.. versionadded:: 2.3
8+
Added ability to integrate with a legacy PHP session
9+
10+
You may take advantage of the PHP Bridge Session when integrating
11+
a legacy application into the Symfony Full Stack Framework when it
12+
may not be possible to avoid the legacy application starting the
13+
session with ``session_start()``
14+
15+
If the application has sets it's own PHP save handler, you can
16+
specify null for the ``handler_id``:
17+
18+
.. code-block:: yml
19+
20+
framework:
21+
session:
22+
storage_id: session.storage.php_bridge
23+
handler_id: ~
24+
25+
Otherwise, if the problem is simply that you cannot avoid the application
26+
starting the session with ``session_start()`` but you can still make use of
27+
a Symfony based session save handler, you can specify the save handle
28+
as in the example below:
29+
30+
.. code-block:: yml
31+
32+
framework:
33+
session:
34+
storage_id: session.storage.php_bridge
35+
handler_id: session.handler.native_file
36+

0 commit comments

Comments
 (0)