2017-08-03 81 views
0

我正在構建一個symfony 3項目,我試圖在提交表單以通知用戶表單已成功提交但它不適用於我的情況下在樹枝模板中顯示一個flashbag消息! 這裏是控制器symfony 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() 
     )); 


    } 
} 

這裏是樹枝模板:

{% 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 %} 

我已經試過了,但DOC我不知道它不工作在我的身邊。

請幫忙!感謝

回答

1

嘗試改變這一行:

$this->get('session')->getFlashBag()->add('notice', 'la session est lancée avec succes'); 

TO:

$request->getSession()->getFlashBag()->add('notice', 'la session est lancée avec succes'); 

或者你可以這樣做:

$this->addFlash(
    'notice', 
    'la session est lancée avec succes' 
); 

而且在嫩枝得到像這樣:

{% 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