Symfony2 Assetic get asset urls from inside controller, not template

安稳与你 提交于 2019-11-29 11:51:02

问题


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

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?


回答1:


You can call ($packageName is optional):

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

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

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

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



来源:https://stackoverflow.com/questions/15644598/symfony2-assetic-get-asset-urls-from-inside-controller-not-template

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!