-
-
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
Closed
Closed
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,26 +4,79 @@ | |
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 | ||
|
||
<!-- app/config/services.xml --> | ||
<services> | ||
<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 | ||
$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: | ||
|
||
.. 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 | ||
$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 | ||
-------------------------- | ||
|
||
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 +112,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; | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 -->