簡體   English   中英

如何在 Twig 中顯示指向使用 gaufrette 上傳的文件的鏈接?

[英]How to show a link in twig to a file uploaded with gaufrette?

我想展示一個用 gaufrette 上傳的 PDF 文件。 我從 Doctrine ("wine_information/1-wine_information.pdf") 獲得 URI。

但是,如果根目錄位於本地或遠程 (Amazon s3) 上,則根目錄可能會有所不同。

我嘗試使用 liip_imagine 的過濾器樹枝來使用 CacheManager.php 和方法 getBrowserPath 但我無法創建一個,因為我沒有過濾器可應用於我的 PDF。 它僅適用於圖像。

現在我找到了這個解決方案: https : //github.com/KnpLabs/Gaufrette/issues/332#issuecomment-159849533

上傳服務.php

public function getPath($filePath)
{
    $fileSystem = $this->container->get('gaufrette.' . $this->env . self::MEDIA_FILESYSTEM);
    $adapter = $fileSystem->getAdapter();
    $reflection = new \ReflectionClass($adapter);
    $directory = $reflection->getProperty('directory');
    $directory->setAccessible(true);

    return $directory->getValue($adapter) . $filePath;
} 

當它在本地時,它返回以下路徑: /home/company/projectName/web/media/wine_information/1-wine_information.pdf

但我想在我的<a href="" target="_blanck"></a>設置一個真正的鏈接

我找到了解決方案。 我做了一個樹枝過濾器來獲取用 gaufrette 上傳的文件的路徑。 用於Amazon S3本地.

應用\\配置\\服務.yml

app.aws_s3.client:
    class: Aws\S3\S3Client
    arguments:
        -
            version: '%amazon_s3.version%'
            region: '%amazon_s3.region%'
            credentials:
                key: '%amazon_s3.key%'
                secret: '%amazon_s3.secret%'

html.twig

<a class="btn btn-primary" href="{{ pdf|get_path }}" target="_blank">Afficher le PDF actuel</a>

AppBundle\\Resources\\config\\services.yml

app.twig_filter_extension:
    class: AppBundle\Twig\AppFilterExtension
    public: true
    arguments:
        - '%kernel.environment%'
        - '%prod_env%'
        - '%kernel.root_dir%'
        - '@router.request_context'
        - '@app.aws_s3.client'
        - '%amazon_s3.media_bucket%'
    tags:
        - { name: twig.extension }

AppBundle\\Twig\\AppFilterExtension.php

<?php

namespace AppBundle\Twig;

use Aws\S3\S3Client;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Routing\RequestContext;

class AppFilterExtension extends \Twig_Extension
{
/**
 * @var string
 */
private $env;

/**
 * @var bool
 */
private $prodEnv;

/**
 * @var S3Client
 */
private $s3Client;

/**
 * @var string
 */
private $s3Bucket;

/**
 * @var Filesystem
 */
private $fileSystem;

/**
 * @var string
 */
private $rootPath;

/**
 * @var string
 */
private $baseUrl;

/**
 * AppFilterExtension constructor.
 *
 * @param string $env
 * @param bool   $prodEnv
 */
public function __construct(string $env, bool $prodEnv, string $rootDir, RequestContext $requestContext, S3Client $s3Client, string $s3Bucket)
{
    $this->env     = $env;
    $this->prodEnv = $prodEnv;

    $this->fileSystem = new Filesystem();
    $this->s3Client   = $s3Client;
    $this->s3Bucket   = $s3Bucket;
    $this->rootPath   = $rootDir . '/../web/';
    $this->baseUrl    = sprintf('%s://%s', $requestContext->getScheme(), $requestContext->getHost());
}

public function getFilters()
{
    return [
        new \Twig_SimpleFilter('get_path', [$this, 'getPath']),
    ];
}

/**
 * @param string      $uri
 * @param null|string $folder
 *
 * @return string
 */
public function getPath(string $uri, string $folder = null)
{
    if (
        (
            'dev' === $this->env ||
            false === $this->prodEnv
        ) &&
        true === $this->fileSystem->exists(str_replace('//', '/', sprintf('%s/%s/%s', $this->rootPath, ($folder ?? 'media'), $uri)))
    ) {
        return $this->baseUrl . str_replace('//', '/', sprintf('/%s/%s', ($folder ?? 'media'), $uri));
    }

    return str_replace('//', '/', $this->s3Client->getObjectUrl($folder ?? $this->s3Bucket, $uri));
}

/**
 * @return string
 */
public function getName()
{
    return 'app_filter_extension';
}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM