无法为命名路由 "participants" 生成 URL,因为这样的路由不存在

Unable to generate a URL for the named route "participants" as such route does not exist

我想在我的代码中添加这个 link :

<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,
    ]);
}

但是没用。我需要做什么?

在你的例子中一切都是正确的。可能您忘记配置 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

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

您错过了从请求中获取 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,
    ]);
}

dump $id 你可以得到id值,然后像你一样调用repo。

变化,

public function participants(Formations $formation,$id)

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

你可以开始了。