123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- <?php
- namespace App\Controller;
- use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
- use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
- use Symfony\Component\HttpFoundation\Request;
- use Symfony\Component\HttpFoundation\Response;
- use Symfony\Component\HttpFoundation\Session\SessionInterface;
- /**
- * Class SessionAwareController demonstrates session usage.
- *
- * @package App\Controller
- */
- class SessionAwareController extends AbstractController {
- /**
- * @param \Symfony\Component\HttpFoundation\Session\SessionInterface $session
- *
- * @return \Symfony\Component\HttpFoundation\Response
- *
- * @Route(
- * name = "session-get",
- * path = "session/get"
- * )
- */
- public function getValue(SessionInterface $session) {
- $q = $session->has('q') ? $session->get('q') : NULL;
- if ($q) {
- $this->addFlash('info', "Q was found to be $q");
- }
- return new Response($q ? "Q is $q" : "Q is not set");
- }
- /**
- * @param \Symfony\Component\HttpFoundation\Session\SessionInterface $session
- * @param \Symfony\Component\HttpFoundation\Request $request
- *
- * @return \Symfony\Component\HttpFoundation\Response
- *
- * @Route(
- * name = "session-set",
- * path = "session/set"
- * )
- */
- public function setValue(SessionInterface $session, Request $request) {
- $q = $request->query->has('q') ? (int) $request->query->get('q') : 0;
- $session->set('q', $q);
- $this->addFlash('info', "Q was set to $q");
- return new Response("Set q to $q");
- }
- }
|