-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Updated the session proxy article #5892
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,26 +4,84 @@ | |
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 | ||
|
||
<services> | ||
<service id="app.session_handler" | ||
class="AppBundle\Session\CustomSessionHandler"> | ||
</service> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's use this: <service id="app.session_handler" class="AppBundle\Session\CustomSessionHandler" /> |
||
</services> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should use a complete XML example here and below. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So it should be: <?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services
http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="app.session_handler" class="AppBundle\Session\CustomSessionHandler" />
</services>
</container> |
||
|
||
.. 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')); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can simply use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm sorry but I don't understand this comment. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. - $container->setDefinition('app.session_handler', new Definition('AppBundle\Session\CustomSessionHandler'));
+ $container->register('app.session_handler', 'AppBundle\Session\CustomSessionHandler'); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks @ogizanagi! It's fixed now. |
||
|
||
Finally, use the ``framework.session.handler_id`` configuration option to tell | ||
Symfony yo use your own session handler instead of the default one: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [...] to use [...] |
||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: yaml | ||
|
||
# app/config/config.yml | ||
framework: | ||
session: | ||
# ... | ||
handler_id: app.session_handler | ||
|
||
.. code-block:: xml | ||
|
||
<!-- app/config/config.xml --> | ||
<framework:config> | ||
<framework:session handler-id="app.session_handler" ... /> | ||
</framework:config> | ||
|
||
.. code-block:: php | ||
|
||
// app/config/config.php | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this blank line should be removed |
||
$container->loadFromExtension('framework', array( | ||
..., | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. // ... |
||
'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 | ||
-------------------------- | ||
|
||
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; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing file header comment:
<!-- app/config/services.xml -->