Home > Software engineering >  Symfony 6 RequestStack Session not getting value
Symfony 6 RequestStack Session not getting value

Time:12-21

Until now I worked with Symfony 5.2. Now I installed new environment with Symfony 6.

Now the Session engine is changed, RequestStack should use now. I setup as https://symfony.com/doc/current/session.html.

It looks like, value is saving but not loading again (session folder contains corresponding files).

Initial route:

/**
 * @Route(
 *     "/",
 *     name="home",
 *     methods={"GET","POST"}
 * )
 *
 * @param RequestStack $requestStack
 * @return Response
 */
public function index(RequestStack $requestStack): Response
{
    $session = $requestStack->getSession();
    $session->set('test','test');

    return $this->render('main/index.html.twig', []);
}

Test route:

    /**
 * @Route(
 *     "/test",
 *     name="test",
 *     methods={"GET","POST"}
 * )
 * @param RequestStack $requestStack
 * @return Response
 * @throws Exception
 */
public function test(RequestStack $requestStack): Response
{
    $session = $requestStack->getSession();
    echo "Test: ".$session->get('test','err');  // <== I get 'err'

    return $this->render('main/index.html.twig', []);
}
  • Every main page refresh, new session file will created.
  • In browser i don't see corresponding session hash
  • In JavaScript document.cookie = "MyCookie"; works

CodePudding user response:

I found the problem: Since update Synfony and PHP, access to resources by domain name needs to have certificate. After installing lets encrypt it works (also for in-house-acces-only).

  • Related