Skip to content

[Cookbook] Added some examples of session save handler proxies #2562

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from May 4, 2013
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions cookbook/sessions/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Sessions
========

.. toctree::
:maxdepth: 2

proxy_examples
90 changes: 90 additions & 0 deletions cookbook/sessions/proxy_examples.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
.. index::
single: Sessions, session proxy, proxy

Session Proxy Examples
----------------------

The session proxy mechanism has a variety of uses, in this
example we'll demonstrate two common uses. Rather than injecting
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

always avoid a form of 'we' in the documentation

the session handler as normal, you inject the handler into the proxy
and regsiter the proxy with the session::

<?php
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
use Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionStorage;

$proxy = new YourProxy(new PdoSessionStorage());
$session = new Session(new NativeSessionStorage($proxy));

The example below show should give some ideas for ``YourProxy``.


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this empty line

Encryption of Session Data
--------------------------

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

<?php
use Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy;

class NoGuestUserSessionProxy extends SessionHandlerProxy
{
private $key;

public function __construct(\SessionHandlerInterface $handler, $key)
{
$this->key = $key;

parent::__construct($handler);
}

public function read($id)
{
$data = parent::write($id, $data);

return mcrypt_decrypt(\MCRYPT_3DES, $this->key, $data);
}

public function write($id, $data)
{
$data = mcrypt_encrypt(\MCRYPT_3DES, $this->key, $data);

return parent::write($id, $data);
}
}


Readonly Guest sessions
-----------------------

There are some applications where a session is required for guest
users, but there is no particular need persist the session. In this
case you can intercept the session before it writes::

<?php
use Foo\User;
use Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy;

class ReadOnlyGuestSessionProxy extends SessionHandlerProxy
{
private $user;

public function __construct(\SessionHandlerInterface $handler, User $user)
{
$this->user = $user;

parent::__construct($handler);
}

public function write($id, $data)
{
if ($this->user->isGuest()) {
return;
}

return parent::write($id, $data);
}
}