Skip to content

Commit 88b740e

Browse files
Iltar van der Bergxabbuh
Iltar van der Berg
authored andcommitted
Added docs for the SessionValueResolver
1 parent 89cba1e commit 88b740e

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
@@ -348,18 +348,17 @@ Symfony provides a nice session object that you can use to store information
348348
about the user between requests. By default, Symfony stores the attributes in a
349349
cookie by using native PHP sessions.
350350

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

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

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

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

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

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

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

controller/argument_value_resolver.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ functionality.
1818
Functionality Shipped with the HttpKernel
1919
-----------------------------------------
2020

21+
.. versionadded:: 3.3
22+
The ``SessionValueResolver`` was introduced in Symfony 3.3.
23+
2124
Symfony ships with four value resolvers in the HttpKernel component:
2225

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

33+
:class:`Symfony\\Component\\HttpKernel\\Controller\\ArgumentResolver\\SessionValueResolver`
34+
Injects the configured session class extending ``SessionInterface`` if
35+
type-hinted with ``SessionInterface`` or a class extending
36+
``SessionInterface``.
37+
3038
:class:`Symfony\\Component\\HttpKernel\\Controller\\ArgumentResolver\\DefaultValueResolver`
3139
Will set the default value of the argument if present and the argument
3240
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)