symfony flashBag 消息不起作用
symfony flashBag message not working
我正在构建一个 symfony 3 项目,我试图在提交表单后在 twig 模板中显示 flashbag 消息,以通知用户表单已成功提交,但它对我不起作用!
这是控制器:
<?php
namespace EvalBundle\Controller;
use EvalBundle\Entity\SessionEvaluation;
use EvalBundle\Form\SessionEvaluationType;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class EvaluationController extends Controller
{
/**
* @Route("/evaluation", name="evaluation_session")
* @Method({"GET", "POST"})
*/
public function indexAction(Request $request)
{
$sessioneval = new SessionEvaluation();
$form = $this->createForm(SessionEvaluationType::class, $sessioneval);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()){
$em = $this->getDoctrine()->getManager();
$em->persist($sessioneval);
$em->flush();
$this->get('session')->getFlashBag()->add('notice', 'la session est lancée avec succes');
unset($entity);
}
return $this->render('@Eval/Default/Evaluation/newEvaluationSession.html.twig',array(
'form' => $form->createView()
));
}
}
这里是 twig 模板:
{% extends ':default:superAdminBase.html.twig' %}
{% block menu %}
{% include(':default:menu.html.twig') %}
{% endblock %}
{% block header %}
{% endblock %}
{% block body %}
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12">
<h1 class="page-header">
Lancer une nouvelle session d'évaluation
</h1>
</div>
</div>
{% set flashbag_notices = app.session.flashBag('notice') %}
{% if flashbag_notices is not empty %}
<div class="row">
<div class="col-lg-6">
<ul>
{% for notice in flashbag_notices %}
{{ dump(flashbag_notices) }}
<li>{{ notice }}</li>
{% endfor %}
</ul>
</div>
</div>
{% endif %}
<div class="row">
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-heading">
Session d'évaluation des Compétences
</div>
<div class="panel-body">
<div class="row">
<div class="container">
<div class="col-lg-12 ">
{{ form_start(form) }}
{{ form_widget(form) }}
{{ form_end(form) }}
</div>
</div>
</div>
</div>
</div>
</div>
</div>
{% endblock %}
{% block javascript %}
{% endblock %}
我已经按照文档进行操作,但我不知道它对我不起作用。
请帮忙!谢谢
尝试更改此行:
$this->get('session')->getFlashBag()->add('notice', 'la session est lancée avec succes');
收件人:
$request->getSession()->getFlashBag()->add('notice', 'la session est lancée avec succes');
或者你可以这样做:
$this->addFlash(
'notice',
'la session est lancée avec succes'
);
在 Twig 中,像这样:
{% set flashbag_notices = app.session.flashBag('notice') %}
如果你碰巧是 运行 Symfony 3.3,你可以改用这个:
{% set flashbag_notices = app.flashes('notice') %}
根据新变化:
https://symfony.com/blog/new-in-symfony-3-3-improved-flash-messages
我正在构建一个 symfony 3 项目,我试图在提交表单后在 twig 模板中显示 flashbag 消息,以通知用户表单已成功提交,但它对我不起作用! 这是控制器:
<?php
namespace EvalBundle\Controller;
use EvalBundle\Entity\SessionEvaluation;
use EvalBundle\Form\SessionEvaluationType;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class EvaluationController extends Controller
{
/**
* @Route("/evaluation", name="evaluation_session")
* @Method({"GET", "POST"})
*/
public function indexAction(Request $request)
{
$sessioneval = new SessionEvaluation();
$form = $this->createForm(SessionEvaluationType::class, $sessioneval);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()){
$em = $this->getDoctrine()->getManager();
$em->persist($sessioneval);
$em->flush();
$this->get('session')->getFlashBag()->add('notice', 'la session est lancée avec succes');
unset($entity);
}
return $this->render('@Eval/Default/Evaluation/newEvaluationSession.html.twig',array(
'form' => $form->createView()
));
}
}
这里是 twig 模板:
{% extends ':default:superAdminBase.html.twig' %}
{% block menu %}
{% include(':default:menu.html.twig') %}
{% endblock %}
{% block header %}
{% endblock %}
{% block body %}
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12">
<h1 class="page-header">
Lancer une nouvelle session d'évaluation
</h1>
</div>
</div>
{% set flashbag_notices = app.session.flashBag('notice') %}
{% if flashbag_notices is not empty %}
<div class="row">
<div class="col-lg-6">
<ul>
{% for notice in flashbag_notices %}
{{ dump(flashbag_notices) }}
<li>{{ notice }}</li>
{% endfor %}
</ul>
</div>
</div>
{% endif %}
<div class="row">
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-heading">
Session d'évaluation des Compétences
</div>
<div class="panel-body">
<div class="row">
<div class="container">
<div class="col-lg-12 ">
{{ form_start(form) }}
{{ form_widget(form) }}
{{ form_end(form) }}
</div>
</div>
</div>
</div>
</div>
</div>
</div>
{% endblock %}
{% block javascript %}
{% endblock %}
我已经按照文档进行操作,但我不知道它对我不起作用。
请帮忙!谢谢
尝试更改此行:
$this->get('session')->getFlashBag()->add('notice', 'la session est lancée avec succes');
收件人:
$request->getSession()->getFlashBag()->add('notice', 'la session est lancée avec succes');
或者你可以这样做:
$this->addFlash(
'notice',
'la session est lancée avec succes'
);
在 Twig 中,像这样:
{% set flashbag_notices = app.session.flashBag('notice') %}
如果你碰巧是 运行 Symfony 3.3,你可以改用这个:
{% set flashbag_notices = app.flashes('notice') %}
根据新变化:
https://symfony.com/blog/new-in-symfony-3-3-improved-flash-messages