Symfony2 Could not start session: PHP is already running

I have a very strange problem that looks 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)"} [] 

strange thing: I do not start a session or do not use it, here is 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 great

 /** * @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); 

it also checks to see if all my AppKernel files, etc. start with

both actions (working and non-working) are in one controller

+5
source share
2 answers

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

You can disable CSRF protection, but it is enabled by default.

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

+3
source

You should check to see if you have a listener who is starting a new session.

You should not have a listener with new Session() . You must use a query session as in action methods.


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

0
source

Source: https://habr.com/ru/post/1206339/


All Articles