Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
jtattevin wants to merge1 commit intosymfony:7.1
base:7.1
Choose a base branch
Loading
fromjtattevin:MapSession
Open
Show file tree
Hide file tree
Changes fromall 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 deletionscontroller/value_resolver.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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
Expand Down
41 changes: 41 additions & 0 deletionssession.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1328,6 +1328,47 @@ has to return an integer which will be used as TTL.
// 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;

// ...

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
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp