Skip to content

Commit fc4a4e6

Browse files
committed
minor #5892 Updated the session proxy article (javiereguiluz)
This PR was submitted for the 2.3 branch but it was merged into the 2.7 branch instead (closes #5892). Discussion ---------- Updated the session proxy article | Q | A | ------------- | --- | Doc fix? | yes | New docs? | no | Applies to | all | Fixed tickets | #5844 I'm not sure if this is what @xabbuh was talking about in the related issue. Commits ------- a09d1f2 Updated the session proxy article
2 parents dd3f08f + a09d1f2 commit fc4a4e6

File tree

1 file changed

+81
-12
lines changed

1 file changed

+81
-12
lines changed

cookbook/session/proxy_examples.rst

Lines changed: 81 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,92 @@
44
Session Proxy Examples
55
======================
66

7-
The session proxy mechanism has a variety of uses and this example demonstrates
8-
two common uses. Rather than injecting the session handler as normal, a handler
9-
is injected into the proxy and registered with the session storage driver::
7+
The session proxy mechanism has a variety of uses and this article demonstrates
8+
two common uses. Rather than using the regular session handler, you can create
9+
a custom save handler just by defining a class that extends the
10+
:class:`Symfony\\Component\\HttpFoundation\\Session\\Storage\\Proxy\\SessionHandlerProxy`
11+
class.
1012

11-
use Symfony\Component\HttpFoundation\Session\Session;
12-
use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
13-
use Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler;
13+
Then, define a new service related to the custom session handler:
1414

15-
$proxy = new YourProxy(new PdoSessionHandler());
16-
$session = new Session(new NativeSessionStorage(array(), $proxy));
15+
.. configuration-block::
1716

18-
Below, you'll learn two real examples that can be used for ``YourProxy``:
19-
encryption of session data and readonly guest sessions.
17+
.. code-block:: yaml
18+
19+
# app/config/services.yml
20+
services:
21+
app.session_handler:
22+
class: AppBundle\Session\CustomSessionHandler
23+
24+
.. code-block:: xml
25+
26+
<!-- app/config/services.xml -->
27+
<?xml version="1.0" encoding="UTF-8" ?>
28+
<container xmlns="http://symfony.com/schema/dic/services"
29+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
30+
xsi:schemaLocation="http://symfony.com/schema/dic/services
31+
http://symfony.com/schema/dic/services/services-1.0.xsd">
32+
33+
<services>
34+
<service id="app.session_handler" class="AppBundle\Session\CustomSessionHandler" />
35+
</services>
36+
</container>
37+
38+
.. code-block:: php
39+
40+
// app/config/config.php
41+
$container->register('app.session_handler', 'AppBundle\Session\CustomSessionHandler');
42+
43+
Finally, use the ``framework.session.handler_id`` configuration option to tell
44+
Symfony to use your own session handler instead of the default one:
45+
46+
.. configuration-block::
47+
48+
.. code-block:: yaml
49+
50+
# app/config/config.yml
51+
framework:
52+
session:
53+
# ...
54+
handler_id: app.session_handler
55+
56+
.. code-block:: xml
57+
58+
<!-- app/config/config.xml -->
59+
<?xml version="1.0" encoding="UTF-8" ?>
60+
<container xmlns="http://symfony.com/schema/dic/services"
61+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
62+
xsi:schemaLocation="http://symfony.com/schema/dic/services
63+
http://symfony.com/schema/dic/services/services-1.0.xsd">
64+
<framework:config>
65+
<framework:session handler-id="app.session_handler" ... />
66+
</framework:config>
67+
</container>
68+
69+
.. code-block:: php
70+
71+
// app/config/config.php
72+
$container->loadFromExtension('framework', array(
73+
// ...
74+
'session' => array(
75+
// ...
76+
'handler_id' => 'app.session_handler',
77+
),
78+
));
79+
80+
Keep reading the next sections to learn how to use the session handlers in practice
81+
to solve two common use cases: encrypt session information and define readonly
82+
guest sessions.
2083

2184
Encryption of Session Data
2285
--------------------------
2386

2487
If you wanted to encrypt the session data, you could use the proxy to encrypt
2588
and decrypt the session as required::
2689

90+
// src/AppBundle/Session/EncryptedSessionProxy.php
91+
namespace AppBundle\Session;
92+
2793
use Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy;
2894

2995
class EncryptedSessionProxy extends SessionHandlerProxy
@@ -59,10 +125,13 @@ There are some applications where a session is required for guest users, but
59125
where there is no particular need to persist the session. In this case you
60126
can intercept the session before it is written::
61127

62-
use Foo\User;
128+
// src/AppBundle/Session/ReadOnlySessionProxy.php
129+
namespace AppBundle\Session;
130+
131+
use AppBundle\Entity\User;
63132
use Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy;
64133

65-
class ReadOnlyGuestSessionProxy extends SessionHandlerProxy
134+
class ReadOnlySessionProxy extends SessionHandlerProxy
66135
{
67136
private $user;
68137

0 commit comments

Comments
 (0)