如何将URL参数传递给呈现的symfony3表单?

如何将URL参数传递给呈现的symfony3表单?,symfony,twig,symfony-forms,Symfony,Twig,Symfony Forms,如果在细枝模板中嵌入控制器,如何向呈现的嵌入路由传递所需的URL参数 在以下示例中,图像属于相册,并且需要相册Id(它是复合键的一部分) 方法如下 public function newAction(Request $request, $album) { $image = new Image(); $form = $this->createForm( 'AppBundle\Form\ImageUploadType', $ima

如果在细枝模板中嵌入控制器,如何向呈现的嵌入路由传递所需的URL参数

在以下示例中,图像属于相册,并且需要相册Id(它是复合键的一部分)

方法如下

    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(),
    ));
}
呈现嵌入表单的模板会导致以下错误

在呈现模板期间引发了异常(“缺少某些必需参数(“相册”)以生成路由“image_new”的URL)


使用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
方法。

您确定错误来自控制器的嵌入吗<代码>'action'=>$this->generateUrl('image_new'),,此处缺少必需参数。由于是嵌入,渲染会导致问题,但不需要直接嵌入cal。
$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',
        ]
);