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