Symfony 2.1 app.session.flashbag.get('something') 返回空值 [英] Symfony 2.1 app.session.flashbag.get('something') returns empty value

查看:50
本文介绍了Symfony 2.1 app.session.flashbag.get('something') 返回空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 app.session.flashbag.get('notice') 时遇到问题.

I have a trouble with using 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)));

    }

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

In editAction I'm getting 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
    );
}

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

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

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

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

app.session.flashbag.get('notice') 为空,flash 有值.

The app.session.flashbag.get('notice') is empty, the flashes has a value.

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

Do you have any ideas, why I can't get data from the app.session.flashbag.get('notice')?

推荐答案

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

Its normal behavior. You access flash in controller first, so it is returned and unset then. When you access it again then key does not exists in flashbag that way is empty.

参见 FlashBag::在 github 上获取

这篇关于Symfony 2.1 app.session.flashbag.get('something') 返回空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆