I am trying to implement a simple login page for my usermanagementbundle and I'm new in creating a form thru formbuilder. There is only 3 function inside my bundle where there is a new session and it is called in different routes, even if I remove the other 2 I still receive the same error in it. Here is my code:

    <?php

    namespace Acme\UserManagementBundle\Controller;

    use Symfony\Bundle\FrameworkBundle\Controller\Controller;
    use Symfony\Component\HttpFoundation\Request;
    use Symfony\Component\HttpFoundation\Session\Session;
    use Acme\UserManagementBundle\Entity\Users;
    use Acme\UserManagementBundle\Form\SigninType;

    class DefaultController extends Controller
    {
        public function indexAction()
        {
            $session = new Session();
            $users = new Users();
            $form = $this->createForm(new SigninType(), $users);

            if ($session->has('token'))
            {
                 return $this->render('AcmeUserManagementBundle:Default:home.html.twig', array('token' => $session->get('token')));
            }
            if (!($session->has('alert')))
            {
                $session->set('alert', 'Welcome');
                return $this->render('AcmeUserManagementBundle:Default:index.html.twig', array('form' => $form->createView(), 'alert' => 'Welcome!'));   
            }
            else
            {
                $alert = $session->get('alert');
                $session->clear();
                return $this->render('AcmeUserManagementBundle:Default:index.html.twig', array('form' => $form->createView() ,'alert' => $alert));
            }
        }

        public function logoutAction()
        {
            $session = new Session();
            $session->invalidate();
            return $this->render('AcmeUserManagementBundle:Default:index.html.twig');
        }

         public function signupAction()
        {
            return $this->render('AcmeUserManagementBundle:Default:signup.html.twig');
        }
        public function LoginAction(Request $request)
        {
            $session = new Session();
            if ($request->getmethod()=='POST' || $session->get('token') != ""){
                $user = $request->get('user');
                $password = $request->get('password');
                $em = $this->getDoctrine()->getManager();
                $repository = $em->getRepository('AcmeUserManagementBundle:Users');
                $username = $repository->findOneBy(array('username'=>$user,'password'=>$password));
                    if (!$session->get('token') && $username)
                    {
                       $token = $this->get('token_generator')->getToken();
                       $session->set('token', $token, 'user', $username);
                    } else {
                        $session->set('alert', 'Invalid Username and/or Password!');
                        return $this->redirect($this->generateUrl('homepage'));
                    }
                    return $this->redirect($this->generateUrl('homepage'));
            } else {
                return $this->redirect($this->generateUrl('homepage'));
            }   
        }
    }

When I tried to implement a formbuilderinterface inside my function I suddenly encountered this one vs. the normal creating of from thru html which I've get no problems at all. Is there a problem with my code? because I tried this login code thru HTML and convert it to formbuilderinterface component of Symfony.

2

There are 2 answers

3
Krish R On BEST ANSWER

Symfony2, by default, starts sessions for you automatically.

There is no need of $session = new Session();

Try this,

  public function indexAction(Request $request)
 {
    $session = $request->getSession();

Ref: http://symfony.com/doc/current/book/controller.html#managing-the-session

0
tenac On

You're doing it wrong !

I've been wasting some time looking for a solution for that problem, and end up doing the same thing ( using Request $request ) in the functionAction() to send a session variable to a template.

However all of this is wrong. TWIG is able to access to all session variables directly, you don't need to pass any variable in the TWIG render().

To answer the original problem, you should keep the default behavior of indexAction() with no parameter, and remove the " array('token' => $session->get('token')) " from your TWIG render call.

The only thing to know is that you can call the session variable "token" directly in your template, using : {{ app.session.get('token') }}

And let the magic be ! That simple !