From d144c16c9d18066448e793de7f9244923bda4114 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Tue, 17 Nov 2015 10:30:06 +0100 Subject: [PATCH 1/5] Updated the session proxy article --- cookbook/session/proxy_examples.rst | 85 +++++++++++++++++++++++++---- 1 file changed, 73 insertions(+), 12 deletions(-) diff --git a/cookbook/session/proxy_examples.rst b/cookbook/session/proxy_examples.rst index 34c28d78f99..9dd083f2e9c 100644 --- a/cookbook/session/proxy_examples.rst +++ b/cookbook/session/proxy_examples.rst @@ -4,19 +4,74 @@ Session Proxy Examples ====================== -The session proxy mechanism has a variety of uses and this example demonstrates -two common uses. Rather than injecting the session handler as normal, a handler -is injected into the proxy and registered with the session storage driver:: +The session proxy mechanism has a variety of uses and this article demonstrates +two common uses. Rather than using the regular session handler, you can create +a custom save handler just by defining a class that extends the +:class:`Symfony\\Component\\HttpFoundation\\Session\\Storage\\Proxy\\SessionHandlerProxy` +class. - use Symfony\Component\HttpFoundation\Session\Session; - use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage; - use Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler; +Then, define a new service related to the custom session handler: - $proxy = new YourProxy(new PdoSessionHandler()); - $session = new Session(new NativeSessionStorage(array(), $proxy)); +.. configuration-block:: -Below, you'll learn two real examples that can be used for ``YourProxy``: -encryption of session data and readonly guest sessions. + .. code-block:: yaml + + # app/config/services.yml + services: + app.session_handler: + class: AppBundle\Session\CustomSessionHandler + + .. code-block:: xml + + + + + + + .. code-block:: php + + // app/config/config.php + use Symfony\Component\DependencyInjection\Definition; + use Symfony\Component\DependencyInjection\Reference; + + $container->setDefinition('app.session_handler', new Definition('AppBundle\Session\CustomSessionHandler')); + +Finally, use the ``framework.session.handler_id`` configuration option to tell +Symfony yo use your own session handler instead of the default one: + +.. configuration-block:: + + .. code-block:: yaml + + # app/config/config.yml + framework: + session: + # ... + handler_id: app.session_handler + + .. code-block:: xml + + + + + + + .. code-block:: php + + // app/config/config.php + + $container->loadFromExtension('framework', array( + ..., + 'session' => array( + // ..., + 'handler_id' => 'app.session_handler', + ), + )); + +Keep reading the next sections to learn how to use the session handlers in practice +to solve two common use cases: encrypt session information and define readonly +guest sessions. Encryption of Session Data -------------------------- @@ -24,6 +79,9 @@ 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:: + // src/AppBundle/Session/EncryptedSessionProxy.php + namespace AppBundle/Session; + use Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy; class EncryptedSessionProxy extends SessionHandlerProxy @@ -59,10 +117,13 @@ There are some applications where a session is required for guest users, but where there is no particular need to persist the session. In this case you can intercept the session before it is written:: - use Foo\User; + // src/AppBundle/Session/ReadOnlySessionProxy.php + namespace AppBundle/Session; + + use AppBundle\Entity\User; use Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy; - class ReadOnlyGuestSessionProxy extends SessionHandlerProxy + class ReadOnlySessionProxy extends SessionHandlerProxy { private $user; From 253240984b9d8f58a342fd7b6dae9fb8b342ebb2 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Thu, 10 Mar 2016 18:00:23 +0100 Subject: [PATCH 2/5] Fixed namespace typos --- cookbook/session/proxy_examples.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cookbook/session/proxy_examples.rst b/cookbook/session/proxy_examples.rst index 9dd083f2e9c..a88e2aae12b 100644 --- a/cookbook/session/proxy_examples.rst +++ b/cookbook/session/proxy_examples.rst @@ -80,7 +80,7 @@ If you wanted to encrypt the session data, you could use the proxy to encrypt and decrypt the session as required:: // src/AppBundle/Session/EncryptedSessionProxy.php - namespace AppBundle/Session; + namespace AppBundle\Session; use Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy; @@ -118,7 +118,7 @@ where there is no particular need to persist the session. In this case you can intercept the session before it is written:: // src/AppBundle/Session/ReadOnlySessionProxy.php - namespace AppBundle/Session; + namespace AppBundle\Session; use AppBundle\Entity\User; use Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy; From bc47055ae9f9652eb1cb3244cb6c714e44462394 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Mon, 18 Apr 2016 12:14:25 +0200 Subject: [PATCH 3/5] Minor fixes --- cookbook/session/proxy_examples.rst | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/cookbook/session/proxy_examples.rst b/cookbook/session/proxy_examples.rst index a88e2aae12b..ef4d2721251 100644 --- a/cookbook/session/proxy_examples.rst +++ b/cookbook/session/proxy_examples.rst @@ -23,10 +23,9 @@ Then, define a new service related to the custom session handler: .. code-block:: xml + - - + .. code-block:: php @@ -38,7 +37,7 @@ Then, define a new service related to the custom session handler: $container->setDefinition('app.session_handler', new Definition('AppBundle\Session\CustomSessionHandler')); Finally, use the ``framework.session.handler_id`` configuration option to tell -Symfony yo use your own session handler instead of the default one: +Symfony to use your own session handler instead of the default one: .. configuration-block:: @@ -60,11 +59,10 @@ Symfony yo use your own session handler instead of the default one: .. code-block:: php // app/config/config.php - $container->loadFromExtension('framework', array( - ..., + // ... 'session' => array( - // ..., + // ... 'handler_id' => 'app.session_handler', ), )); From b644dc46fc68886e1b537bc7ac01c3609932d1a7 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Mon, 18 Apr 2016 19:28:53 +0200 Subject: [PATCH 4/5] Simplified the example --- cookbook/session/proxy_examples.rst | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/cookbook/session/proxy_examples.rst b/cookbook/session/proxy_examples.rst index ef4d2721251..59c70f8df54 100644 --- a/cookbook/session/proxy_examples.rst +++ b/cookbook/session/proxy_examples.rst @@ -31,10 +31,7 @@ Then, define a new service related to the custom session handler: .. code-block:: php // app/config/config.php - use Symfony\Component\DependencyInjection\Definition; - use Symfony\Component\DependencyInjection\Reference; - - $container->setDefinition('app.session_handler', new Definition('AppBundle\Session\CustomSessionHandler')); + $container->register('app.session_handler', 'AppBundle\Session\CustomSessionHandler'); Finally, use the ``framework.session.handler_id`` configuration option to tell Symfony to use your own session handler instead of the default one: From b0be22f0535c431fb166c52b6afeb206a7332641 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Sat, 21 May 2016 16:15:02 +0200 Subject: [PATCH 5/5] Added the full XML config --- cookbook/session/proxy_examples.rst | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/cookbook/session/proxy_examples.rst b/cookbook/session/proxy_examples.rst index 59c70f8df54..361096d4c92 100644 --- a/cookbook/session/proxy_examples.rst +++ b/cookbook/session/proxy_examples.rst @@ -24,9 +24,16 @@ Then, define a new service related to the custom session handler: .. code-block:: xml - - - + + + + + + + .. code-block:: php @@ -49,9 +56,15 @@ Symfony to use your own session handler instead of the default one: .. code-block:: xml - - - + + + + + + .. code-block:: php