【问题标题】:Symfony2.7 : Twig render controller() ignore my parametersSymfony2.7:Twig 渲染控制器()忽略我的参数
【发布时间】:2016-09-24 01:01:19
【问题描述】:

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

例外是:

在@Blog\Default\Blog\singlePost.html 中呈现模板期间引发了异常(“缺少一些强制参数(“slug”)以生成路由“blogcomment_create”的 URL。”)。第 29 行的树枝。

路线配置:

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

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

<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

【问题讨论】:

    标签: symfony templates controller twig twig-extension


    【解决方案1】:

    好的,我终于明白了!

    问题根本不在我的树枝模板中,而是在我的控制器中。我有一个函数可以在这个控制器中生成我的表单。我在行动中叫她:

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

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

    希望我的问题以后能帮助到其他人。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-30
      • 1970-01-01
      • 2018-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多