SessionAwareController.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace App\Controller;
  3. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  7. /**
  8. * Class SessionAwareController demonstrates session usage.
  9. *
  10. * @package App\Controller
  11. */
  12. class SessionAwareController {
  13. /**
  14. * @param \Symfony\Component\HttpFoundation\Session\SessionInterface $session
  15. *
  16. * @return \Symfony\Component\HttpFoundation\Response
  17. *
  18. * @Route(
  19. * name = "session-get",
  20. * path = "session/get"
  21. * )
  22. */
  23. public function get(SessionInterface $session) {
  24. $q = $session->has('q') ? $session->get('q') : NULL;
  25. return new Response($q ? "Q is $q" : "Q is not set");
  26. }
  27. /**
  28. * @param \Symfony\Component\HttpFoundation\Session\SessionInterface $session
  29. * @param \Symfony\Component\HttpFoundation\Request $request
  30. *
  31. * @return \Symfony\Component\HttpFoundation\Response
  32. *
  33. * @Route(
  34. * name = "session-set",
  35. * path = "session/set"
  36. * )
  37. */
  38. public function set(SessionInterface $session, Request $request) {
  39. $q = $request->query->has('q') ? (int) $request->query->get('q') : 0;
  40. $session->set('q', $q);
  41. return new Response("Set q to $q");
  42. }
  43. }