Symfony2.7:Twig渲染controller()忽略我的参数

时间:2016-09-24 01:01:19

标签: symfony templates controller twig twig-extension

我遇到了symfony exeption和我试图得到的一些参数的问题。我完全不知道为什么在经过数千次检查我的代码和在互联网上获得此异常。

例外是:

  

在呈现模板期间抛出异常(“缺少某些必需参数(”slug“)以在@Blog \ Default \ Blog \ singlePost.html中生成路径”blogcomment_create“。”)的URL。 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

1 个答案:

答案 0 :(得分:0)

好的,我最终得到它!

问题根本不在我的树枝模板中,而是在我的控制器中。我有一个功能,在这个控制器中生成我的表单。我在行动中给她打电话:

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

当我遇到那种问题时,我真的需要早点睡觉&gt;。&gt;

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