Skip to content

[HttpKernel] [WCM] Document the MapSessionParameter value resolver #19728

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

Open
wants to merge 1 commit into
base: 7.1
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions controller/value_resolver.rst
Original file line number Diff line number Diff line change
@@ -110,6 +110,13 @@ Symfony ships with the following value resolvers in the
Injects a service if type-hinted with a valid service class or interface. This
works like :doc:`autowiring </service_container/autowiring>`.

:class:`Symfony\\Component\\HttpKernel\\Controller\\ArgumentResolver\\SessionParameterValueResolver.php`
Pass a session parameter to a controller argument.

Because this is a :ref:`targeted value resolver <value-resolver-targeted>`,
you'll have to use the :ref:`MapSessionParameter <session-passing-session-parameter>` attribute
in order to use this resolver.

:class:`Symfony\\Component\\HttpKernel\\Controller\\ArgumentResolver\\SessionValueResolver`
Injects the configured session class implementing ``SessionInterface`` if
type-hinted with ``SessionInterface`` or a class implementing
41 changes: 41 additions & 0 deletions session.rst
Original file line number Diff line number Diff line change
@@ -1328,6 +1328,47 @@
// Inject whatever dependencies you need to be able to resolve a TTL for the current session
->args([service('security')]);
.. _session-passing-session-parameter:

Passing a session parameter to a controller argument
----------------------------------------------------

A possibility is to pass a session parameter to a controller argument.
Let's say you declare the following DTO ::

namespace App\Model;

class UserPreference
{
public bool $enableNotification = true;
public string $locale = "en";
}

You can then use the :class:`Symfony\\Component\\HttpKernel\\Attribute\\MapSessionParameter`
attribute in your controller::

use App\Model\UserPreference;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\MapSessionParameter;

Check failure on line 1353 in session.rst

GitHub Actions / Code Blocks

[Missing class] Class, interface or trait with name "Symfony\Component\HttpKernel\Attribute\MapSessionParameter" does not exist

// ...

public function dashboard(
#[MapSessionParameter] UserPreference $userPreferences
): Response
{
// ...
}

The method will then receive an instance of your DTO linked with the session.
Value set in this object will be persisted in session and will be available for reading later.

The name of the parameter is used as the session key.

This attribute work with classes and interfaces.
If you use an interface, you must provide a default value or make the parameter nullable, if the session isn't set, it will not be possible to create a new instance when resolving the value.

.. _locale-sticky-session:

Making the Locale "Sticky" during a User's Session