无法为命名路线“参与者”生成URL,因为该路线不存在

时间:2019-10-06 22:16:00

标签: php twig symfony4

我想在我的代码中添加此链接:

<a href="{{ path('participants', {id: formation.id})  }}" class="btn btn-secondary">La liste des participants</a>

然后我在控制器中编写此函数:

 /**
 * @Route("/admin/formation/{id}/participants", name="participants")
 */
public function participants(Formations $formation)
{
    $participants = $this->repositoryp->findBy(array('id_f_id' => $formation->getId() ));

    return $this->render('Formations/Participants.html.twig', [
        'participants' => $participants,
    ]);
}

但是它不起作用。我该怎么办?

2 个答案:

答案 0 :(得分:2)

在您的示例中,一切都是正确的。可能您忘记配置routing via annotations

选中config/routes.yaml。应该有这样的配置:

# config/routes.yaml
controllers:
    resource: '../src/Controller/'
    type: annotation

也许此配置位于config/routes/annotations.yaml中。

# config/routes/annotations.yaml
controllers:
    resource: '../../src/Controller/'
    type: annotation

如果这些文件中没有该设置,请添加它。

答案 1 :(得分:0)

您错过了从请求中获取ID:

 /**
 * @Route("/admin/formation/{id}/participants", name="participants")
 */
public function participants(Formations $formation,$id)
{
    $participants = $this->repositoryp->findBy(array('id_f_id' => $id ));

    return $this->render('Formations/Participants.html.twig', [
        'participants' => $participants,
    ]);
}

转储$ id,您可以获得id值,然后像您一样进行回购调用。

更改

public function participants(Formations $formation,$id)

$participants = $this->repositoryp->findBy(array('id_f_id' => $id ));

你很好。