请求对象为空-Symfony 3.4

时间:2018-07-20 12:19:19

标签: php symfony

我对提交的表单有疑问。

我在控制器方法中创建一个表单对象,并指定在提交时应在另一个操作方法中对其进行处理。

    $invoiceForm = $oldInvoiceForm ?? $this->createForm(InvoiceProjectInvoiceType::class, $invoiceProject, [
        'action' => $this->generateUrl('invoice_edit', ['id' => $invoiceProject->getId()]),
    ]);

这是发票_编辑路线

/**
 * @param Request $request
 * @param InvoiceProject $invoiceProject
 * @return Response
 * @Route("/{id}/edit/invoice", name="invoice_edit", methods="POST")
 */
public function editInvoice(Request $request, InvoiceProject $invoiceProject){

    $form = $this->createForm(InvoiceProjectInvoiceType::class, $invoiceProject);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        $this->getDoctrine()->getManager()->flush();
        $this->addFlash("notice", "La config a bien été créée");
        return $this->redirectToRoute('invoice_show', ['id' => $invoiceProject->getId()]);
    }
    else{
        return $this->forward("ClientBundle:InvoiceProject:show", [
            'id' => $invoiceProject->getId(),
            'oldInvoiceForm' => $form
        ]);
    }
}

我确实使用了该方法,但收到了以下错误消息:

控制器“ ClientBundle \ Controller \ InvoiceProjectController :: editInvoice()”要求您为“ $ request”参数提供一个值。该参数可以为空,并且没有提供空值,没有提供默认值,或者因为此参数之后有一个非可选参数。

因此,我进行了一些测试,我将“ Request $ request = null”设置为可选,并进行了测试。显然,它稍后会在handleRequest部分中崩溃,但是我想知道为什么请求对象没有注入到动作中... 这是呈现形式的html(没有输入,因为有很多输入)

    <form name="clientbundle_invoiceproject_invoice" method="post" action="/client/131/edit/invoice">                
    <button type="submit" class="btn btn-secondary">Modifier les informations</button>
     </form>

有人在代码中看到异常之处吗?

1 个答案:

答案 0 :(得分:2)

似乎您的文件头中没有包含Request

<?php

namespace App\AppBundle\Controller;


use Symfony\Component\HttpFoundation\Request;