1

I am using the VichUploaderBundle in my project.
I am unable to get the download link (when using the vich_file form type) to take into account of the environment (notably dev). Which I want to do as I want to route the download function through a function.

My config is like;

vich_uploader:
    db_driver: orm
    twig:      true
    mappings:
        logo_image:
            uri_prefix:         /foo/image
            upload_destination: %kernel.root_dir%/../app/private/images
            inject_on_load:     false
            delete_on_update:   true
            delete_on_remove:   true

And this generates the download uri of http://example.com/foo/image/fubar.jpg, regardless of the environment.
When in dev environment it should be http://example.com/app_dev.php/foo/image/fubar.jpg, or in test environment http://example.com/app_test.php/foo/image/fubar.jpg

Rooneyl
  • 7,802
  • 6
  • 52
  • 81
  • Why do you need this? These are static files, so they should not need to be handled by the frontend controller – Carlos Granados Oct 20 '15 at 10:35
  • @CarlosGranados because I want the images held above the web root so can only be accessed via a function where I can check credentials prior to serving the image. – Rooneyl Oct 20 '15 at 10:38

1 Answers1

0

A possible solution is to use a custom file namer class that has access to the router class. In the config set the uri_prefix to /

vich_uploader:
    db_driver: orm
    twig:      true
    mappings:
        logo_image:
            uri_prefix:         /
            upload_destination: %kernel.root_dir%/../app/private/images
            inject_on_load:     false
            delete_on_update:   true
            delete_on_remove:   true
            namer: my.namer.foo

Create a service namer class that return the full url with the route required (with parameter);

<?php
namespace AppBundle\Service;

use Vich\UploaderBundle\Naming\NamerInterface,
    Vich\UploaderBundle\Mapping\PropertyMapping;
use Symfony\Bundle\FrameworkBundle\Routing\Router;
class MyNamer implements NamerInterface 
{

    private $router;

    public function __construct(Router $router) 
    {
        $this->router = $router;
    }

    public function name($foo, PropertyMapping $mapping) 
    {
        $router = $this->router->getContext();
        $full_url = $router->getHost();
        $full_url .= $router->getBaseUrl().'/';
        $full_url .= 'foo/logo/'.strtolower($foo->getName());
        return $full_url;
    }

}

Register it as a service;

services:
    my.namer.foo:
        class: AppBundle\Service\MyNamer
        arguments: ['@router']
        tags:
            - { name: namer }
Rooneyl
  • 7,802
  • 6
  • 52
  • 81