Symfony使用表单上传文件

时间:2018-08-29 22:23:29

标签: php symfony

我正在使用Symfony 3.4.8,并尝试创建一个用于上传文件的表单。我严格按照Symfony document的步骤进行操作,但收到错误消息:

Controller "AppBundle\Report::uploadReport()" requires that you provide a value for the "$fileUploader" argument. Either the argument is nullable and no null value has been provided, no default value has been provided or because there is a non optional argument after this one.

这是我的代码的一部分,其余部分与文档相同,只是我更改了类名。显然,调用该函数时,没有FileUploader参数传递给该函数。如果删除参数FileUploader $fileUploader,则页面可以加载而不会引发异常,但不会获取文件。我是Symfony的新手,如何解决此问题?

/**
 * @Route("/report/create-report/upload/", name="report_create")
 */
public function uploadReport(Request $request, FileUploader $fileUploader)
{
    $report = new Report();
    $form = $this->createForm(ReportType::class, $report);
    $form->add('submit', SubmitType::class);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        // $file stores the uploaded PDF file
        /** @var Symfony\Component\HttpFoundation\File\UploadedFile $file */
        $file = $report->getReport();

        $fileName = $fileUploader->upload($file);
        $report->setBrochure($fileName);
        //$fileName = $this->generateUniqueFileName().'.'.$file->guessExtension();

        // moves the file to the directory where brochures are stored
        //$file->move(
        //    $this->getParameter('reports_directory'),
        //    $fileName
        //);

        // updates the 'brochure' property to store the PDF file name
        // instead of its contents
        //$report->setReport($fileName);

        // ... persist the $product variable or any other work
    }
    return $this->render('report/createReport.html.twig', array(
        'form' => $form->createView(),
    ));
}

我看过post,但由于没有这样的变量$container,我无法得到答案,因此无法继续工作。

最新更新:我放弃了尝试从头开始执行工具的过程。我使用了推荐的bundle,使其以最少的编码量工作。

1 个答案:

答案 0 :(得分:0)

FileUploader.php服务的参数 brochures_directory 似乎是空的。 您是否在service.yml中指定了它? 您是否也将其添加到config.yml中?

然后您是否在更改后清除了symfony缓存?