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

Commit711aa6b

Browse files
committed
[HttpKernel] Document the MapSessionParameter value resolver
1 parentaf558ed commit711aa6b

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

‎controller/value_resolver.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,13 @@ Symfony ships with the following value resolvers in the
110110
Injects a service if type-hinted with a valid service class or interface. This
111111
works like:doc:`autowiring</service_container/autowiring>`.
112112

113+
:class:`Symfony\\Component\\HttpKernel\\Controller\\ArgumentResolver\\SessionParameterValueResolver.php`
114+
Pass a session parameter to a controller argument.
115+
116+
Because this is a:ref:`targeted value resolver<value-resolver-targeted>`,
117+
you'll have to use the:ref:`MapSessionParameter<session-passing-session-parameter>` attribute
118+
in order to use this resolver.
119+
113120
:class:`Symfony\\Component\\HttpKernel\\Controller\\ArgumentResolver\\SessionValueResolver`
114121
Injects the configured session class implementing ``SessionInterface`` if
115122
type-hinted with ``SessionInterface`` or a class implementing

‎session.rst

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1328,6 +1328,47 @@ has to return an integer which will be used as TTL.
13281328
// Inject whatever dependencies you need to be able to resolve a TTL for the current session
13291329
->args([service('security')]);
13301330
1331+
1332+
.. _session-passing-session-parameter:
1333+
1334+
Passing a session parameter to a controller argument
1335+
----------------------------------------------------
1336+
1337+
A possibility is to pass a session parameter to a controller argument.
1338+
Let's say you declare the following DTO ::
1339+
1340+
namespace App\Model;
1341+
1342+
class UserPreference
1343+
{
1344+
public bool $enableNotification = true;
1345+
public string $locale = "en";
1346+
}
1347+
1348+
You can then use the:class:`Symfony\\Component\\HttpKernel\\Attribute\\MapSessionParameter`
1349+
attribute in your controller::
1350+
1351+
use App\Model\UserPreference;
1352+
use Symfony\Component\HttpFoundation\Response;
1353+
use Symfony\Component\HttpKernel\Attribute\MapSessionParameter;
1354+
1355+
// ...
1356+
1357+
public function dashboard(
1358+
#[MapSessionParameter] UserPreference $userPreferences
1359+
): Response
1360+
{
1361+
// ...
1362+
}
1363+
1364+
The method will then receive an instance of your DTO linked with the session.
1365+
Value set in this object will be persisted in session and will be available for reading later.
1366+
1367+
The name of the parameter is used as the session key.
1368+
1369+
This attribute work with classes and interfaces.
1370+
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.
1371+
13311372
.. _locale-sticky-session:
13321373

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp