如何在 Symfony 控制器中获取资产 img url [英] How to get assets img url in Symfony controller
问题描述
我在 Symfony 2.7.10 中使用资产,我想在控制器中生成一个图像 url.
I'm using assets in Symfony 2.7.10 and I want to generate an image url in the controller.
在 symfony config.yml 文件中,我添加了以下设置:
In the symfony config.yml file I have added the following setting:
framework:
assets:
version: '311nk2'
version_format: '%%s?v=%%s'
base_path: /bundles/myBundleName
在 twig 中生成 url 工作正常,因此使用以下 twig 代码:
In twig the generating of an url works ok, so with the following twig code:
{{ asset('images/logo.png') }}
它产生:
/bundles/myBundleName/images/logo.png?v=311nk2
现在我想在控制器中生成相同的 url,这怎么可能?
Now I want to generate the same url in a controller, how is this possible?
推荐答案
好吧,看到这个答案随着时间的推移得到了一些关注,我应该指出两点:
All right, seeing that this answer get a little bit of attention over the time, I should point out two things:
- 当我们在 SO 中仍有基于版本的标记时添加了此答案,并且该问题最初是为 Sf2 添加的
- 从那以后,事情发生了很大变化,所以我要扩展答案
如何使用容器服务处理 Symfony 2.8.36 的资产 url:
How to handle asset url for Symfony 2.8.36 using the Container service:
public function indexAction(Request $request)
{
/** @var SymfonyBundleFrameworkBundleTemplatingHelperAssetsHelper $manager */
$manager = $this->get('templating.helper.assets');
return $this->render('default/index.html.twig', array(
'base_dir' => realpath($this->container->getParameter('kernel.root_dir').'/..').DIRECTORY_SEPARATOR,
'logo' => $manager->getUrl('bundles/myBundleName/images/logo.png'),
));
}
如何使用容器服务和新的 Autowire 配置处理 Symfony 3.4.6 的资产 url:
How to handle asset url for Symfony 3.4.6 using Container service and the new Autowire configuration:
public function indexAction(Request $request)
{
/** @var SymfonyComponentAssetPackages $manager */
$manager = $this->get('assets.packages');
return $this->render('default/index.html.twig', [
'base_dir' => realpath($this->getParameter('kernel.project_dir')).DIRECTORY_SEPARATOR,
'logo' => $manager->getUrl('bundles/myBundleName/images/logo.png'),
]);
}
和
public function autowireAction(Request $request, SymfonyComponentAssetPackages $assetsManager)
{
return $this->render('default/index.html.twig', [
'base_dir' => realpath($this->getParameter('kernel.project_dir')).DIRECTORY_SEPARATOR,
'logo' => $assetsManager->getUrl('bundles/myBundleName/images/logo.png'),
]);
}
如何仅使用 Autowire 配置处理 Symfony 4.0 的资产 URL:
How to handle asset url for Symfony 4.0 only with Autowire configuration:
public function indexAction(Request $request, SymfonyComponentAssetPackages $assetsManager)
{
$path = $assetsManager->getUrl('bundles/myBundleName/images/logo.png');
}
对于 Symfony 4,有几件事需要了解:
For Symfony 4 there are few things to know:
- 项目的框架略有变化,现在您必须包含您将需要的包.在这种情况下,默认情况下不包括资产管理,因此为了使该服务正常工作,您需要执行
composer require symfony/asset
.
使用名为 templating.helper.assets
的辅助服务可以解决问题.尝试这样的事情:
Using the helper service called templating.helper.assets
could do the trick.
Try something like this:
var_dump($this->get('templating.helper.assets')->getUrl('bundles/myBundleName/images/logo.png'));
这篇关于如何在 Symfony 控制器中获取资产 img url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!