Symfony 2.1 app.session.flashbag.get('something')返回空值

Nic*_*rev 1 php session symfony

我使用时遇到了麻烦app.session.flashbag.get('notice').

在我做的控制器中

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)));

    }
Run Code Online (Sandbox Code Playgroud)

在editAction中,我从会话中获取信息:

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
    );
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试TWIG从会话中获取信息:

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

PHP: {% for flashMessage2 in flashes %}{{ flashMessage2 }}{% endfor %}
Run Code Online (Sandbox Code Playgroud)

app.session.flashbag.get('notice')为空,闪烁有一个值.

你有任何想法,为什么我无法从app.session.flashbag.get('notice')获取数据?

l3l*_*3l0 5

它的正常行为.您首先访问控制器中的闪存,然后返回并取消设置.当你再次访问它时,flashbag中的密钥不存在.

请参阅FlashBag :: get at github