如何将 URL 参数传递给呈现的 symfony3 表单?
How to pass a URL parameter to a rendered symfony3 form?
如果我在 twig 模板中嵌入控制器,如何将必需的 URL 参数传递给呈现的嵌入式路由?
在下面的例子中,图像属于一个相册,需要相册 ID(它是复合键的一部分)
<tbody>
<tr>
<th>Id</th>
<td>{{ album.id }}</td>
</tr>
<tr>
<th>Name</th>
<td>{{ album.name }}</td>
</tr>
<tr>
{{ render(controller(
'AppBundle:Image:new',
{'album': album}
)) }}
</tr>
</tbody>
Image:new 路由以以下注释为前缀
* @Route("{album}/image")
方法如下
public function newAction(Request $request, $album)
{
$image = new Image();
$form = $this->createForm(
'AppBundle\Form\ImageUploadType',
$image,
[
'action' => $this->generateUrl('image_new'),
'method' => 'POST',
]
);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($image);
$em->flush();
return $this->redirectToRoute('image_show', array('id' => $image->getId()));
}
return $this->render('image/new.html.twig', array(
'image' => $image,
'form' => $form->createView(),
));
}
呈现我们嵌入该表单的模板会导致以下错误;
An exception has been thrown during the rendering of a template ("Some mandatory parameters are missing ("album") to generate a URL for route "image_new".").
使用 symfony3.2 和 twig 向嵌入式控制器传递 URL 参数的正确方法是什么?
Symfony 抛出的错误不是关于传递 ID,而是关于 URL 构建。
在你的代码中,你有这个片段:
$form = $this->createForm(
'AppBundle\Form\ImageUploadType',
$image,
[
'action' => $this->generateUrl('image_new'),
'method' => 'POST',
]
);
虽然应该是:
$form = $this->createForm(
'AppBundle\Form\ImageUploadType',
$image,
[
'action' => $this->generateUrl('image_new',['album'=>$album,]),
'method' => 'POST',
]
);
只需将相册传递给 generateUrl
方法即可。
如果我在 twig 模板中嵌入控制器,如何将必需的 URL 参数传递给呈现的嵌入式路由?
在下面的例子中,图像属于一个相册,需要相册 ID(它是复合键的一部分)
<tbody>
<tr>
<th>Id</th>
<td>{{ album.id }}</td>
</tr>
<tr>
<th>Name</th>
<td>{{ album.name }}</td>
</tr>
<tr>
{{ render(controller(
'AppBundle:Image:new',
{'album': album}
)) }}
</tr>
</tbody>
Image:new 路由以以下注释为前缀
* @Route("{album}/image")
方法如下
public function newAction(Request $request, $album)
{
$image = new Image();
$form = $this->createForm(
'AppBundle\Form\ImageUploadType',
$image,
[
'action' => $this->generateUrl('image_new'),
'method' => 'POST',
]
);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($image);
$em->flush();
return $this->redirectToRoute('image_show', array('id' => $image->getId()));
}
return $this->render('image/new.html.twig', array(
'image' => $image,
'form' => $form->createView(),
));
}
呈现我们嵌入该表单的模板会导致以下错误;
An exception has been thrown during the rendering of a template ("Some mandatory parameters are missing ("album") to generate a URL for route "image_new".").
使用 symfony3.2 和 twig 向嵌入式控制器传递 URL 参数的正确方法是什么?
Symfony 抛出的错误不是关于传递 ID,而是关于 URL 构建。
在你的代码中,你有这个片段:
$form = $this->createForm(
'AppBundle\Form\ImageUploadType',
$image,
[
'action' => $this->generateUrl('image_new'),
'method' => 'POST',
]
);
虽然应该是:
$form = $this->createForm(
'AppBundle\Form\ImageUploadType',
$image,
[
'action' => $this->generateUrl('image_new',['album'=>$album,]),
'method' => 'POST',
]
);
只需将相册传递给 generateUrl
方法即可。