php - Symfony fatal error __construct() 必须是 MyBundle\FileUploader 的实例

标签 php symfony file-upload doctrine-orm

我确实按照本教程在 Symfony 中上传文件,但我收到一个错误,我已经坐了大约一个小时。

完整错误:

FatalThrowableError in FileListener.php line 21:
Type error: Argument 1 passed to MyBundle\FileListener::__construct() 
must be an instance of MyBundle\FileUploader, string given, called in 
/dev/shm/appname/cache/dev/appDevDebugProjectContainer.php on line 830

有问题的 2 个类是 fileuploader 类和 doctrine 文件监听器类。错误出现在 fileListener 中,我认为这是因为我没有在任何地方创建 fileuploader 的对象,但教程没有:

http://symfony.com/doc/current/controller/upload_file.html

文件上传代码:

namespace MyBundle;

use Symfony\Component\HttpFoundation\File\UploadedFile;

class FileUploader
{
private $targetDir;

public function __construct($targetDir)
{
    $this->targetDir = $targetDir;
}

public function upload(UploadedFile $file)
{
    $fileName = md5(uniqid()).'.'.$file->guessExtension();

    $file->move($this->targetDir, $fileName);

    return $fileName;
}

public function getTargetDir()
{
    return $this->targetDir;
}

文件监听器代码:

namespace MyBundle;

use Symfony\Component\HttpFoundation\File\UploadedFile;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Event\PreUpdateEventArgs;
use MyBundle\Entity\MainMedia;
use MyBundle\FileUploader;

class FileListener
{
private $uploader;

public function __construct(FileUploader $uploader)
{
    $this->uploader = $uploader;
}

public function prePersist(LifecycleEventArgs $args)
{
    $entity = $args->getEntity();

    $this->uploadFile($entity);
}

public function preUpdate(PreUpdateEventArgs $args)
{
    $entity = $args->getEntity();

    $this->uploadFile($entity);
}

private function uploadFile($entity)
{
    if (!$entity instanceof MainMedia) {
        return;
    }

    $file = $entity->getFile();

    // only upload new files
    if (!$file instanceof UploadedFile) {
        return;
    }

    $fileName = $this->uploader->upload($file);
    $entity->setFile($fileName);
}

我还设置了 YAML 服务:

   file_uploader:
  class: MyBundle\FileUploader
  arguments: ['%file_directory%']


  file_listener:
  class: MyBundle\FileListener
  arguments: ['file_uploader']
  tags:
       - { name: doctrine.event_listener, event: prePersist }
       - { name: doctrine.event_listener, event: preUpdate }

以及目录配置中的参数:

  file_directory: '%kernel.root_dir%/../web/uploads'

无论如何希望你能有所帮助。谢谢。

最佳答案

在您的 yaml 文件中,您编写 arguments: ['file_uploader']。这会将字符串“file_uploader”传递给 FileListener 的构造函数。您真正想要的是传递由名称“file_uploader”引用的服务。您可以通过在前面添加一个 @ 符号来执行此操作,如下所示:

参数:["@file_uploader"]

关于php - Symfony fatal error __construct() 必须是 MyBundle\FileUploader 的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43850184/

相关文章:

PHP循环遍历月份数组

php - 奏鸣曲管理员 : send email after validation

MySQL 加载数据本地 Infile - 路径作为用户变量

php - 我们可以在同一行同时运行 mysql_fetch_assoc 和 mysql_query 吗?

php - MVC 模型中的外部 CSS 和 JS 文件

php - 如何修改laravel中的请求值?

json - 在 Symfony3 中捕获登录失败事件

symfony - Doctrine - 根据字段检查记录是否存在

javascript - 使用 XMLHttpRequest 将多个文件上传到 Express.js 3.5 服务器

angular - Asp.net Core 和 Angular 7 Cors