Symfony 4,从数据夹具类获取根路径目录

use*_*454 0 symfony symfony4

在 Symfony 4 项目中,是否有一种正确的/symfony 方法可以从 Fixture 类中获取根路径目录?

最终目标:

当我部署我的网站时,我使用fixtures 类在我的数据库中加载一些“默认”数据。(可能这不是一个好习惯)

在这些默认数据中,我需要存储项目的根路径目录,因为我需要来自项目中自定义 utils 和服务类的这些信息。

我知道有多种方法可以做到这一点。目前,我对绝对路径进行了硬编码(<= 有史以来最丑陋的方式;))

Sud*_*nan 5

Symfony 4.1.7

服务.yml

services:
  _defaults:
    bind:
      $webDirectory:  '%kernel.project_dir%/public_html/'
Run Code Online (Sandbox Code Playgroud)

灯具文件

use Symfony\Component\HttpFoundation\File\UploadedFile;
// --
private $webDirectory, $imageLocation;
public function __construct($webDirectory)
{
    $this->webDirectory = $webDirectory;
    $this->imageLocation = $webDirectory . 'img/config/';
}
public function load(ObjectManager $manager){
    for ($i = 0; $i < 20; $i++) {
        copy($this->imageLocation . 'blog_default_50x50.jpg', $this->imageLocation . 'blog_default_50x50-copy.jpg');
        $file = new UploadedFile($this->imageLocation . 'blog_default_50x50-copy.jpg', 'Image1', null, null, null, true);
        ---
        $blogPost->setFile($file);
        $manager->persist($blogPost);
    }
    $manager->flush();
}
Run Code Online (Sandbox Code Playgroud)

UploadedFile() 信息来自/sf/answers/1321833411/(谢谢!)。

希望这可以帮助某人。