Search code examples
symfonyassetic

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?


Solution

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