如何在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
在树枝中,可以正常生成网址,因此使用以下树枝代码即可:
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添加的.
- 从那时起,情况发生了很大变化,所以我要扩大答案
如何使用Container服务处理 Symfony 2.8.36 的资产网址:
How to handle asset url for Symfony 2.8.36 using the Container service:
public function indexAction(Request $request)
{
/** @var \Symfony\Bundle\FrameworkBundle\Templating\Helper\AssetsHelper $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'),
));
}
如何使用容器服务和新的自动装配配置来处理 Symfony 3.4.6 的资产网址:
How to handle asset url for Symfony 3.4.6 using Container service and the new Autowire configuration:
public function indexAction(Request $request)
{
/** @var \Symfony\Component\Asset\Packages $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, \Symfony\Component\Asset\Packages $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'),
]);
}
如何仅使用自动装配配置来处理Symfony 4.0的资产网址:
How to handle asset url for Symfony 4.0 only with Autowire configuration:
public function indexAction(Request $request, \Symfony\Component\Asset\Packages $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屋!