模板渲染期间抛出异常("Parameter "id" for route "url" must match "[^/]++" ("" given) to generate a corresponding URL."
Exception thrown during the rendering of a template("Parameter "id" for route "url" must match "[^/]++" ("" given) to generate a corresponding URL.")
我在这里创建了一个删除按钮:
_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');
}
编辑的控制器操作:
/**
* @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}) }}
现在出现此错误:
Impossible to access an attribute ("id") on a null variable.
但是我正在查看我的数据库,并且在这个 table 中只有 2 条记录都有 id,所以我不知道它是如何得到空值的?
更新 2
我在 symfony 日志中找到了这个,它确认了正确的 ID“7”
所以这让我更加困惑..
SELECT 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 paragraph_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" ]
进入你的edit.html.twig,你的包含文件所在的行_delete.html.twig
您需要添加参数'id'。
例如:
{% include 'template.html' with {'foo': 'bar'} %}
我在这个 link
中遵循此文档 Twig
正如加里评论中指出的那样,我在编辑功能中有一个新调用,显然不属于那里,所以这就是我的问题所在
我在这里创建了一个删除按钮:
_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');
}
编辑的控制器操作:
/**
* @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}) }}
现在出现此错误:
Impossible to access an attribute ("id") on a null variable.
但是我正在查看我的数据库,并且在这个 table 中只有 2 条记录都有 id,所以我不知道它是如何得到空值的?
更新 2
我在 symfony 日志中找到了这个,它确认了正确的 ID“7” 所以这让我更加困惑..
SELECT 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 paragraph_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" ]
进入你的edit.html.twig,你的包含文件所在的行_delete.html.twig
您需要添加参数'id'。 例如:
{% include 'template.html' with {'foo': 'bar'} %}
我在这个 link
中遵循此文档 Twig正如加里评论中指出的那样,我在编辑功能中有一个新调用,显然不属于那里,所以这就是我的问题所在