【问题标题】:Passing multiple arguments through twig path通过树枝路径传递多个参数
【发布时间】:2014-12-28 01:15:34
【问题描述】:

我有这个树枝代码:

<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”参数提供一个值(因为 没有默认值或因为有非可选参数 在这个之后)。

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

【问题讨论】:

    标签: symfony twig


    【解决方案1】:

    您的@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');
    

    【讨论】:

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