Search code examples
symfonysymfony4

Symfony use a dynamic variable in config file path


For every Program can have one or more Documents, that are files uploaded with the help of the VichUploaderBundle.

I have a config file for vich_uploader like this:

vich_uploader:
    db_driver: orm
    mappings:
        program_document:
            upload_destination: '%kernel.project_dir%/public_html/uploads/program-documents'
            namer: Vich\UploaderBundle\Naming\UniqidNamer

I would like to store documents for one Program in one folder and not altogether. Is it possible to add a dynamic variable in upload_destination? Something like %program_id%, so that files for one program are stored in a folder with the ID of that program?


Solution

  • In addition to a file namer (which you already have in your config), you can also specify a directory namer (https://github.com/dustin10/VichUploaderBundle/blob/master/Resources/doc/namers.md#directory-namer). There are already 3 available, one of which (the PropertyDirectoryNamer), should solve your problem. If not, you can still write a custom directory namer.