Symfony 2.1 app.session.flashbag.get ("something") returns an empty value

I have a problem using app.session.flashbag.get('notice').

In the controller, I do

public function updateAction(Request $request, $id)
{
    $em = $this->getDoctrine()->getManager();

    $entity = $em->getRepository('SomeBundle:SomeEntity')->find($id);

    $editForm = $this->createForm(new SomeEntityType(), $entity);
    $editForm->bind($request);

    if ($editForm->isValid()) {
        $em->persist($entity);
        $em->flush();

        $flash = $this->get('translator')->trans('Some Entity was successfully updated');
        $this->get('session')->getFlashBag()->add('notice', $flash);

        return $this->redirect($this->generateUrl('some_entity_edit', array('id' => $id)));

    }

In editAction, I get information from the session:

public function editAction($id)
{
    $em = $this->getDoctrine()->getManager();

    $flashes = $this->get('session')->getFlashBag()->get('notice', array());

    //...
    //...

    return array(
        'entity'      => $entity,
        'edit_form'   => $editForm->createView(),
        'flashes' => $flashes
    );
}

And I'm trying in TWIG to get information from the session:

TWIG: {% for flashMessage in app.session.flashbag.get('notice') %}{{ flashMessage }}{% endfor %}

PHP: {% for flashMessage2 in flashes %}{{ flashMessage2 }}{% endfor %}

The app.session.flashbag.get ('notice') application is empty, flashes matter.

Do you have any ideas why I cannot get data from app.session.flashbag.get ('notice')?

+1
source share
2 answers

His normal behavior. First you get access to the flash controller, so it returns and then is canceled. When you access it again, then the key is missing in the flashbag, so it's empty.

FlashBag:: get github

+5
0

All Articles