我正在尝试为我的 usermanagementbundle 实现一个简单的登录页面,并且我是通过 formbuilder 创建表单的新手。我的包中只有 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'));
}
}
}
当我尝试在我的函数中实现一个 formbuilderinterface 时,我突然遇到了这个与从 html 正常创建的情况相比,我完全没有遇到任何问题。我的代码有问题吗?因为我通过 HTML 尝试了这个登录代码并将其转换为 Symfony 的 formbuilderinterface 组件。