Skip to content

Added docs for the SessionValueResolver #7322

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
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
34 changes: 25 additions & 9 deletions controller.rst
Original file line number Diff line number Diff line change
Expand Up @@ -348,18 +348,17 @@ Symfony provides a nice session object that you can use to store information
about the user between requests. By default, Symfony stores the attributes in a
cookie by using native PHP sessions.

To retrieve the session, call
:method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller::getSession`
method on the ``Request`` object. This method returns a
:class:`Symfony\\Component\\HttpFoundation\\Session\\SessionInterface` with easy
methods for storing and fetching things from the session::

use Symfony\Component\HttpFoundation\Request;
.. versionadded:: 3.3
The ability to request a ``Session`` in actions was introduced in Symfony 3.3.

public function indexAction(Request $request)
{
$session = $request->getSession();
To retrieve the session, add the :class:`Symfony\\Component\\HttpFoundation\\Session\\SessionInterface`
type-hint to your argument and Symfony will provide you with a session::

use Symfony\Component\HttpFoundation\Session\SessionInterface;

public function indexAction(SessionInterface $session)
{
// store an attribute for reuse during a later user request
$session->set('foo', 'bar');

Expand All @@ -372,6 +371,23 @@ methods for storing and fetching things from the session::

Stored attributes remain in the session for the remainder of that user's session.

.. tip::

Every ``SessionInterface`` implementation is supported. If you have your
own implementation, type-hint this in the arguments instead.

As a developer, you might prefer not to extend the ``Controller``. To use the
Copy link
Member

Choose a reason for hiding this comment

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

This paragraph should be moved to the end of the next section ("Flash Messages") and can maybe be put in a .. sidebar:: or .. tip:: directive instead.

flash message functionality, you can request the flash bag from the
:class:`Symfony\\Component\\HttpFoundation\\Session\\Session`::

use Symfony\Component\HttpFoundation\Session\Session;

public function indexAction(Session $session)
{
// getFlashBag is not available in the SessionInterface and requires the Session
$flashBag = $session->getFlashBag();
}

.. index::
single: Session; Flash messages

Expand Down
8 changes: 8 additions & 0 deletions controller/argument_value_resolver.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ functionality.
Functionality Shipped with the HttpKernel
-----------------------------------------

.. versionadded:: 3.3
The ``SessionValueResolver`` was introduced in Symfony 3.3.

Symfony ships with four value resolvers in the HttpKernel component:
Copy link
Member

Choose a reason for hiding this comment

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

four -> five


:class:`Symfony\\Component\\HttpKernel\\Controller\\ArgumentResolver\\RequestAttributeValueResolver`
Expand All @@ -27,6 +30,11 @@ Symfony ships with four value resolvers in the HttpKernel component:
Injects the current ``Request`` if type-hinted with ``Request`` or a class
extending ``Request``.

:class:`Symfony\\Component\\HttpKernel\\Controller\\ArgumentResolver\\SessionValueResolver`
Injects the configured session class extending ``SessionInterface`` if
type-hinted with ``SessionInterface`` or a class extending
``SessionInterface``.

:class:`Symfony\\Component\\HttpKernel\\Controller\\ArgumentResolver\\DefaultValueResolver`
Will set the default value of the argument if present and the argument
is optional.
Expand Down
8 changes: 3 additions & 5 deletions quick_tour/the_controller.rst
Original file line number Diff line number Diff line change
Expand Up @@ -299,12 +299,10 @@ in a cookie by using native PHP sessions.
Storing and retrieving information from the session can be easily achieved
from any controller::

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Session;

public function indexAction(Request $request)
public function indexAction(Session $session)
{
$session = $request->getSession();

// store an attribute for reuse during a later user request
$session->set('foo', 'bar');

Expand All @@ -319,7 +317,7 @@ You can also store "flash messages" that will auto-delete after the next
request. They are useful when you need to set a success message before
redirecting the user to another page (which will then show the message)::

public function indexAction(Request $request)
public function indexAction()
{
// ...

Expand Down