【问题标题】:Pass several parameters correctly to your controller将几个参数正确传递给您的控制器
【发布时间】:2020-03-27 17:11:19
【问题描述】:

我已经询问过here和there,但没有什么或多或少与我的问题相符。

我有一个包含电影信息的页面,我使用 id 参数访问该页面:

<a href="{{path('film', {'id': film.id{){{" class="btn btn-primary"> See comments </a>

电影表与表有关系,我通过 ArrayCollection 显示电影特定的所有 cmets:

 $filmRepo = $repo->find($id);

 $comments = $filmRepo->getComments();

我创建了一个 CommentController,我在其中编写了这个方法,其目标是恢复电影的 id和评论的 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()
        ]);
    }

但是无论我选择哪条评论,它都采用默认评论,即id为1的评论。所以我的请求有问题。但是我在树枝模板中传递了两个参数:

<a href="{{path('modifComment', {'id' : film.id }, {'comment' : comment.id})}}">Modif</a>

【问题讨论】:

  • 这里有一个错字:{{path('film', {'id': film.id{
  • 这段代码在浏览器中的实际 html 是什么? &lt;a href="{{path('modifComment', {'id' : film.id }, {'comment' : comment.id})}}"&gt;Modif&lt;/a&gt; 我期待类似 &lt;a href="/modifyComment/id/123/comment/456&gt;Modif&lt;/a&gt;
  • @FelippeDuarte 修饰符我的浏览器里有这个
  • 你有 @Route("{id}/{comment}/modif" 所以电影 id = 1 和评论 id = 1。这是你所期望的吗?如果没有,请使用您安装链接的代码更新您的问题,并且comment.id 应该与 1 不同
  • @FelippeDuarte 这是对电影 1 的评论 1 的期望。但是当我选择电影 1 的评论 4 时,例如,这让我回到评论 1(这是合乎逻辑的,因为我把它默认放在我的修改方法中

标签: php symfony annotations doctrine twig


【解决方案1】:

问题来自树枝模板中的语法错误。而不是:

<a href="{{path('modifComment', {'id' : film.id }, {'comment' : comment.id})}}">Modif</a>

宁可这样做:

<a href="{{path('modifComment', {'id' : film.id, 'comment' : comment.id})}}">Modif</a>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-10
    • 1970-01-01
    • 2018-07-14
    • 1970-01-01
    • 2020-11-30
    • 2015-09-13
    相关资源
    最近更新 更多