Failed to start the session: already started by PHP ($_SESSION is set). 500 Internal Server Error - RuntimeException

Cedric picture Cedric · Jan 22, 2014 · Viewed 21.6k times · Source

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.

Answer

Krish R picture Krish R · Jan 22, 2014

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