문제

I have two bundles

  1. Main
  2. Login

I am creating the session in one bundle and need to use the session data in another bundle.

How can we access the same session data in different bundles created in one single bundle.

Login Bundle 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 Check

     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"));
            }
        }

Login Twig - index.html.twig

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

Main Twig - layout.html.twig

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

Users Entity

도움이 되었습니까?

해결책

First you should note, creating a Session instance means not creating a real php session. A php session is already created during fetching the request and you have to 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 via app.session.

{% set sessionName = app.session.get('name') %}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top