无法启动会话:已由 PHP 启动(设置了 _SESSION)。500 内部服务器错误 - 运行时异常
2022-08-30 21:39:18
我正在尝试为我的用户管理捆绑实现一个简单的登录页面,我是通过表单构建器创建表单的新手。我的捆绑包中只有3个函数,其中有一个新会话,它在不同的路由中被调用,即使我删除了其他2个,我仍然收到相同的错误。这是我的代码:
<?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'));
}
}
}
当我试图在我的函数中实现一个表单构建器接口时,我突然遇到了这个与从html到html的正常创建,我根本没有遇到任何问题。我的代码有问题吗?因为我通过HTML尝试了这个登录代码,并将其转换为Symfony的formbuilderinterface组件。