|
| 1 | +.. index:: |
| 2 | + single: Sessions, session proxy, proxy |
| 3 | + |
| 4 | +Session Proxy Examples |
| 5 | +---------------------- |
| 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:: |
| 10 | + |
| 11 | + use Symfony\Component\HttpFoundation\Session\Session; |
| 12 | + use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage; |
| 13 | + use Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionStorage; |
| 14 | + |
| 15 | + $proxy = new YourProxy(new PdoSessionStorage()); |
| 16 | + $session = new Session(new NativeSessionStorage($proxy)); |
| 17 | + |
| 18 | +Below, you'll learn two real examples that can be used for ``YourProxy``: |
| 19 | +encryption of session data and readonly guest session. |
| 20 | + |
| 21 | +Encryption of Session Data |
| 22 | +-------------------------- |
| 23 | + |
| 24 | +If you wanted to encrypt the session data, you could use the proxy to encrypt |
| 25 | +and decrypt the session as required:: |
| 26 | + |
| 27 | + use Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy; |
| 28 | + |
| 29 | + class EncryptedSessionProxy extends SessionHandlerProxy |
| 30 | + { |
| 31 | + private $key; |
| 32 | + |
| 33 | + public function __construct(\SessionHandlerInterface $handler, $key) |
| 34 | + { |
| 35 | + $this->key = $key; |
| 36 | + |
| 37 | + parent::__construct($handler); |
| 38 | + } |
| 39 | + |
| 40 | + public function read($id) |
| 41 | + { |
| 42 | + $data = parent::write($id, $data); |
| 43 | + |
| 44 | + return mcrypt_decrypt(\MCRYPT_3DES, $this->key, $data); |
| 45 | + } |
| 46 | + |
| 47 | + public function write($id, $data) |
| 48 | + { |
| 49 | + $data = mcrypt_encrypt(\MCRYPT_3DES, $this->key, $data); |
| 50 | + |
| 51 | + return parent::write($id, $data); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | +Readonly Guest Sessions |
| 56 | +----------------------- |
| 57 | + |
| 58 | +There are some applications where a session is required for guest users, but |
| 59 | +there is no particular need to persist the session. In this case you can |
| 60 | +intercept the session before it writes:: |
| 61 | + |
| 62 | + use Foo\User; |
| 63 | + use Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy; |
| 64 | + |
| 65 | + class ReadOnlyGuestSessionProxy extends SessionHandlerProxy |
| 66 | + { |
| 67 | + private $user; |
| 68 | + |
| 69 | + public function __construct(\SessionHandlerInterface $handler, User $user) |
| 70 | + { |
| 71 | + $this->user = $user; |
| 72 | + |
| 73 | + parent::__construct($handler); |
| 74 | + } |
| 75 | + |
| 76 | + public function write($id, $data) |
| 77 | + { |
| 78 | + if ($this->user->isGuest()) { |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + return parent::write($id, $data); |
| 83 | + } |
| 84 | + } |
0 commit comments