将几个参数正确传递给您的控制器
Pass several parameters correctly to your controller
我已经查询过here and ,但没有或多或少符合我的问题。
我有一个包含电影信息的页面,我使用 id 参数访问该页面:
<a href="{{path('film', {'id': film.id{){{" class="btn btn-primary"> See comments </a>
电影 table 与 table 有关系,我如何显示所有特定于电影的评论感谢 ArrayCollection :
$filmRepo = $repo->find($id);
$comments = $filmRepo->getComments();
我创建了一个 CommentController,我在其中编写了这个方法,其目标是恢复电影的 ID AND 评论的 ID,以便能够 CRUD 操作 就可以了:
/**
* @Route("{id}/{comment}/create", name="createComment")
* @Route("{id}/{comment}/modif", name="modifComment", defaults={"comment"=1}, methods="GET|POST")
*/
public function modification(Comment $comment = null, Film $film, Request $req, EntityManagerInterface $em)
{
if(!$comment) {
$comment = new Comment();
}
$user = $this->getUser();
$form = $this->createForm(CommentType::class, $comment);
$form->handleRequest($req);
if($form->isSubmitted() && $form->isValid()) {
$comment->setAuthor($user);
$comment->setFilm($film);
$em->persist($comment);
$em->flush();
$this->addFlash('success', 'L\'action a bien été effectuée');
return $this->redirectToRoute('home');
}
return $this->render('comment/modif.html.twig', [
"comment" => $comment,
"form" => $form->createView()
]);
}
但是无论我select是哪条评论,都是默认的评论,也就是id为1的评论。所以我的请求有问题。但是我在树枝模板中传递了两个参数:
<a href="{{path('modifComment', {'id' : film.id }, {'comment' : comment.id})}}">Modif</a>
问题来自于 twig 模板中的语法错误。而不是:
<a href="{{path('modifComment', {'id' : film.id }, {'comment' : comment.id})}}">Modif</a>
宁愿做:
<a href="{{path('modifComment', {'id' : film.id, 'comment' : comment.id})}}">Modif</a>
我已经查询过here and
我有一个包含电影信息的页面,我使用 id 参数访问该页面:
<a href="{{path('film', {'id': film.id{){{" class="btn btn-primary"> See comments </a>
电影 table 与 table 有关系,我如何显示所有特定于电影的评论感谢 ArrayCollection :
$filmRepo = $repo->find($id);
$comments = $filmRepo->getComments();
我创建了一个 CommentController,我在其中编写了这个方法,其目标是恢复电影的 ID AND 评论的 ID,以便能够 CRUD 操作 就可以了:
/**
* @Route("{id}/{comment}/create", name="createComment")
* @Route("{id}/{comment}/modif", name="modifComment", defaults={"comment"=1}, methods="GET|POST")
*/
public function modification(Comment $comment = null, Film $film, Request $req, EntityManagerInterface $em)
{
if(!$comment) {
$comment = new Comment();
}
$user = $this->getUser();
$form = $this->createForm(CommentType::class, $comment);
$form->handleRequest($req);
if($form->isSubmitted() && $form->isValid()) {
$comment->setAuthor($user);
$comment->setFilm($film);
$em->persist($comment);
$em->flush();
$this->addFlash('success', 'L\'action a bien été effectuée');
return $this->redirectToRoute('home');
}
return $this->render('comment/modif.html.twig', [
"comment" => $comment,
"form" => $form->createView()
]);
}
但是无论我select是哪条评论,都是默认的评论,也就是id为1的评论。所以我的请求有问题。但是我在树枝模板中传递了两个参数:
<a href="{{path('modifComment', {'id' : film.id }, {'comment' : comment.id})}}">Modif</a>
问题来自于 twig 模板中的语法错误。而不是:
<a href="{{path('modifComment', {'id' : film.id }, {'comment' : comment.id})}}">Modif</a>
宁愿做:
<a href="{{path('modifComment', {'id' : film.id, 'comment' : comment.id})}}">Modif</a>