25

I'm using assets in Symfony 2.7.10 and I want to generate an image url in the controller.

In the symfony config.yml file I have added the following setting:

framework:
    assets:
          version: '311nk2'
          version_format: '%%s?v=%%s'
          base_path: /bundles/myBundleName

In twig the generating of an url works ok, so with the following twig code:

{{ asset('images/logo.png') }}

It generates:

/bundles/myBundleName/images/logo.png?v=311nk2

Now I want to generate the same url in a controller, how is this possible?

3 Answers 3

45

All right, seeing that this answer get a little bit of attention over the time, I should point out two things:

  • This answer was added when we still had version based tagging in SO and the question was originally added for Sf2
  • Since then, things changed quite a bit, so I'm gonna extend the answer

How to handle asset url for Symfony 2.8.36 using the Container service:

public function indexAction(Request $request)
{
    /** @var \Symfony\Bundle\FrameworkBundle\Templating\Helper\AssetsHelper $manager */
    $manager = $this->get('templating.helper.assets');

    return $this->render('default/index.html.twig', array(
        'base_dir' => realpath($this->container->getParameter('kernel.root_dir').'/..').DIRECTORY_SEPARATOR,
        'logo' => $manager->getUrl('bundles/myBundleName/images/logo.png'),
    ));
}

How to handle asset url for Symfony 3.4.6 using Container service and the new Autowire configuration:

public function indexAction(Request $request)
{
    /** @var \Symfony\Component\Asset\Packages $manager */
    $manager = $this->get('assets.packages');

    return $this->render('default/index.html.twig', [
        'base_dir' => realpath($this->getParameter('kernel.project_dir')).DIRECTORY_SEPARATOR,
        'logo' => $manager->getUrl('bundles/myBundleName/images/logo.png'),
    ]);
}

and

public function autowireAction(Request $request, \Symfony\Component\Asset\Packages $assetsManager)
{
    return $this->render('default/index.html.twig', [
        'base_dir' => realpath($this->getParameter('kernel.project_dir')).DIRECTORY_SEPARATOR,
        'logo' => $assetsManager->getUrl('bundles/myBundleName/images/logo.png'),
    ]);
}

How to handle asset url for Symfony 4.0 only with Autowire configuration:

public function indexAction(Request $request, \Symfony\Component\Asset\Packages $assetsManager)
{
    $path = $assetsManager->getUrl('bundles/myBundleName/images/logo.png');
}

For Symfony 4 there are few things to know:

  • The skeleton of the project has changed a bit, and now you are have to include the packages that you are going to need. In this case, by default Assets management is not included, so in order for this service to work you need to execute composer require symfony/asset.

Original answer as of 22.03.2016

Using the helper service called templating.helper.assets could do the trick. Try something like this:

var_dump($this->get('templating.helper.assets')->getUrl('bundles/myBundleName/images/logo.png'));
Sign up to request clarification or add additional context in comments.

5 Comments

The service has to be public to be used in a controller, that is not the case for assets.packages
@FlorentDestremau care to elaborate more, because I believe you've got things wrong. If you refer to v3.4, you can clearly see that I access the service from the container by the service ID. All of the code that I provided is tested as well. As for the Autowire of the same exaple, it get resolved automatically, though I am not entirely sure if is the same instance, I should check on that.
I tried the same code and got errors because the service was not available. A look at the codebase shows it is not public : github.com/symfony/framework-bundle/blob/master/Resources/…
Problem in Symfony 3.4: I always get the error message "Controller requires that you provide a value for the "$assetsManager" argument". Autowiring works in Services, but not in Controller. Any help?
Hmm, no idea @tobidude. Did you tried to use some custom service as autowired argument in your controller? Does it work with something other than request and entity objects? WHen I updated my answer, I've downloaded the exact versions I specified in my answer, without modifying any configuration and tested it out prior to writing the answer.
13

if you are using symfony 3.4+ with auto-wiring you can DI the Packages instance and use that. eg say for a controller action function:

use Symfony\Component\Asset\Packages;

public function myAction(Packages $assetPackage)
{
   $url = $assetPackage->getUrl('css/main-ie.css');
}

1 Comment

Indeed, and the package to use is this next one: use Symfony\Component\Asset\Packages;
6

But in symfony 3. It's not working anymore.

In symfony 3 use

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

Ex.

private function getAssets($path)
{
    return $this->container->get('assets.packages')->getUrl($path);
}

$this->getAssets(...your path...);

1 Comment

User Deprecated: The "assets.packages" service is private, getting it from the container is deprecated since Symfony 3.2 and will fail in 4.0. You should either make the service public, or stop using the container directly and use dependency injection instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.