2014-12-28 27 views
1

我有这样的细枝代码:通过树枝路径传递多个参数

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

,这是函数处理来自链接在树枝代码的输出:

/** 
    * @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() 
     ); 
} 

当我尝试运行代码,我得到这个错误:

控制器“Cvut \ Fit \ BiWt1 \ Blog \ ApplicationBundle \ Controller \ CommentController :: commentResponceAction()” ,您所提供的“$ IDC”参数的值(因为 没有默认值,或因为有这一个接一个非可选参数 )。

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

回答

2

你缺少你@Route注释的$idc定义。它应该是这个样子:

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

或本:

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

你也可以离开它的路线和函数声明,并直接从控制器抓住它:

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