Symfony2 Assetic get asset urls from inside controller, not template

Piddien picture Piddien · Mar 26, 2013 · Viewed 17.4k times · Source

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?

Answer

tomas.pecserke picture tomas.pecserke · Mar 26, 2013

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