简体   繁体   English

Symfony2 - 带有flash消息的数组到字符串转换异常

[英]Symfony2 - Array to string conversion exception with a flash message

I set a flash message in a controller with the following code: 我在控制器中设置了一条flash消息,代码如下:

$this->get('session')->getFlashBag()->add('success', 'Message sent successfully');

And in my template, I use the following to (attempt to) display it: 在我的模板中,我使用以下内容(尝试)显示它:

{% if app.session.flashbag.has('success') %}
    <div id="flash">
        {{ app.session.flashbag.get('success') }}
    </div>
{% endif %}

The problem is that, despite the API documentation stating that get returns a string, I'm getting an array to string conversion exception. 问题是,尽管API文档声明get返回一个字符串,但我得到一个数组到字符串转换异常。 If I change the code in the template to: 如果我将模板中的代码更改为:

{% for flashMessage in app.session.flashbag.get('success') %}
    <div id="flash">
        {{ flashMessage }}
    </div>
{% endfor %}

It works perfectly. 它完美地运作。 I'd rather not use a loop here since I'm only ever either going to have the single message or not. 我宁愿不在这里使用循环,因为我只是要么有或没有。

Is there a solution where I can just check for the existence of a single flash message and display it if it's there? 有没有一个解决方案,我可以检查是否存在单个Flash消息并显示它是否存在? Or am I stuck with a useless loop? 还是我坚持使用无用的循环?

Solved it by indexing at 0: 通过索引为0来解决它:

{{ app.session.flashbag.get('success')[0] }}

My suspicions were correct - get returns an array rather than a string. 我的怀疑是正确的 - get返回一个数组而不是一个字符串。 Here's the flashbag's add method: 这是flashbag的add方法:

public function add($type, $message)
{
    $this->flashes[$type][] = $message;
}

And get : get

public function get($type, array $default = array())
{
    if (!$this->has($type)) {
        return $default;
    }

    $return = $this->flashes[$type];

    unset($this->flashes[$type]);

    return $return;
}

They need to fix the API documentation so it reflects reality. 他们需要修复API文档,以便反映现实。 They should also provide an elegant way to handle a single flash message. 它们还应该提供一种处理单个Flash消息的优雅方式。

EDIT: A backwards compatible (PHP 5.3 and below) version - 编辑:向后兼容(PHP 5.3及以下)版本 -

{% if app.session.flashbag.has('success') %}
    {% set flashbag = app.session.flashbag.get('success') %}
    {% set message = flashbag[0] %}
    <div id="flash">
        {{ message }}
    </div>
{% endif %}

For one flash message: 对于一个flash消息:

{{ app.session.flashbag.get('success')[0] }}

For all: 对全部:

{% for type, messages in app.session.flashbag.all() %}
    {% for message in messages %}
        <div class="alert alert-{{ type }}">
            {{ message }}
        </div>
    {% endfor %}
{% endfor %}

I've just hit this myself. 我自己就是这样打的。 It was because I was using the add() method instead of set() . 这是因为我使用的是add()方法而不是set()

The differences between Add and Set: 添加和设置之间的差异:

public function add($type, $message)
{
    $this->flashes[$type][] = $message;
}

The above would add an extra array which isn't required in this case. 以上将添加一个额外的数组,在这种情况下不需要。

Whereas: 鉴于:

public function set($type, $messages)
{
    $this->flashes[$type] = (array) $messages;
}

So set() results in $array[$key] = $value , rather than what add does, which is $array[$key][] = $value which is what is causing your Array to string conversion because you're passing an array, not a string. 所以set()导致$array[$key] = $value ,而不是add的作用,这是$array[$key][] = $value这是导致你的Array转换为字符串的原因,因为你正在传递一个数组,而不是一个字符串。

OK, I see that you have resolved this issue by yourself but this might be an easier way: 好的,我发现你已经自己解决了这个问题,但这可能是一种更简单的方法:

{% if app.session.hasFlash('success') %}
    {{ app.session.flash('success') }}
{% endif %}

... since you can't guarantee that there will always be at least flash message ;) ...因为你不能保证总会有至少flash消息;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM