Symfony2 Assetic 从控制器内部获取资产 URL,而不是模板 [英] Symfony2 Assetic get asset urls from inside controller, not template

查看:32
本文介绍了Symfony2 Assetic 从控制器内部获取资产 URL,而不是模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将 url 包含在控制器中我的包中的资产之一,而不是模板中.

I want to include the url to one of the assets in my bundle in the controller, not in the template.

更具体地说,但不是很重要:我使用 obHighChartsBundle 制作了一个 HighChart 图表,我必须在我生成的图表的某些点上绘制一些特殊的图标.因此,我需要图标的 url,它位于我的包的资产文件夹中.

More specifically, but not very important: I make a HighChart chart using obHighChartsBundle, and I have to plot some special icons in some points of the graph I am producing. Therefore I need the url to the icon, which is in my bundle's asset folder.

是否可以从控制器调用某种资产管理器并获取该资产的正确网址?

Is it possible to call some kind of asset manager from the controller and get the correct url to this asset?

推荐答案

可以调用($packageName是可选的):

You can call ($packageName is optional):

$this->container->get('assets.packages')->getUrl($path, $packageName);

对于较旧的 Symfony 版本服务称为 templating.helper.assets,因此您使用:

For older Symfony versions service is called templating.helper.assets, so you use:

$this->container->get('templating.helper.assets')->getUrl($path, $packageName);

用法和twig函数一样(其实就是twig函数中调用的).

It's used the same way as twig function (in fact this is called in the twig function).

这篇关于Symfony2 Assetic 从控制器内部获取资产 URL,而不是模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆