symfony 3会话flashdata不工作
如果注册失败,我希望在输入字段附近显示错误消息。正在尝试使用会话flashdata 当用户转到postAction方法时,他被重定向到indexAction。但是symfony 3会话flashdata不工作,symfony,Symfony,如果注册失败,我希望在输入字段附近显示错误消息。正在尝试使用会话flashdata 当用户转到postAction方法时,他被重定向到indexAction。但是 public function __construct() { //echo ini_get('session.auto_start'); die; $this->session = new Session(); /** * @Route("/", name="registration_index")
public function __construct()
{
//echo ini_get('session.auto_start'); die;
$this->session = new Session();
/**
* @Route("/", name="registration_index")
* @Route("/user/registration", name="registration")
* @Method("GET")
*/
public function indexAction()
{
//$this->session->getFlashBag()->add('notice', 'Profile updated');
$errors = $this->session->getFlashBag()->get('notice', array());
print_r($errors);
return $this->render('registration.html.twig', ['errors' => $errors]);
}
/**
* @Route("/user/registration", name="post_registration")
* @Method("POST")
* @return mixed
*/
public function postAction()
{
//$this->session->getFlashBag()->add('errors', 'hahaha');
$this->session->getFlashBag()->add('notice', 'Profile updated');
return $this->redirectToRoute('registration');
}
打印空数组。为什么呢?没有重定向-它工作正常 这不是symfony的问题,而是php服务器的问题。即使没有symfony的php手册中的代码示例也不起作用 我是这样运行服务器的: php-s0.0.0.0:8000 由于某些原因,会议没有就此展开
当我开始使用nginx服务器时,会话开始工作。您为什么不使用symfony表单来获取此功能?Symfony表格非常适合注册页面。
顺便说一下,如果在控制器中执行此操作,则不需要构造函数方法。取而代之的是从Request->getSession()获取会话,我确认WebCookie在他的评论中说:如果您需要在控制器中添加flash消息,请在控制器中使用
$this->addFlash()
注意,您也可以像这样使用会话服务:$this->get('session')->getFlashBag()->add()
了解更多信息。因为我从一个与symfony 2.x合作的家伙那里听说synfony表单不好。他有没有给出原因?这是控制器吗?如果它扩展了symfony控制器,您可以执行$this->addFlash();我甚至没有问原因,因为要理解,我想我需要先看看代码示例。我相信他,因为他是个优秀的程序员。这是控制器扩展控制器类。Request->getSession()-没有getFlashBag()方法。如果它在控制器中,您只需执行$This->addFlash(),请参见此处:
public function __construct()
{
//echo ini_get('session.auto_start'); die;
$this->session = new Session();
/**
* @Route("/", name="registration_index")
* @Route("/user/registration", name="registration")
* @Method("GET")
*/
public function indexAction()
{
//$this->session->getFlashBag()->add('notice', 'Profile updated');
$errors = $this->session->getFlashBag()->get('notice', array());
print_r($errors);
return $this->render('registration.html.twig', ['errors' => $errors]);
}
/**
* @Route("/user/registration", name="post_registration")
* @Method("POST")
* @return mixed
*/
public function postAction()
{
//$this->session->getFlashBag()->add('errors', 'hahaha');
$this->session->getFlashBag()->add('notice', 'Profile updated');
return $this->redirectToRoute('registration');
}
print_r($errors);