How to get web directory path from inside Entity?

I researched How to handle file downloads using Doctrine , and I donโ€™t want to hardcode the __DIR__.'/../../../../web/'.$this->getUploadDir(); because maybe in the future I will change the web/ directory. How to make it more flexible? I found this one , but it does not answer the question of how to make it more flexible from within Entity

+4
source share
4 answers

You should not use an entity class as a model for the form here. It just is not suitable for this job. If the object has a path property, the only valid values โ€‹โ€‹that it can store are: null (if there is no file) and a string representing the path to the file.

  • Create a separate class that will become a model for your form:

     class MyFormModel { /** @Assert\File */ private $file; /** @Assert\Valid */ private $entity; // constructor, getters, setters, other methods } 
  • In your form handler (a separate object configured through DIC is recommended) or a controller:

     ... 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 ...; } ... 

An internal form handler / controller you can access any DIC dependencies / properties (including the path to the download directory).


The tutorial you are connected with works, but this is an example of poor design. Entities should not know about file loading.

+6
source

To access the root directory outside the controller, you can simply enter "% kernel.root_dir%" as an argument in configuring your services.

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

Then you can get the root website in the class constructor:

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

I handled this by creating an abstract class that Entities can extend if they handle file loading, as described in the Symfony documentation. I created an array of files in order to create a copy of the existing file path in the set methods so that it can be deleted from the file system upon successful update or deletion without defining any additional properties in the Entity itself.

 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(); } 
0
source

You can use the variable in your .yml options. How can you change the way whenever you want.

eg:

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

Source: https://habr.com/ru/post/1439784/


All Articles