gpt4 book ai didi

php - 如何在 twig 中显示使用 gaufrette 上传的文件的链接?

转载 作者:行者123 更新时间:2023-12-03 04:21:04 27 4
gpt4 key购买 nike

我想展示一个已使用 gaufrette 上传的 PDF 文件。我从 Doctrine 获取 URI(“wine_information/1-wine_information.pdf”)。

但如果根目录位于本地或远程 (Amazon s3),根目录可能会有所不同。

我尝试使用 liip_imagine 的过滤器 Twig 来使用 CacheManager.php 和 getBrowserPath 方法,但我无法创建一个,因为我没有可应用于我的 PDF 的过滤器。它仅适用于图像。

现在我找到了这个解决方案:https://github.com/KnpLabs/Gaufrette/issues/332#issuecomment-159849533

UploaderService.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> 中设置一个真正的链接

最佳答案

我找到了解决方案。我制作了一个 twig 过滤器 来获取使用 gaufrette 上传的文件的路径。用于Amazon S3本地的解决方案。

app\config\services.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';
}
}

关于php - 如何在 twig 中显示使用 gaufrette 上传的文件的链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54197444/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com