How to get web directory path from inside Entity?
Solution 1
You shouldn't use entity class as a form model here. It's simply not suitable for that job. If the entity has the path
property, the only valid values it can stores are: null
(in case lack of the file) and string representing the path to the file.
Create a separate class, that's gonna be a model for your form:
class MyFormModel { /** @Assert\File */ private $file; /** @Assert\Valid */ private $entity; // constructor, getters, setters, other methods }
In your form handler (separate object configured through DIC; recommended) or the 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 ...; } ...
Inside form handler/controller you can access any dependencies/properties from DIC (including path to the upload directory).
The tutorial you've linked works, but it's an example of bad design. The entities should not be aware of file upload.
Solution 2
To access the root directory from outside the controller you can simply inject '%kernel.root_dir%' as an argument in your services configuration.
service_name:
class: Namespace\Bundle\etc
arguments: ['%kernel.root_dir%']
Then you can get the web root in the class constructor:
public function __construct($rootDir)
{
$this->webRoot = realpath($rootDir . '/../web');
}
Mikhail
CTO at Umbrella-web company Umbrella Web Studio offers you the creation of sharp, up-to-date Web applications with extensible functionality and scalability for high traffic loads. The company offers a wide range of experience in the creation of successful commercial projects.
Updated on August 22, 2022Comments
-
Mikhail over 1 year
I researched the How to Handle File Uploads with Doctrine and I don't want to hard-code the
__DIR__.'/../../../../web/'.$this->getUploadDir();
path because maybe in future I will change theweb/
directory. How to do it more flexible? I found this but it doesn't answer the question how to do it more flexible from inside the Entity -
Mikhail over 11 yearsI understood. Thanks. But how to use a lifecycle callbacks to manage a deletion of the file with your code?
-
Jovan Perovic over 11 yearsBased on previous experience I suggest you store absolute path (or at least relative to webroot path) as well..
-
Crozin over 11 yearsCreate a listener, that subscribes
preRemove
(orpreDelete
) event (see how to) and unlink the file there. -
Mikhail over 11 yearsHow to obtain it inside an Entity?
-
Benjamin Lazarecki over 11 yearsIt's not the responsibility of your entity to know where the document is store. Yo can move this responsibility to your controller. Or you can add a path attribute to your entity
-
Mikhail over 10 yearsHm, I see that you used the
__DIR__ . "/../../../../web/"
too, but my question is how to avoid it. -
Tom Phillips over 10 yearsI'm not sure there is one. It seemed like the best i could do was define it in one place.
-
Mikhail over 10 yearsgot it. Thanks for your solution Tom