@@ -391,128 +391,10 @@ Request object.
391
391
single: Controller; The session
392
392
single: Session
393
393
394
- .. _session-intro :
395
-
396
394
Managing the Session
397
395
--------------------
398
396
399
- Symfony provides a session object that you can use to store information
400
- about the user between requests. Session is enabled by default, but will only be
401
- started if you read or write from it.
402
-
403
- Session storage and other configuration can be controlled under the
404
- :ref: `framework.session configuration <config-framework-session >` in
405
- ``config/packages/framework.yaml ``.
406
-
407
- To get the session, add an argument and type-hint it with
408
- :class: `Symfony\\ Component\\ HttpFoundation\\ Session\\ SessionInterface `::
409
-
410
- use Symfony\Component\HttpFoundation\Response;
411
- use Symfony\Component\HttpFoundation\Session\SessionInterface;
412
- // ...
413
-
414
- public function index(SessionInterface $session): Response
415
- {
416
- // stores an attribute for reuse during a later user request
417
- $session->set('foo', 'bar');
418
-
419
- // gets the attribute set by another controller in another request
420
- $foobar = $session->get('foobar');
421
-
422
- // uses a default value if the attribute doesn't exist
423
- $filters = $session->get('filters', []);
424
-
425
- // ...
426
- }
427
-
428
- Stored attributes remain in the session for the remainder of that user's session.
429
-
430
- For more info, see :doc: `/session `.
431
-
432
- .. index ::
433
- single: Session; Flash messages
434
-
435
- .. _flash-messages :
436
-
437
- Flash Messages
438
- ~~~~~~~~~~~~~~
439
-
440
- You can also store special messages, called "flash" messages, on the user's
441
- session. By design, flash messages are meant to be used exactly once: they vanish
442
- from the session automatically as soon as you retrieve them. This feature makes
443
- "flash" messages particularly great for storing user notifications.
444
-
445
- For example, imagine you're processing a :doc: `form </forms >` submission::
446
-
447
- use Symfony\Component\HttpFoundation\Request;
448
- use Symfony\Component\HttpFoundation\Response;
449
- // ...
450
-
451
- public function update(Request $request): Response
452
- {
453
- // ...
454
-
455
- if ($form->isSubmitted() && $form->isValid()) {
456
- // do some sort of processing
457
-
458
- $this->addFlash(
459
- 'notice',
460
- 'Your changes were saved!'
461
- );
462
- // $this->addFlash() is equivalent to $request->getSession()->getFlashBag()->add()
463
-
464
- return $this->redirectToRoute(/* ... */);
465
- }
466
-
467
- return $this->render(/* ... */);
468
- }
469
-
470
- After processing the request, the controller sets a flash message in the session
471
- and then redirects. The message key (``notice `` in this example) can be anything:
472
- you'll use this key to retrieve the message.
473
-
474
- In the template of the next page (or even better, in your base layout template),
475
- read any flash messages from the session using the ``flashes() `` method provided
476
- by the :ref: `Twig global app variable <twig-app-variable >`:
477
-
478
- .. code-block :: html+twig
479
-
480
- {# templates/base.html.twig #}
481
-
482
- {# read and display just one flash message type #}
483
- {% for message in app.flashes('notice') %}
484
- <div class="flash-notice">
485
- {{ message }}
486
- </div>
487
- {% endfor %}
488
-
489
- {# read and display several types of flash messages #}
490
- {% for label, messages in app.flashes(['success', 'warning']) %}
491
- {% for message in messages %}
492
- <div class="flash-{{ label }}">
493
- {{ message }}
494
- </div>
495
- {% endfor %}
496
- {% endfor %}
497
-
498
- {# read and display all flash messages #}
499
- {% for label, messages in app.flashes %}
500
- {% for message in messages %}
501
- <div class="flash-{{ label }}">
502
- {{ message }}
503
- </div>
504
- {% endfor %}
505
- {% endfor %}
506
-
507
- It's common to use ``notice ``, ``warning `` and ``error `` as the keys of the
508
- different types of flash messages, but you can use any key that fits your
509
- needs.
510
-
511
- .. tip ::
512
-
513
- You can use the
514
- :method: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Flash\\ FlashBagInterface::peek `
515
- method instead to retrieve the message while keeping it in the bag.
397
+ :ref: `Reading <session-intro >` for more information about using Sessions.
516
398
517
399
.. index ::
518
400
single: Controller; Response object
0 commit comments