异常:合并过滤器仅适用于“”中的数组或散列;HerzultForumBundle:主题:new.html.trick&q


Exception: The merge filter only works with arrays or hashes in "HerzultForumBundle:Topic:new.html.twig"

我正在通过添加更多语言来改进我的web应用程序。我已经按照本教程完成了:https://coderwall.com/p/eiqd_g

一切似乎都很好,但现在我发现,当我试图在论坛(HerzultForumBundle)上创建一个新帖子时,我会遇到上述异常。

我可以在调试器中看到:

在kernel.root_dir/cache/dev/classes.php中8422

function twig_array_merge($arr1, $arr2)
{
    if (!is_array($arr1) || !is_array($arr2)) {
        throw new Twig_Error_Runtime('The merge filter only works with arrays or hashes.');
    }
    return array_merge($arr1, $arr2);
}

twig_array_merge(null,array('_locale'=>'en')) 在kernel.root_dir/cache/dev/twig/c7/1/91808022d83474dae4ab5fad0cda278f4614c031cf7a5531428c5812bd57.php中,第200行

            echo ">
            't        <a href='"";
            // line 81
            echo twig_escape_filter($this->env, $this->env->getExtension('routing')->getPath($this->getAttribute($this->getAttribute((isset($context["app"]) ? $context["app"] : $this->getContext($context, "app")), "request"), "get", array(0 => "_route"), "method"), twig_array_merge($this->getAttribute($this->getAttribute((isset($context["app"]) ? $context["app"] : $this->getContext($context, "app")), "request"), "get", array(0 => "_route_params"), "method"), array("_locale" => (isset($context["locale"]) ? $context["locale"] : $this->getContext($context, "locale"))))), "html", null, true);
            echo "'">";
            echo twig_escape_filter($this->env, (isset($context["locale"]) ? $context["locale"] : $this->getContext($context, "locale")), "html", null, true);
            echo "</a>

所以twig_array_merge()函数似乎得到了一个null值作为第一个参数。HerzultForumBundle:Topic:new.html.twig模板似乎是原因。HerzultForumBundle:Topic:new.html.twig模板包含以下代码:

{% extends 'HerzultForumBundle::layout.html.twig' %}
{% block title %}New Reply{% endblock %}
{% block content %}
<div class="forum  post_new">
<ul class="crumbs">
    <li><a href="{{ path('herzult_forum_index') }} ">Forum</a></li>
    <li><a href="{{ forum_urlForCategory(topic.category) }}">{{ topic.category.name }}</a></li>
    <li><a href="{{ forum_urlForTopic(topic) }}">{{ topic.subject }}</a></li>
    <li>New Reply</li>
</ul>
<div class="main">
    <h2>New Reply</h2>
    <form action="{{ url('herzult_forum_topic_post_create', { 'slug': topic.slug, 'categorySlug' : topic.category.slug }) }}" method="post">
        {{ form_widget(form) }}
        <div>
            <button type="submit" name="reply">Add post</button>
        </div>
    </form>
</div>
<div class="side">
    <p><a href="{{ forum_urlForTopic(topic) }}">Back to the topic</a></p>
</div>
</div>
{% endblock %}

我的猜测是,异常来自这一行:

<form action="{{ url('herzult_forum_topic_post_create', { 'slug': topic.slug, 'categorySlug' : topic.category.slug }) }}" method="post">

但不知道怎么解决,知道吗?论坛以前运作得很好。

好吧,这是我的问题,在基本模板中:

<ul id="languages">{% for locale in ['en', 'fr', 'es', 'de'] %}
    <li {% if locale == app.request.locale %}class="active"{% endif %}>
        <a href="{{ path(app.request.get('_route'), app.request.get('_route_params')|merge({'_locale' : locale})) }}">{{ locale }}</a>
    </li>
        {% endfor %}
</ul>

所以认为app.request.get('_route_params')为空。我试过了:

<ul id="languages">{% for locale in ['en', 'fr', 'es', 'de'] %}
    <li {% if locale == app.request.locale %}class="active"{% endif %}>
        {% if app.request.get('_route_params') %} <a href="{{ path(app.request.get('_route'), app.request.get('_route_params')|merge({'_locale' : locale})) }}">{{ locale }}</a>{% endif %}
    </li>
        {% endfor %}
</ul>

是的!它有效。我不会在论坛上有翻译,但没关系,我认为这在英语中是可以理解的。

我在模板中遇到了类似的错误"合并过滤器只适用于…中的数组或哈希"。不管是哪一行,不管我做了什么改变,错误的那一行移动了,但信息保持不变。。在我将i18n兼容性添加到我的网站后,它只出现在有表单的页面上。

原因是我使用了一个->forward()到另一个模板,这取决于是否存在错误。

所以这个错误确实和路由问题有关。

我最终呈现了模板,而不是转发到另一个页面。