Skip to content

Commit b418615

Browse files
committed
feature #7322 Added docs for the SessionValueResolver (iltar)
This PR was squashed before being merged into the master branch (closes #7322). Discussion ---------- Added docs for the SessionValueResolver Explains the functionality added in symfony/symfony#21164 Commits ------- 88b740e Added docs for the SessionValueResolver
2 parents 68bcaa0 + 88b740e commit b418615

File tree

3 files changed

+36
-14
lines changed

3 files changed

+36
-14
lines changed

controller.rst

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -344,18 +344,17 @@ Symfony provides a nice session object that you can use to store information
344344
about the user between requests. By default, Symfony stores the attributes in a
345345
cookie by using native PHP sessions.
346346

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

353-
use Symfony\Component\HttpFoundation\Request;
348+
.. versionadded:: 3.3
349+
The ability to request a ``Session`` in actions was introduced in Symfony 3.3.
354350

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

354+
use Symfony\Component\HttpFoundation\Session\SessionInterface;
355+
356+
public function indexAction(SessionInterface $session)
357+
{
359358
// store an attribute for reuse during a later user request
360359
$session->set('foo', 'bar');
361360

@@ -368,6 +367,23 @@ methods for storing and fetching things from the session::
368367

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

370+
.. tip::
371+
372+
Every ``SessionInterface`` implementation is supported. If you have your
373+
own implementation, type-hint this in the arguments instead.
374+
375+
As a developer, you might prefer not to extend the ``Controller``. To use the
376+
flash message functionality, you can request the flash bag from the
377+
:class:`Symfony\\Component\\HttpFoundation\\Session\\Session`::
378+
379+
use Symfony\Component\HttpFoundation\Session\Session;
380+
381+
public function indexAction(Session $session)
382+
{
383+
// getFlashBag is not available in the SessionInterface and requires the Session
384+
$flashBag = $session->getFlashBag();
385+
}
386+
371387
.. index::
372388
single: Session; Flash messages
373389

controller/argument_value_resolver.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ functionality.
1515
Functionality Shipped with the HttpKernel
1616
-----------------------------------------
1717

18+
.. versionadded:: 3.3
19+
The ``SessionValueResolver`` was introduced in Symfony 3.3.
20+
1821
Symfony ships with four value resolvers in the HttpKernel component:
1922

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

30+
:class:`Symfony\\Component\\HttpKernel\\Controller\\ArgumentResolver\\SessionValueResolver`
31+
Injects the configured session class extending ``SessionInterface`` if
32+
type-hinted with ``SessionInterface`` or a class extending
33+
``SessionInterface``.
34+
2735
:class:`Symfony\\Component\\HttpKernel\\Controller\\ArgumentResolver\\DefaultValueResolver`
2836
Will set the default value of the argument if present and the argument
2937
is optional.

quick_tour/the_controller.rst

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -299,12 +299,10 @@ in a cookie by using native PHP sessions.
299299
Storing and retrieving information from the session can be easily achieved
300300
from any controller::
301301

302-
use Symfony\Component\HttpFoundation\Request;
302+
use Symfony\Component\HttpFoundation\Session\Session;
303303

304-
public function indexAction(Request $request)
304+
public function indexAction(Session $session)
305305
{
306-
$session = $request->getSession();
307-
308306
// store an attribute for reuse during a later user request
309307
$session->set('foo', 'bar');
310308

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

322-
public function indexAction(Request $request)
320+
public function indexAction()
323321
{
324322
// ...
325323

0 commit comments

Comments
 (0)