-->

如何从实体获得web目录路径?(How to get web directory path from

2019-09-24 03:11发布

我研究了如何处理与学说文件上传 ,我不想硬编码__DIR__.'/../../../../web/'.$this->getUploadDir(); 路径,因为也许在将来,我会改变web/目录。 怎么做更加灵活? 我发现这个 ,但它不回答这个问题如何做到这一点从实体内更灵活

Answer 1:

你不应该使用实体类的表单模型在这里。 它根本不适合这项工作。 如果实体具有path属性,唯一有效的值,它可以商店: null (如果缺少文件)和字符串代表文件的路径。

  1. 创建一个单独的类,这会是你的表单模型:

     class MyFormModel { /** @Assert\File */ private $file; /** @Assert\Valid */ private $entity; // constructor, getters, setters, other methods } 
  2. 在您的表单处理程序(通过DIC配置单独的对象;推荐)或控制器:

     ... if ($form->isValid()) { /** @var \Symfony\Component\HttpFoundation\File\UploadedFile */ $file = $form->getData()->getFile(); /** @var \Your\Entity\Class */ $entity = $form->getData()->getEntity(); // move the file // $path = '/path/to/the/moved/file'; $entity->setPath($path); $someEntityManager->persist($entity); return ...; } ... 

在内部形成处理器/控制器,你可以访问任何依赖性/自DIC特性(包括路径上传目录)。


本教程中,您链接的作品,但它是不好的设计的一个例子。 该机构不应该知道文件上传。



Answer 2:

要访问根目录控制器外,你可以简单地注入“%kernel.root_dir%”在你的服务的配置参数。

service_name:
    class: Namespace\Bundle\etc
    arguments: ['%kernel.root_dir%']

然后,你可以在类的构造Web根目录:

public function __construct($rootDir)
{
    $this->webRoot = realpath($rootDir . '/../web');
}


Answer 3:

您可以在parameters.yml使用的变量。 像这样,当你想,你就可以改变路径。

例如 :

# app/config/parameters.yml
# Upload directories
upload_avatar_dir:        /uploads/avatars
upload_content_dir:       /uploads/content
upload_product_offer_dir: /uploads/product-offer
...


Answer 4:

我通过创建一个抽象类,如果他们正在处理的文件上传在Symfony的文档中描述的实体可以扩展处理这一点。 我创建的文件阵列,所以我可以在设定的方法创建的现有文件路径的副本,以便它可以关闭文件系统上的更新成功被删除或不正确的定义在实体的任何附加属性删除。

use Symfony\Component\HttpFoundation\File\File;

abstract class FileUploadEntity
{
    private $files;

    public function __set($name, File $value)
    {
        $this->files[$name] = $value;
    }

    public function __get($name)
    {
        if (!is_array($this->files)) $this->files = array();

        if (!array_key_exists($name, $this->files)) {
            return null;
        }
        return $this->files[$name];
    }

    public function getUploadRootDirectory()
    {
        return $this->getWebDirectory() . $this->getUploadDirectory();
    }

    public function getWebDirectory()
    {
        return __DIR__ . "/../../../../web/";
    }

    public function getUploadDirectory()
    {
        $year = date("Y");
        $month= date("m");

        return "images/uploads/$year/$month/";
    }

    public function getEncodedFilename($name)
    {
        return sha1($name . uniqid(mt_rand(), true));
    }

    // this should be a PrePersist method
    abstract public function processImages();

    // This should be defined as a Doctrine PreUpdate Method
    abstract public function checkImages();

    // this should be a PostPersist method
    abstract public function upload();

    // this should be a PostUpdate method and delete old files
    abstract public function checkUpload();

    // This should be a PostRemove method and delete files
    abstract public function deleteFile();
}


文章来源: How to get web directory path from inside Entity?