How can I access session data from one package to another

I have two bunches

  • the main
  • To come in

I create a session in one set and must use session data in another set.

How can we access the same session data in different related packages in one package.

Bundle Input Controller - Session Created:

$session = new Session(); $session->set('name', $user->getFname()." ".$user->getLname()); $session->set('uname', $user->getUsername()); $session->set('pwd', $user->getPassword()); 

Login - Username and Password Verification

  if ($request->getMethod() == 'POST') { $uname = $request->request->get('uname'); $pwd = $request->request->get('pwd'); $em = $this->getDoctrine()->getEntityManager(); $repository = $em->getRepository('SimranMainBundle:Users'); $user = $repository->findOneBy(array('username'=>$uname, 'password'=>$pwd)); if($user){ $session = new Session(); $session->set('name', $user->getFname()." ".$user->getLname()); $session->set('uname', $user->getUsername()); $session->set('pwd', $user->getPassword()); return $this->render('SimranLoginBundle:Default:index.html.twig', array('name' => $user->getFname()." ".$user->getLname(),'uname'=>$uname, 'pwd'=>$pwd)); } else{ return $this->render('SimranLoginBundle:Default:index.html.twig', array('name' => "LOGIN")); } } 

Twig input - index.html.twig

 {% extends 'SimranMainBundle::layout.html.twig' %} 

Main Twig - layout.html.twig

 {% set sessionName = session.name %} {{ sessionName }} 

User entity

+4
source share
1 answer

You should first notice that creating an instance of Session means not creating a real php session. In the process of receiving the request, a php session has already been created, and you should use this object.

So, in your controller:

 $session = $request->getSession(); $session->set('name', $user->getFname()." ".$user->getLname()); $session->set('uname', $user->getUsername()); $session->set('pwd', $user->getPassword()); 

And in twig, the session is accessible through app.session .

 {% set sessionName = app.session.get('name') %} 
+3
source

Source: https://habr.com/ru/post/1501890/


All Articles