Symfony 表单无法启动会话:PHP 已启动
Symfony form Failed to start the session: already started by PHP
我收到错误 无法启动会话:在向 Twig 发送表单视图时已由 PHP. 启动。我不知道为什么,我想知道。
动作
public function indexAction(Request $request) {
$em = $this->getDoctrine()->getManager();
$uploadFormType = new \phpUploadFileTest\phpUploadFileTestBundle\Form\UploadType();
$uploadEntity= new \phpUploadFileTest\phpUploadFileTestBundle\Entity\Upload();
$uploadForm = $this->createForm($uploadFormType, $uploadEntity );
$uploadForm->handleRequest($request);
if($uploadForm->isSubmitted() && $uploadForm->isValid()){
$em->persist($uploadEntity);
$em->flush();
}
return $this->render("phpUploadFileTestphpUploadFileTestBundle::index.html.twig", array("uploadForm" => $uploadForm->createView()));
}
错误发生在代码$uploadForm->createView()
。当我删除它时,我没有收到该错误。
\phpUploadFileTest\phpUploadFileTestBundle\Form\UploadType->buildForm代码:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name', "text",[])->add('file', "text",[]);
}
当我禁用表单上的 CSRF 并设置 "csrf_protection"=>false 时,该错误已修复。但我不想禁用 csrf 保护。
我不知道会话管理。但是 vendor/symfony/symfony/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php at line 132
:
抛出的错误
if (PHP_VERSION_ID >= 50400 && \PHP_SESSION_ACTIVE === session_status()) {
throw new \RuntimeException('Failed to start the session: already started by PHP.');
}
来自 /home/guervyl/NetBeansProjects/gvwb/website-creator-symfony/vendor/symfony/symfony/src/Symfony/Component/Security/Csrf/TokenStorage/SessionTokenStorage.php 第 90 行:
public function hasToken($tokenId)
{
if (!$this->session->isStarted()) {
**$this->session->start();**
}
return $this->session->has($this->namespace.'/'.$tokenId);
我创建了一个自定义 app_dev.php。在其中我开始了一个会话。我使用了 legacy session and session,但仍然出现该错误。
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\PhpBridgeSessionStorage;
// legacy application configures session
ini_set('session.save_handler', 'files');
ini_set('session.save_path', '/tmp');
session_start();
// Get Symfony to interface with this existing session
$session = new Session(new PhpBridgeSessionStorage());
$session->start();
...
$session->set(...);
...
$session->has(...);
...
$session->get(...);
我的 Symfony 版本是 2.8.20(我知道它很旧)
我通过将 storage_id: session.storage.php_bridge
添加到我的 config.yml 下修复了它:
framework:
session: { handler_id: null, storage_id: session.storage.php_bridge}
然后在我的 php 文件中使用 Symfony Session class:
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\PhpBridgeSessionStorage;
// legacy application configures session
ini_set('session.save_handler', 'files');
ini_set('session.save_path', '/tmp');
session_start();
// Get Symfony to interface with this existing session
$session = new Session(new PhpBridgeSessionStorage());
$session->start();
...
$session->set(...);
...
$session->has(...);
...
$session->get(...);
我收到错误 无法启动会话:在向 Twig 发送表单视图时已由 PHP. 启动。我不知道为什么,我想知道。
动作
public function indexAction(Request $request) {
$em = $this->getDoctrine()->getManager();
$uploadFormType = new \phpUploadFileTest\phpUploadFileTestBundle\Form\UploadType();
$uploadEntity= new \phpUploadFileTest\phpUploadFileTestBundle\Entity\Upload();
$uploadForm = $this->createForm($uploadFormType, $uploadEntity );
$uploadForm->handleRequest($request);
if($uploadForm->isSubmitted() && $uploadForm->isValid()){
$em->persist($uploadEntity);
$em->flush();
}
return $this->render("phpUploadFileTestphpUploadFileTestBundle::index.html.twig", array("uploadForm" => $uploadForm->createView()));
}
错误发生在代码$uploadForm->createView()
。当我删除它时,我没有收到该错误。
\phpUploadFileTest\phpUploadFileTestBundle\Form\UploadType->buildForm代码:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name', "text",[])->add('file', "text",[]);
}
当我禁用表单上的 CSRF 并设置 "csrf_protection"=>false 时,该错误已修复。但我不想禁用 csrf 保护。
我不知道会话管理。但是 vendor/symfony/symfony/src/Symfony/Component/HttpFoundation/Session/Storage/NativeSessionStorage.php at line 132
:
if (PHP_VERSION_ID >= 50400 && \PHP_SESSION_ACTIVE === session_status()) {
throw new \RuntimeException('Failed to start the session: already started by PHP.');
}
来自 /home/guervyl/NetBeansProjects/gvwb/website-creator-symfony/vendor/symfony/symfony/src/Symfony/Component/Security/Csrf/TokenStorage/SessionTokenStorage.php 第 90 行:
public function hasToken($tokenId)
{
if (!$this->session->isStarted()) {
**$this->session->start();**
}
return $this->session->has($this->namespace.'/'.$tokenId);
我创建了一个自定义 app_dev.php。在其中我开始了一个会话。我使用了 legacy session and session,但仍然出现该错误。
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\PhpBridgeSessionStorage;
// legacy application configures session
ini_set('session.save_handler', 'files');
ini_set('session.save_path', '/tmp');
session_start();
// Get Symfony to interface with this existing session
$session = new Session(new PhpBridgeSessionStorage());
$session->start();
...
$session->set(...);
...
$session->has(...);
...
$session->get(...);
我的 Symfony 版本是 2.8.20(我知道它很旧)
我通过将 storage_id: session.storage.php_bridge
添加到我的 config.yml 下修复了它:
framework:
session: { handler_id: null, storage_id: session.storage.php_bridge}
然后在我的 php 文件中使用 Symfony Session class:
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\PhpBridgeSessionStorage;
// legacy application configures session
ini_set('session.save_handler', 'files');
ini_set('session.save_path', '/tmp');
session_start();
// Get Symfony to interface with this existing session
$session = new Session(new PhpBridgeSessionStorage());
$session->start();
...
$session->set(...);
...
$session->has(...);
...
$session->get(...);