VichUploader uri_prefix不考虑环境

时间:2015-10-20 10:23:34

标签: php symfony vichuploaderbundle

我在我的项目中使用VichUploaderBundle 我无法获取下载链接(使用vich_file表单类型时)以考虑环境(特别是 dev )。我想要通过函数路由下载功能。

我的配置就像;

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

无论环境如何,这都会生成http://example.com/foo/image/fubar.jpg的下载uri 在开发环境中,它应该是http://example.com/app_dev.php/foo/image/fubar.jpg,或者在测试环境中 http://example.com/app_test.php/foo/image/fubar.jpg

1 个答案:

答案 0 :(得分:0)

一种可能的解决方案是使用可以访问路由器类的自定义文件名称类。 在配置中将uri_prefix设置为 /

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

创建一个服务名称类,返回包含所需路径的完整URL(带参数);

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

}

将其注册为服务;

services:
    my.namer.foo:
        class: AppBundle\Service\MyNamer
        arguments: ['@router']
        tags:
            - { name: namer }