【问题标题】:Exception thrown during the rendering of a template("Parameter "id" for route "url" must match "[^/]++" ("" given) to generate a corresponding URL.")渲染模板期间抛出异常(“路由“url”的参数“id”必须匹配“[^/]++”(“”给定)以生成相应的URL。“)
【发布时间】:2021-10-17 21:20:38
【问题描述】:

我有一个在这里创建的删除按钮:

_delete_form.html.twig

<form method="post" action="{{ path('finals_delete', {'id': final.id}) }}" onsubmit="return confirm('Are you sure you want to delete this item?');">
<input type="hidden" name="_token" value="{{ csrf_token('delete' ~ final.id) }}">
<button class="btn btn-success" style="margin-top: 10px">Verwijderen</button>

我将它包含在 编辑 页面中,如下所示:

   {{ include('finals/_form.html.twig', {'button_label': 'Opslaan'}) }}

    <button class="btn btn-success"><a href="{{ path('finals_index') }}">Terug naar lijst</a>  </button>

   {{ include('finals/_delete_form.html.twig') }}

删除的控制器操作:

    /**
 * @Route("/{id}", name="finals_delete", methods={"POST"})
 */
public function delete(Request $request, Finals $final): Response
{
    if ($this->isCsrfTokenValid('delete'.$final->getId(), $request->request->get('_token'))) {
        $entityManager = $this->getDoctrine()->getManager();
        $entityManager->remove($final);
        $entityManager->flush();
    }

    return $this->redirectToRoute('finals_index');
}

edit:

的控制器操作
/**
 * @Route("/{id}/edit", name="finals_edit", methods={"GET","POST"})
 */
public function edit(Request $request, Finals $final): Response
{
    $final = new Finals();
    $form = $this->createForm(FinalsType::class, $final);
    $form->handleRequest($request);

    $imageFile = $form->get('imageTeam1')->getData();

    //If function to only process an imagine if its uploaded
    if ($imageFile) {

        $originalFilename = pathinfo($imageFile->getClientOriginalName(), PATHINFO_FILENAME);

        //Remove unwanted characters from filename
        $safeFilename = transliterator_transliterate('Any-Latin; Latin-ASCII; [^A-Za-z0-9_] remove; Lower()', $originalFilename);
        $newFilename = $safeFilename.'-'.uniqid().'.'.$imageFile->guessExtension();


        //Move file to image dir
        try {
            $imageFile->move($this->getParameter('images_directory'),$newFilename);
        }   catch (FileException $e) {
            $this->addFlash('danger', 'Er is iets fout gegaan probeer het opnieuw');
        }

        $final->setImageTeam1($newFilename);


        $this->getDoctrine()->getManager()->flush();
        return $this->redirectToRoute('finals_index');
    }

    return $this->render('finals/edit.html.twig', [
        'final' => $final,
        'form' => $form->createView(),
    ]);
}

所有这些都是使用 bin/console make:crud 命令创建的,我用完全相同的按钮和代码制作了多个这样的页面,但由于某种原因只有这个页面会出现此错误:

An exception has been thrown during the rendering of a template ("Parameter "id" for route "finals_delete" must match "[^/]++" ("" given) to generate a corresponding URL.").

在 C:\xampp\htdocs\Freulepartij\templates\finals_delete_form.html.twig(第 1 行)中

当我点击我的编辑页面时它会抛出错误(当删除按钮刚刚被呈现并且它背后的功能没有执行时?)我觉得很奇怪。当我删除包含时,我可以进入我的编辑页面,但是 我的编辑表单也不会更新记录,所以它只是不知道如何处理数据? 我真的很困惑,因为当我单击没有删除按钮的编辑页面时,id 就在 url 中

更新 1

正如 Gary Houbre 建议的那样,我替换了

 {{ include('finals/_delete_form.html.twig' }}

{{ include('finals/_delete_form.html.twig' , {'final': final.id}) }}

现在得到这个错误:

无法访问空变量的属性(“id”)。

但我正在查看我的数据库,并且该表中只有 2 条记录都有 id,所以我不知道它是如何获得空值的?

更新 2

我在 symfony 日志中发现了这个,它确认了正确的 id "7" 所以这让我更加困惑..

选择 t0.id AS id_1, t0.team1 AS team1_2, t0.team2 AS team2_3, t0.image_team1 AS image_team1_4, t0.image_team2 AS image_team2_5, t0.paragraph AS 段落_6, t0.eersten_p1 AS eersten_p1_7, t0.eersten_p2 AS eersten_p2_8, t0.punten_p1 AS punten_p1_9, t0.punten_p2 AS punten_p2_10 FROM finals t0 WHERE t0.id = ?

[ “7”]

【问题讨论】:

    标签: php symfony twig symfony4


    【解决方案1】:

    进入你的edit.html.twig,你的包含文件_delete.html.twig所在的行

    您需要添加参数“id”。 例如:

    {% include 'template.html' with {'foo': 'bar'} %}
    

    我在 link 中关注这个文档 Twig

    【讨论】:

    • 我试过这个并没有改变任何东西,仍然是同样的错误
    • 你包含 {'final': 'final'} 对吗?
    • 当我用 {'final': final.id} 替换它时,我得到:无法访问空变量上的属性(“id”)。
    • 试试这个:{{ include('finals/_delete_form.html.twig' , {'final': final}) }}
    • 我试过了,但这给了我原来的错误
    【解决方案2】:

    正如 gary 的 cmets 中所指出的,我在编辑函数中有一个新调用,该函数显然不属于那里,所以这就是我的问题所在

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-29
      • 2014-01-13
      • 2016-04-05
      • 2013-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多