How do I store session and access it anywhere in my application in symfony2?

Code is given below. I have a loginAction method and I want to access username through the session in my twig file.

public function loginAction(Request $request)
{
    $session= $this->getRequest()->getSession();

    if($request->getMethod()=='POST')
    {

        $username=$request->get('username');
        $password=$request->get('password');

        $em= $this->getDoctrine()->getEntityManager();
        $repository = $em->getRepository('MWANRegisterBundle:User');

        $user= $repository->findOneBy(array('userName'=>$username,    'userPassword'=> $password ));
        if($user)
        {
        $login= new Session(); 

     // Session is in the same class as getter setter functions
     // for username and password

        $login->setUsername($username);
        $session->set('login', $login);
          if($session->has('login'))
            {
            $login = $session->get('login');
            $username = $login->getUsername();
            //  var_dump($username); exit();

       // in show_home I redirect to twig file list.html.twig

            return $this->redirect($this->generateUrl('show_home'));
            }

        }
        else
        {
        echo("wrong username   OR  password");
        exit();
        }
    }

    return $this->render('MWANRegisterBundle:Default:login.html.twig');
 }

--------------------- Here is the logout method --------------------------

public function logoutAction(Request $request)
{
    $session= $this->getRequest()->getSession();
    $session->clear();
    return $this->render('MWANRegisterBundle:Default:login.html.twig');
}

  // listAction is action to render list.html.twig i want username of     session here 

 public function listAction (Request $request)
 {
    $session= $this->getRequest()->getSession();

        return $this->render('MWANRegisterBundle:Default:list.html.twig' );

}

Kindly guide me step by step ......

Answers


You don't need this "hack" to retrieve logged username, even in twig.

Simply use this snippet of code

{{ app.user.username }}

If you want to be sure that user is currently logged in, you could add some explicit control

{% if app.user %}
  {{ app.user.username }}
{% endif %}

For a complete answer, I add also session retrieval from twig

{{ app.session.get('parameterName') }}

you can access your sessions in twig by using {{app.session.get("nameOfSession")}} and setting session {{app.session.set("name","value")}}

for your user managment I suggest you to use FOSUserBundle to have a best and secur application


Need Your Help

call python from nodejs on AWS lambda - permission denied

python linux node.js amazon-web-services aws-lambda

I am able to call my python from nodejs on AWS Lambda using the below function. However, because I need specific python libraries, I created a virutalenv in the env directory. I zipped everything u...

All previous Mobile Views are visible in Worklight Android Application for Samsung devices with OS version higher the 4.1.x

android mobile dojo worklight

I'm using dojo performTransition to switch between views and it is working fine all the devices except Samsung devices with Os version higher than 4.1.x

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.