|
4 | 4 | Session Proxy Examples
|
5 | 5 | ======================
|
6 | 6 |
|
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. |
10 | 12 |
|
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: |
14 | 14 |
|
15 |
| - $proxy = new YourProxy(new PdoSessionHandler()); |
16 |
| - $session = new Session(new NativeSessionStorage(array(), $proxy)); |
| 15 | +.. configuration-block:: |
17 | 16 |
|
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. |
20 | 83 |
|
21 | 84 | Encryption of Session Data
|
22 | 85 | --------------------------
|
23 | 86 |
|
24 | 87 | If you wanted to encrypt the session data, you could use the proxy to encrypt
|
25 | 88 | and decrypt the session as required::
|
26 | 89 |
|
| 90 | + // src/AppBundle/Session/EncryptedSessionProxy.php |
| 91 | + namespace AppBundle\Session; |
| 92 | + |
27 | 93 | use Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy;
|
28 | 94 |
|
29 | 95 | class EncryptedSessionProxy extends SessionHandlerProxy
|
@@ -59,10 +125,13 @@ There are some applications where a session is required for guest users, but
|
59 | 125 | where there is no particular need to persist the session. In this case you
|
60 | 126 | can intercept the session before it is written::
|
61 | 127 |
|
62 |
| - use Foo\User; |
| 128 | + // src/AppBundle/Session/ReadOnlySessionProxy.php |
| 129 | + namespace AppBundle\Session; |
| 130 | + |
| 131 | + use AppBundle\Entity\User; |
63 | 132 | use Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy;
|
64 | 133 |
|
65 |
| - class ReadOnlyGuestSessionProxy extends SessionHandlerProxy |
| 134 | + class ReadOnlySessionProxy extends SessionHandlerProxy |
66 | 135 | {
|
67 | 136 | private $user;
|
68 | 137 |
|
|
0 commit comments