Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 Assetic get asset urls from inside controller, not template

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?

like image 210
Piddien Avatar asked Mar 26 '13 18:03

Piddien


1 Answers

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).

like image 122
tomas.pecserke Avatar answered Oct 16 '22 08:10

tomas.pecserke