通过树枝路径传递多个参数

时间:2014-12-28 01:15:34

标签: symfony twig

我有这个树枝代码:

<div style="padding-left: 5em" class="comment">
    <p>{{ comment.author.name }} - {{ comment.created|date('j. n. Y H:i') }}</p>
    <p>{{ comment.text }}</p>
    <p><a href="{{ path('comment_response_new', {'id': post.id, 'idc': comment.id}) }}">Odpovědět na komentář</a></p>

    {% for child in comment.children %}
        {% include 'BlogApplicationBundle:Post:_comment.html.twig' with {'comment' : child}%}
    {% endfor %}

</div>

这是在twig代码中处理链接输出的函数:

/**
     * @Route("/post/{id}/newcommentresponse", name="comment_response_new")
     * @Template("BlogApplicationBundle:Post:form.html.twig")
     */
    public function commentResponceAction($id,$idc)
    {
        $comment = new Comment();
        $form = $this->createForm(new CommentType(), $comment);

        return array(
            'form' => $form->createView()
        );
}

当我尝试运行代码时出现此错误:

  

Controller&#34; Cvut \ Fit \ BiWt1 \ Blog \ ApplicationBundle \ Controller \ CommentController :: commentResponceAction()&#34;   要求您为&#34; $ idc&#34;提供价值。争论(因为   没有默认值,或者因为存在非可选参数   在此之后)。

所以似乎忽略了通过链接传递的第二个参数,我不知道我做错了什么。

1 个答案:

答案 0 :(得分:2)

您在@Route注释中缺少$idc定义。看起来应该是这样的:

@Route("/post/{id}/newcommentresponse/{idc}", name="comment_response_new")

或者这个:

@Route("/post/{id}/{idc}/newcommentresponse", name="comment_response_new")

您也可以将其从路线和功能声明中删除,并直接从Controller获取:

/**
 * @Route("/post/{id}/newcommentresponse", name="comment_response_new")
 * @Template("BlogApplicationBundle:Post:form.html.twig")
 */
public function commentResponceAction($id)
{
    $idc = $request->query->get('idc');
相关问题