john Smith
john Smith

Reputation: 17936

Symfony2 Failed to start the session: already started by PHP

i got a very strange issue looking like this:

[2014-11-06 11:21:13] request.INFO: Matched route "core_timetracking_new_user" (parameters: "_controller": "Bricks\Custom\CoreBundle\Controller\TimeTrackingController::newuserAction", "_route": "core_timetracking_new_user") [] []
[2014-11-06 11:21:13] request.CRITICAL: Uncaught PHP Exception RuntimeException: "Failed to start the session: already started by PHP." at /var/cache/app/prod/classes.php line 113 {"exception":"[object] (RuntimeException: Failed to start the session: already started by PHP. at /var/cache/app/prod/classes.php:113)"} []

the strange thing is i do not start a session or use it, heres the Controller code:

/**
 * @View
 */
public function newuserAction()
{
    $trackingService=$this->get('core.timetracking_service');
    $user= new TimeTrackingUser();
    $request=$this->getRequest();

    $form = $this->createFormBuilder($user)
        ->add('name','text')
        ->add('email','text')
        ->add('pass','password')
        ->add('save', 'submit', array('label' => 'Erstellen'))
        ->getForm();

    $form->handleRequest($request);

    if ($form->isValid()) {
        $trackingService->persistUser($form->getData());
        return $this->redirect($this->generateUrl('core_timetracking_user_list'));
    }else {
        return array(
            'form' => $form->createView()
        );
    }
}

while this action works wonderfull

/**
 * @View
 */
public function listuserAction()
{

    $request=$this->getRequest();
    $trackingService=$this->get('core.timetracking_service');    
    $users=$trackingService->getAllUsers();

    return array(
        'users' => $users
    );
}

so the only difference is that i use

$form->handleRequest($request); 

also checked if all my files AppKernel etc do start with

both actions ( the working one and the not working one ) are in the same controller

Upvotes: 5

Views: 4558

Answers (2)

PhoneixS
PhoneixS

Reputation: 11036

You should check if you have any listener who start a new session.

You mustn't have a listener with a new Session(). You should use the request session as in the action methods.


I have a onKernelController listener who started a new session with new Session() and then when the form try to make the csrf token it checks if a session exists and throw the exception.

Upvotes: 0

Thomas Landauer
Thomas Landauer

Reputation: 8355

As soon as you render a form, Symfony automatically starts a session to store the token for CSRF Protection: http://symfony.com/doc/current/book/forms.html#csrf-protection

You can disable CSRF Protection, but it's on by default.

@rakete:
The only additional idea I have is to change the way in which session files are stored (e.g. file system, database, memory, etc.). See here: http://symfony.com/doc/current/components/http_foundation/session_configuration.html

Upvotes: 4

Related Questions