Symfony2.7:Twig 渲染控制器()忽略我的参数

Symfony2.7 : Twig render controller() ignore my parameters

我遇到了 symfony 异常和我试图获取的一些参数的问题。我完全不知道为什么在对我的代码和互联网进行了数千次检查后我会得到这个异常。

例外情况是:

An exception has been thrown during the rendering of a template ("Some mandatory parameters are missing ("slug") to generate a URL for route "blogcomment_create".") in @Blog\Default\Blog\singlePost.html.twig at line 29.

路由配置:

blogcomment_create:
    path:   /{slug}/comment/create/
    defaults: { _controller: "BlogBundle:Comment:create" }
    requirements: { methods: post }

我的 twig 模板中用于导入我的控制器的代码:

<footer>
    {{ render(controller('BlogBundle:Comment:create', {
        'slug': Post.slugname
    })) }}
</footer>

我的行动宣言:

 public function createAction(Request $request, $slug = null)
{
    //...
}

我开始对这个例外感到生气,我不知道为什么我得到她,我真的需要一双好眼睛。

我尝试了很多例子:

How to insert a Controller in Twig with "render" in Symfony 2.2?

symfony2 - twig - how to render a twig template from inside a twig template

Change template via parameter when render a controller action?

...

这里是函数的转储:

object(Symfony\Component\HttpKernel\Controller\ControllerReference)[992]
    public 'controller' => 
        string 'BlogBundle:Comment:create' (length=31)
    public 'attributes' => 
        array (size=1)
            'slug' => string 'article-de-test-yolowowo' (length=24)
    public 'query' => 
        array (size=0)
            empty

好的,我终于明白了!

问题根本不在我的树枝模板中,而是在我的控制器中。我有一个功能可以在此控制器中生成我的表单。我在行动中称呼她:

private function createCreateForm(Comment $entity, $slug)
{
    $form = $this->createForm(new CommentType(), $entity, array(
        'action' => $this->generateUrl('blogcomment_create', array('slug' => $slug)),
        'method' => 'POST',
    ));

    $form->add('submit', 'submit', array('label' => 'Create'));

    return $form;
}

在这个函数中我忘记传递参数了:

$this->generateUrl()

当我遇到这种问题时,我真的需要早点睡觉>.>

我希望我的问题将来能帮助到其他人。